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

I have the following C code ( not C++). The program reads in up to 20 numbers (u

ID: 3764351 • Letter: I

Question

I have the following C code ( not C++). The program reads in up to 20 numbers (user does not have to put all 20) and stores them in an array. The max, min, and average values of the array are displayed. The problem is that when the program asks the user to enter the first value, if they press q to quit, it is displaying garbage. Can someone help me figure out the issue.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>   

int arrmin(int nums[], int num_of_elmnts)
{
int min=nums[0];   
int i=0;   
for (i;i<num_of_elmnts; i++)   
{
if (nums[i]<min)
{
min=nums[i];   
}
}
return min;
}

int arrmax(int nums[], int num_of_elmnts)
{
int max=nums[0];
int i=0;
for (i;i<num_of_elmnts; i++) //for loop that will be used to compare the elements and set the max value
{
if (nums[i]>max)
{
max=nums[i];
}
}   
return max;
}

double arraver(int nums[], int num_of_elmnts)
{
int sum = 0;
int i = 0;   
for (i;i<num_of_elmnts; i++)   
{
sum += nums[i];   
}
return sum/(float)num_of_elmnts;   
}   

int main()   
{   
printf("Enter up to 20 numbers or q to quit ");
int num_of_elmnts=20;   
int nums[num_of_elmnts];
int i=0;
for (i; i<num_of_elmnts; i++)
{
char num[30];   

if(i+1 == 1)   
strncpy(num,"st number ", 30);   
else if(i+1 == 2)
strncpy(num,"nd number ", 30);
else if (i+1 == 3)   
strncpy(num,"rd number ", 30);
else   
strncpy(num,"th number ", 30);

printf("Enter the %d%s", i+1, num);   
if (scanf("%d", &nums[i]) != 1)
{
num_of_elmnts=i;   
break;   
}   
}   

printf("Minimum number is %d ",arrmin(nums,num_of_elmnts));
printf("Maximum number is %d ",arrmax(nums,num_of_elmnts));
printf("Average is %.2f ",arraver(nums,num_of_elmnts));   

return 0;   
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int arrmin(int nums[], int num_of_elmnts)
{
int min=nums[0];
int i=0;
for (i;i<num_of_elmnts; i++)
{
   if (nums[i]<min)
   {
   min=nums[i];
   }
}
return min;
}
int arrmax(int nums[], int num_of_elmnts)
{
int max=nums[0];
int i=0;
for (i;i<num_of_elmnts; i++) //for loop that will be used to compare the elements and set the max value
{
   if (nums[i]>max)
   {
   max=nums[i];
   }
}
return max;
}
double arraver(int nums[], int num_of_elmnts)
{
int sum = 0;
int i = 0;
for (i;i<num_of_elmnts; i++)
{
   sum += nums[i];
}
return sum/(float)num_of_elmnts;
}
int main()
{
   int num_of_elmnts=20;
int nums[20];
int i=0;

printf("Enter up to 20 numbers or q to quit ");
for (i; i<num_of_elmnts; i++)
{
   char num[30];
if(i+1 == 1)
   strncpy(num,"st number ", 30);
   else if(i+1 == 2)
   strncpy(num,"nd number ", 30);
   else if (i+1 == 3)
   strncpy(num,"rd number ", 30);
   else
   strncpy(num,"th number ", 30);
   printf("Enter the %d%s", i+1, num);
   if (scanf("%d", &nums[i]) != 1)
   {
   num_of_elmnts=i;
   break;
   }
   }
printf("Minimum number is %d ",arrmin(nums,num_of_elmnts));
printf("Maximum number is %d ",arrmax(nums,num_of_elmnts));
printf("Average is %.2f ",arraver(nums,num_of_elmnts));
return 0;
}