In: Computer Science
The following program is supposed to read 10 pairs of 2D coordinates, (xi , yi ), and print the average coordinate. However, it does not work correctly. Fix the program so that it compiles with no errors or warnings and correctly computes the average coordinate.
int main() { float coords[9][2] = { 0 }; float sum[2]; while (i < 2) { scanf("%d%d", &coords[i][0], coords[i][1]); sum[0] += coords[i][0]; sum[1] += coords[i][1]; } printf("Average coordinate: (%.2d, %.2d)\n", sum[0], sum[1]); return 0; }
Need it in 10 minutes, please.
#include<stdio.h>
int main() {
float coords[9][2] = { 0 };
float sum[2];
int i=0;
while (i < 10) {
scanf("%f%f", &coords[i][0], &coords[i][1]);
sum[0]+= coords[i][0];
sum[1]+= coords[i][1];
i+=1;
}
printf("Average coordinate: (%.2f,%.2f)\n",(sum[0]/10),(sum[1]/10));
return 0;
}
The above is the program that will calculate the average of the 10 pair of 2d coordinate and pritns the output.The program asks the user to enter space seperated number for 10 times and then it iwll print the average of the numbers.
SCREENSHOT OF THE OUTPUT :
EXPLAINATION OF THE PROGRAM ERRORS :
int main() { float coords[9][2] = { 0 }; float sum[2]; while (i < 2) { //HERE WE ARE USING I DUT THE I IS NOT DECLARED AND ALSO INORDER TO //ENTER 10 VALUES WE NEED TO USE I <10 scanf("%d%d", &coords[i][0], coords[i][1]); //%d%d BUT FOR FLOAT WE NEED TO USE %f%f //SO WE GET AN WARNING sum[0] += coords[i][0]; sum[1] += coords[i][1]; } printf("Average coordinate: (%.2d, %.2d)\n", sum[0], sum[1]); //here we have float output //then we need to use %.2f,%.2f but we are using %.2d,%.2d so we get an //warning.(HEre we are using %.2f inorder to restrict output to 2 //values) return 0; }
**************************************PLEASE ENCOURAGE US WITH AN OUTPUT***************************************