Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Second attempt to get the correct answer.. Modify the grade book code below so t

ID: 3582777 • Letter: S

Question

Second attempt to get the correct answer..

Modify the grade book code below so that it uses heap memory to store percentage grades in the range from 0 to 100 (inclusive). The program should allow the user to indicate when he or she is done entering grades (since the user may not have grades to fill the whole array). When the user is done entering grades, the program should print out the grades entered by the user. Be sure to free the head memory before the program ends. Add comments to the modified code.

#include "stdio.h"

int main(void)

{

//initialize array
int arr[100];

  //initialize variables
  int i=0, j=0, n=0;

   //infinite loop which will stop when user enters -1
  while(n != -1)

{
  

printf("Enter percentage grade(0-100). Enter -1 to stop: ");

//read grade
  scanf("%d",&n);

  //if user entered grade is not -1
if(n != -1)
{
//save it to array
arr[i++] = n;
  
}
//if user entered -1, then exit this loop
else
{

break;

}  
  
}
  
  
printf(" The grades are: ");

//loop which will iterate till no:of user entered grades
for(j=0; j<i; j++)

{
  //print the grade
  printf("%d ",arr[j]);

}
  
return 0;

}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
int main(void)
{

int *arr = malloc(sizeof(int)*100); //dynamic memory allocation
//initialize variables
int i=0, j=0, grade=0;
//infinite loop which will stop when user enters -1
while(grade != -1)
{
  
printf(" Enter percentage grade(0-100). Enter -1 to stop: ");
//read grade
scanf("%d",&grade);
//if user entered grade is not -1
if(grade != -1)
{
//save it to array
arr[i++] = grade;
  
}
//if user entered -1, then exit this loop
else
{
break;
}
  
}
  
  
printf(" The grades are: ");
//loop which will iterate till no:of user entered grades
for(j=0; j<i; j++)
{
//print the grade
printf("%d ",arr[j]);
}
free(arr); //deallocate memory
return 0;
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote