Write a program that allows the user to enter an integer n, and then generates n
ID: 3832328 • Letter: W
Question
Write a program that allows the user to enter an integer n, and then generates n random numbers between 1 and 100. The program calculates and prints the sum, average, and all the number above the average. Draw the flowchart for the program on the back this paper. Open Textpad and save the source file on the desktop using the file name as lastname final Write your name on the first line of the source file, and short comment for each task in the program. Leave the TextPad open and this paper on the keyword when you finish the exam.Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#define range 100
int main()
{
int n, sum = 0;
double average;
int random_number = 0;
srand( time(NULL));
printf("Enter an Integer-> ");
scanf("%d", &n);
int *arr =(int*)(malloc(n * sizeof(int) ) );
printf("%d random numbers are generated: ", n);
for(int i = 0; i<n; i++)
{
random_number = rand() % range + 1;
printf("%d ", random_number);
arr[i] = random_number;
sum+=random_number;
}
average = (double)sum/n;
printf(" The Sum of the random numbers is: %d " , sum);
printf(" The Average of the random numbers is: % 0.2lf ", average);
int *arr2 = (int*)(malloc(n * sizeof(int) ));
int j = 0;
// getting above average numbers
for(int i=0; i<n; i++)
{
if(arr[i]>average)
{
arr2[j] = arr[i];
j++;
}
}
printf(" There are %d random numbers above the average : ", j+1);
// printing above average numbers
for(int i=0; i<j;i++)
{
printf("%d ", arr2[i]);
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.