Please explain the solutions and the code line by line. // Q3. complete the func
ID: 3731729 • Letter: P
Question
Please explain the solutions and the code line by line. // Q3. complete the function0 above. Parameters that // function0 takes are two addresses // and exchange the two values of these addresses // and call function0 to exchage x[3] and x[5]. x[3] = 3; x[5]=5; printf(" 3 5 ? %d %d ", x[3], x[5]); function0(&x[3], &x[5]); printf(" 5 3 ? %d %d ", x[3], x[5]); // Q4. complete the struct xyz on the top of the page // to have three floating point numbers: x, y, and z. // And, create an array of struct xyz, size 20 (20) // Q5. create an array of type XYZ and a pointer to XYZ type // and assign the address of the 4th element of the array // to that pointer. // solution to Q5 XYZ *xyzptr, xyzpoint[10]; xyzptr = &xyzpoint[4]; // Q6. using the pointer you created above, assign 1.1 to all the // structure members: x, y, and z. // solution to Q6 xyzptr->x = xyzptr->y = xyzptr->z=1.1; printf("%f %f %f ", xyzptr->x , xyzptr->y , xyzptr->z); // Q7. A text file, "file.dat" contains ascii data // 1.1 1.2 // 1.3 1.4 // 1.5 1.6 // 1.7 1.8 // 1.9 1.0 // write code to read these numbers into an array of strusture below typedef struct { float x, y; } POINT; POINT pointarray[5]; FILE *fp; fp = fopen("file.dat", "r"); for(i=0; i<5; i++) fscanf(fp, "%f %f", &pointarray[i].x, &pointarray[i].y); fclose(fp); for(i=0; i<5; i++) printf("%f %f ", pointarray[i].x, pointarray[i].y); printf(" "); // Q8. Read the above data into a liked list. Then, delete the node // whose x is 1.7. typedef struct ll { float x, y; struct ll *nextll; } NODE; NODE rtnode, *newptr, *currentptr; fp = fopen("file.dat", "r"); rtnode.nextll = NULL; currentptr = &rtnode; for(i=0; i<5; i++) { fscanf(fp, "%f %f", &fx, &fy); newptr = malloc( sizeof(NODE) ); newptr->x = fx; newptr->y = fy; newptr->nextll = NULL; currentptr->nextll = newptr; currentptr = newptr; // or currentptr = currentptr->next; } fclose(fp); printf(" "); currentptr = rtnode.nextll; for(i=0; currentptr; i++) { printf("trav ll (%u) %f %f ", currentptr,currentptr->x, currentptr->y); currentptr = currentptr->nextll; } printf(" "); // Q9. Add a node with x value 2.0 after the node whose data is 1.3. currentptr = rtnode.nextll; for(i=0; currentptr; i++) { printf("-- trav ll (%u) %f %f ", currentptr, currentptr->x, currentptr->y); if( (currentptr->x > 1.299) && (currentptr->x <1.301) ) //if( currentptr->x ==1.3 ) { printf(" 1.3 found.... "); newptr = malloc( sizeof(NODE) ); newptr->x = 2.0; newptr->y = 2.0; newptr->nextll = currentptr->nextll; currentptr->nextll = newptr; printf(" 1.3 found.... breaking ... "); break; } currentptr = currentptr->nextll; } printf(" "); currentptr = rtnode.nextll; for(i=0; currentptr; i++) { printf("trav ll (%u) %f %f ", currentptr, currentptr->x, currentptr->y); currentptr = currentptr->nextll; } printf(" "); // Q10. Write code to approximate the area below f(x)=x*x+2 // from x=a to b with x interval interval. {double x, interval, a, b, xval, h, area; x = 0.000000001; a = 0.0; b = 0.5; area = 0.0; for( xval = a; xval<b; xval += x ) { h = xval*xval +2.0; area += h * x; } printf("area = %f ", area); } // assume x, interval, a, and b are given already currentptr = &rtnode; for(i=0; currentptr; i++) { printf("==-- trav ll (%u) %f %f ", currentptr->nextll, currentptr->nextll->x, currentptr->nextll->y); if( (currentptr->nextll->x > 1.299) && (currentptr->nextll->x <1.301) ) //if( currentptr->x ==1.3 ) { printf(" 1.3 found.... "); newptr = malloc( sizeof(NODE) ); newptr->x = 2.0; newptr->y = 2.0; newptr->nextll = currentptr->nextll; currentptr->nextll = newptr; printf(" 1.3 found.... breaking ... "); break; } currentptr = currentptr->nextll; } printf(" "); currentptr = rtnode.nextll; for(i=0; currentptr; i++) { printf("trav ll (%u) %f %f ", currentptr, currentptr->x, currentptr->y); currentptr = currentptr->nextll; } printf(" "); }
Explanation / Answer
Some of questions already answered. The answers are correct... I answered those which questions are not asnwered...
Still you need any help, give me a comment...
Please Don't forget to give thumb, if you are satisfied...
Q3)
function0(int *val1, int *val2){
int t = *val1;
*val1 = *val2;
*val2 = t;
}
x[3] = 3;
x[5] = 5;
printf("3 5 ? %d %d ", x[3], x[5]);
function0(&x[3], &x[5]);
printf("3 5 ? %d %d ", x[3], x[5]);
Q4)
struct xyz{
float x;
float y;
float z;
};
struct xyz xyzpoint[20];
Q5)
typdef struct xyz XYZ;
XYZ *xyzptr, xyzpoint[10];
xyzptr = &xyzpoint[4];
Q6)
xyzptr->x = xyzptr->y = xyzptr->z=1.1;
printf("%f %f %f ", xyzptr->x , xyzptr->y , xyzptr->z);
Q7)
typedef struct{
float x, y;
} POINT;
POINT pointarray[5];
FILE *fp;
fp = fopen("file.dat", "r");
for(i=0; i<5; i++)
fscanf(fp, "%f %f", &pointarray[i].x, &pointarray[i].y);
fclose(fp);
for(i=0; i<5; i++)
printf("%f %f ", pointarray[i].x, pointarray[i].y);
printf(" ");
Q8)
typedef struct ll{
float x, y;
struct ll *nextll;
} NODE;
NODE rtnode, *newptr, *currentptr;
fp = fopen("file.dat", "r");
rtnode.nextll = NULL;
currentptr = &rtnode;
for(i=0; i<5; i++){
fscanf(fp, "%f %f", &fx, &fy);
newptr = malloc( sizeof(NODE) );
newptr->x = fx;
newptr->y = fy;
newptr->nextll = NULL;
currentptr->nextll = newptr;
currentptr = newptr; // or currentptr = currentptr->next;
}
fclose(fp);
printf(" ");
currentptr = rtnode.nextll;
for(i=0; currentptr; i++){
printf("trav ll (%u) %f %f ", currentptr,currentptr->x, currentptr->y);
currentptr = currentptr->nextll;
}
printf(" ");
//deleting node whose x value is 1.7
currentptr = rtnode.nextll;
NODE *prev;
for(i=0; currentptr; i++){
if(currentptr->x==1.7){
prev->nextll = currentptr->nextll;
free(currentptr);
break;
}
prev = currentptr;
currentptr = currentptr->nextll;
}
printf(" ");
currentptr = rtnode.nextll;
for(i=0; currentptr; i++){
printf("trav ll (%u) %f %f ", currentptr,currentptr->x, currentptr->y);
currentptr = currentptr->nextll;
}
printf(" ");
Q9)
currentptr = rtnode.nextll;
for(i=0; currentptr; i++)
{
printf("-- trav ll (%u) %f %f ", currentptr, currentptr->x, currentptr->y);
if( (currentptr->x > 1.299) && (currentptr->x <1.301) )
//if( currentptr->x ==1.3 )
{
printf(" 1.3 found.... ");
newptr = (NODE *)malloc( sizeof(NODE) );
newptr->x = 2.0;
newptr->y = 2.0;
newptr->nextll = currentptr->nextll;
currentptr->nextll = newptr;
printf(" 1.3 found.... breaking ... ");
break;
}
currentptr = currentptr->nextll;
}
printf(" ");
currentptr = rtnode.nextll;
for(i=0; currentptr; i++)
{
printf("trav ll (%u) %f %f ", currentptr, currentptr->x, currentptr->y);
currentptr = currentptr->nextll;
}
printf(" ");
Q10)
void computeArea(){
double x, interval, a, b, xval, h, area;
x = 0.000000001;
a = 0.0;
b = 0.5;
area = 0.0;
for( xval = a; xval<b; xval += x ){
h = xval*xval +2.0;
area += h * x;
}
printf("area = %f ", area);
}