Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / n the following code snippet, what value will be printed for c and d before the loop is executed? What value will be printed afterward? Why do you feel that this will occur? (Code needed to make this a complete program intentionally left out

n the following code snippet, what value will be printed for c and d before the loop is executed? What value will be printed afterward? Why do you feel that this will occur? (Code needed to make this a complete program intentionally left out

Computer Science

n the following code snippet, what value will be printed for c and d before the loop is executed? What value will be printed afterward? Why do you feel that this will occur? (Code needed to make this a complete program intentionally left out.)

int c = 99;
int a[5];
int d = 12;
printf("c = %dn",c);
printf("d = %dn",d);
while (c < 3)
{
printf("%dn",c);
c++;
}
a[5] = 0;
printf("c = %dn",c);
printf("d = %dn",d);
printf("Donen");
c = getchar();

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Thias code snippet prints before the loop:
c = 99
d = 12

Thias code snippet prints after the loop:
c = 99
d = 12

And then it crashes.

a) The loop never executes, because c (=99) is greater than 3. So, c needs to be less than 3 for the loop to execute. For instance c = 0 ;

b) It crashes because:
a[5] = 0;
accesses memory out of range. The last index of array a is 4, not 5. Because of 0-based indexing in C.

Here is a corrected code snippet (aslo attached):

int c = 0;
int a[5];
int d = 12;
printf("c = %dn",c);
printf("d = %dn",d);

while (c < 3)
{
printf("%dn",c);
c++;
}

a[4] = 0;
printf("c = %dn",c);
printf("d = %dn",d);
printf("Donen");
c = getchar();