In: Computer Science
Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: 1. Display 10 random numbers stored in the array 2. Compute and display the largest number in the array 3. Compute and display the average value of all numbers 4. Exit The options 2 and 3 should call an appropriate user-defined function and pass the array to the function to compute the largest and the average value respectively. Design and call these two user-defined functions. The average value should be calculated and displayed with a precision of two decimal places. The program should loop back to the main menu until the user selects the option to exit the program. Use the register storage class for two the most frequently used variables in your program. Submit your program's source code (i.e., .c file) and a file with screen captures
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void get_maximum(int *arr)
{
//takes array as argument and displays maximum no from array
int max=0;
int i;
for(i=0;i<10;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
printf("sum=%d",max);
}
void get_average(int *arr)
{
//takes array as argument and displays average of array upto 2 decimal point
int sum=0,i;
for(i=0;i<10;i++)
{
sum+=arr[i];
}
float n=10.0;
float res=sum/n;
printf("Average=%.2f", res);
}
int main()
{
int i,j;
int arr[10];
int lower=1,upper=50,count=10;
srand(time(0)); //current time as seed of random number generator
for(i=0;i<count;i++)
{
int rand_num=(rand() % (lower - upper+ 1)) +lower;//generates random no
arr[i]=rand_num;
}
int temp=0;
while(temp==0)
{
int user;
printf("Enter\n");
printf("1: Display 10 random numbers stored in array:\n");
printf("2: Largest number in array:\n");
printf("3: Average value of all numbers:\n");
printf("4: Exit:\n");
scanf("%d",&user);
if(user==1)
{
for(j=0;j<10;j++)//prints array
{
printf("%d ",arr[j]);
}
}
else if(user==2)
{
get_maximum(arr);
}
else if(user==3)
{
get_average(arr);
}
else if(user==4)
{
temp=1;
}
else
{
printf("Enter number between 1 and 4:");
}
printf("\n");
}
return 0;
}
Screenshots of the code:
screenshot of the output: