Write a main function that declares an array of 100 ints. Fill the array with ra
ID: 3644711 • Letter: W
Question
Write a main function that declares an array of 100 ints.Fill the array with random values between 1 and 100.
Calculate the average of the values in the array. Output the average.
here is what I have so far, not sure if im doing it right.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main(){
int arrayof100[100], index, numberToaverage = 0, numberCount = 0, counter = 0;
double average;
srand(time(NULL)); //seeding random number generater
//filling 100 elements with random numbers between 1 and 100
for(index = 0; index < 100; index++)
arrayof100[index] = rand() % 98 + 2;
//using loops to scan through each element
while(counter <= 100){
numberToaverage++;
numberCount = 0;
counter++;
for(index = 0; index < 100; index++){
arrayof100[index];
//if numberTo average is in the element then count as 1
if(arrayof100[index] == numberToaverage)
numberCount++;
}
if(numberCount > 0){//if number is in the element then get average and print
average = (double)numberCount / 100;
printf("%i average is %.2lf ", numberToaverage, average);
}
}
system("pause");
}
Explanation / Answer
#include #include #include main(){ int arrayof100[100], index, numberToaverage = 0, numberCount = 0, counter = 0; double average; srand(time(NULL)); //seeding random number generater //filling 100 elements with random numbers between 1 and 100 for(index = 0; index < 100; index++) arrayof100[index] = rand() % 98 + 2; //using loops to scan through each element while(counter 0){//if number is in the element then get average and print average = (double)numberCount / 100; printf("%i average is %.2lf ", numberToaverage, average); } } system("pause"); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.