PLEASE EXPLAIN THE ANSWER
The file data1 contains: 0 3 6 9
12
The file data2 contains: 10 8 6 4
2
The file data3 contains: 20 10 0 20
80
Give the output when run with: ./a.out data1 data2
data3
#include <stdio.h> int main( int argc, char *argv[] ) {
FILE *fpOne = fopen(argv[1], "r"); FILE *fpTwo = fopen(argv[2], "r"); FILE *fpThree = fopen(argv[3], "r"); int a, num1, num2, num3, sum1=0, sum2=0; for (a=0; a<5; a++) {
fscanf(fpOne, "%d", &num1); fscanf(fpTwo, "%d", &num2); fscanf(fpThree, "%d", &num3); if (num1 < num2) {
printf("%d\n", num1 + num3);
sum1 = sum1 + num1; }
else { printf("%d\n", num2 * num3);
sum2 = sum2 + num2; }
} printf("%d and %d\n", sum1, sum2); fclose(fpOne);fclose(fpTwo);fclose(fpThree); return 0;
}
Output
$ ./a.out data1 data2 data3
20
13
0
80
160
3 and 12
Explaination:
at the Begining
sum1=0
sum2=0
there are five rounds of for-loop
Round 1:
-------
num1 = 0
num2 = 10
num3 = 20
here (num1 < num2) hence
print num1+num3=0+20=20
sum1 = sum1+num1=0+0=0
Round 2:
-------
num1 = 3
num2 = 8
num3 = 10
here (num1 < num2) hence
print num1+num3=3+10=13
sum1 = sum1+num1=0+3=3
Round 3:
-------
num1 = 6
num2 = 6
num3 = 0
here (num1 IS NOT < num2) hence
print num2 * num3=6*0=0
sum2 = sum2 + num2 = 0+6=6
Round 4:
-------
num1 = 9
num2 = 4
num3 = 20
here (num1 IS NOT < num2) hence
print num2 * num3=4*20=80
sum2 = sum2 + num2 = 6+4=10
Round 5:
-------
num1 = 12
num2 = 2
num3 = 80
here (num1 IS NOT < num2) hence
print num2 * num3=2*80=160
sum2 = sum2 + num2 = 10+2=12
hence at the end, sum1=3, sum2=12
Get Answers For Free
Most questions answered within 1 hours.