1 write the lines to swap the value at index j of array a wi
1. write the lines to swap the value at index j of array a with the value at inex j+1 if the value at index j is greater.
2. write the lines to declare 2 variables one as double and intialize it to 2.5 and the other as character and intialize to character b. then call a function called f and pass these variables by reference.
3. write the function f of previous questions to modify the value of the double by multiplying it by 3 and making the other variable uppercase.
write in c program.
Solution
Assuming that one C program will do all the 3 jobs, the source code for the above question is given below:
#include <stdio.h>
void swap(int[],int);
void f(double*,char*);
int main()
{
int arr[5] = {2,3,4,5,6};
int j = 2;
double d= 2.5;
char c = \'b\';
swap(arr,j);
f(&d,&c);
printf(\"%f\\t%c\ \",d,c);
return 0;
}
void swap(int arr[], int j){
int temp = 0;
if(j<4){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
void f(double *d_ptr, char*c_ptr){
*d_ptr = (*d_ptr)*3;
*c_ptr = \'B\';
}
In the above program, swap function swaps the jth element of the array with the (j+1)th if the jth element is greater than (j+1)th. It takes input the value of j for this purpose.
f function receives the values of the double variable and the character variable through pass by reference(or address, to be precise). d_ptr and c_ptr are pointer variables pointing to the double and character variables respectively. The double variable\'s value is multiplied by 3 and the character variable\'s value is changed to uppercase within f(). Finally, the values are printed in the main().
