In: Computer Science
a) Which lines of code contain operations that change the contents of memory in this program?
b) What are those operations?
int main ( void ){
double dScore1;
double dScore2;
double dScore3;
double dAverage;
printf("Enter score 1: "); //line 1
scanf("%lg", &dScore1); //line 2
printf("Enter score 2: "); //line 3
scanf("%lg", &dScore2); //line 4
printf("Enter score 3: "); //line 5
scanf("%lg", &dScore3); //line 6
dAverage = (dScore1 + dScore2 + dScore3) / 3.0; //line 7
printf("The average is %g\n", dAverage); //line 8
return 0;
}
int main ( void ){
double dScore1;
double dScore2;
double dScore3;
double dAverage;
printf("Enter score 1: "); //line 1
scanf("%lg", &dScore1); //line 2
printf("Enter score 2: "); //line 3
scanf("%lg", &dScore2); //line 4
printf("Enter score 3: "); //line 5
scanf("%lg", &dScore3); //line 6
dAverage = (dScore1 + dScore2 + dScore3) / 3.0; //line 7
printf("The average is %g\n", dAverage); //line 8
return 0;
}
a) In the above program, line 2 , line 4 and line 6 (marked as green) change the contents of memory.
b) In the scanf() function we are using & (address resolution operator). Now, &dScore1 , &dScore2 and &dScore4 specify the address of dScore1 , dScore2 and dScore3 variables. So, by using scanf() function, user takes the input of dScore1 , dScore2 and dScore3 from the user and store those values in the memory address of dScore1 , dScore2 and dScore3 variables. So, the memory contents of these variables will be changed by these new values (values which are entered by the user as an input).