Create 3 arrays of 5 elements of doubletype. Then use fscanf( ) with a for loop
ID: 3618011 • Letter: C
Question
Create 3 arrays of 5 elements of doubletype. Then use
fscanf( ) with a for loop to fill the firstarray. Use hard coding method as indicated in the class notes tofill the second array.
The third array is used to store the sum ofthe first two arrays, that is irst element of first array is addedto the first element of the second array and the result is assignedto the first element of the third array and so on. Use a for loopto do the adding.
Then use a for loop to print the result in atable format like the following:-
Firstarray Secondarray sum
2.00 10.02 12.02
3.00 20.05 23.05
-- -- --
Explanation / Answer
#include<stdio.h> #include<conio.h> void main() { float first[5],second[5],third[5]; int v; for(v=0;v<5;v++) { printf(" Enter element : "); scanf("%f",&first[v]); }
printf(" Second array ");
for(v=0;v<5;v++) { printf(" Enter element : "); scanf("%f",&second[v]); third[v]=first[v]+second[v]; }
printf("First Array Second Array Third Array "); for(v=0;v<5;v++) { printf("%f %f %f",first[v],second[v],third[v]); printf(" "); }
getch(); }
#include<stdio.h> #include<conio.h> void main() { float first[5],second[5],third[5]; int v; for(v=0;v<5;v++) { printf(" Enter element : "); scanf("%f",&first[v]); }
printf(" Second array ");
for(v=0;v<5;v++) { printf(" Enter element : "); scanf("%f",&second[v]); third[v]=first[v]+second[v]; }
printf("First Array Second Array Third Array "); for(v=0;v<5;v++) { printf("%f %f %f",first[v],second[v],third[v]); printf(" "); }
getch(); }