Question

PLEASE EXPLAIN THE ANSWER The file data1 contains: 0 3 6 9 12 The file data2...

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;

}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL && pointVar2 == NULL) {       printf("Pointer is null\n");    }    else if (*pointVar2 > *pointVar1) {       printf("%p\n", *pointVar2);    }    else if (*pointVar1 > *pointVar2) {       printf("%p\n", *pointVar1);    } } int main() {    int num1 = 5;    int num2 = 9;       CheckValue(&num1, &num2);           return 0; } a. 0 b. 5 c. 9 d. Pointer is null e....
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT