Give the output of the C program shown below when run with ./a.out 2 3 include #
ID: 3861854 • Letter: G
Question
Give the output of the C program shown below when run with ./a.out 2 3 include # include # include int main (int argc, char *argv []) {char str[] = "CRIMSON-TIDE"; Int X = atoi (argv [1]); int y = atoi (argv[2]); int z = strlen(str); for (int a = x; a 0; b = b - y) printf("%d ", b); return 0; Assume that the file data contains:5 10 25 45 0 15 Give the output of the C program shown below when run with # include # include int main (int argc, char *argv []) {File *fp1 = fopen (argv [1], :x"); Int oldVal, num; fscanf (fpl, '%d", num); oldVal = num; while (! feof (fp1)) {if (num > o1dVal) {printf ("% d ", num oldVal = num;} fscanf (fpl, , "%d", num);} printf ("Answer = % d ", oldVal): return 0;Explanation / Answer
Answer for question1:
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[]){
char str[] = "CRIMSON-TIDE";
int x = atoi(argv[1]);
int y = atoi(argv[2]);
int z = strlen(str);
int a=0, b=0;
for( a=x; a<z; a = a+y) //a value starts from 2 and increments by 3 until reaches the srlen value of string CRIMSON_TIDE that is 12
printf("%c", str[a]); //prints characters at indexes 2, 5, 8, 11 . Hence the output of this print statement is IOTE
printf(" "); // prints in newline
for( b=z; b>0; b=b-y) // b value starts from strlen value that is 12 and decrements by 3
printf("%d ", b); // prints each index value in newline. The index values are 12, 9, 6, 3
return 0;
}
Output:
IOTE
12
9
6
3
Answer for question2:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
FILE *fp1 = fopen(argv[1], "r");
int oldVal, num;
fscanf(fp1, "%d", &num);
oldVal = num;
while( !feof(fp1) ){ //loop runs until end of file is reached
if( num > oldVal){ //checks if the read value from the file is greater than old value. Intially oldVal and nu, are same since num is stored in oldVal variable
printf("%d ", num-oldVal); // this line is not clearly visible in the image provided in the question. I am guessing the statement is like this printf("%d ", num-oldVal); This statement subtracts the oldVal from the read num value and prints it
oldVal = num; // stores the latest num value in oldVal
}
fscanf(fp1, "%d", &num); // reads next number from the file
}
printf("Answer = %d ", oldVal); // prints Answer = <oldVal>
return 0;
}
Output:
5
15
20
Answer = 45
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.