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

I did some of them right, but I keep getting the error message on visual studio.

ID: 3791497 • Letter: I

Question

I did some of them right, but I keep getting the error message on visual studio. Need help how to it set up and complete this exercise?

1.      (Housekeeping): Declare the following variables in your program using appropriate data type. When selecting a data type keep in mind the type of value you want to store, i.e., int, float, character. Also, be sure to use conventional naming standards for all variable declarations.

·         n //variable used to store the number of values the user wishes to average

·         i //counter variable to loop through each array element

·         num[10] //array structure to hold 10 values

·         sum

·         average

       (Hint): Since sum is going to be used for adding the value, it needs to be initialized to zero.

2. (Display Prompt): Write a printf() statement to ask user for the following message.

3. (Input): Add a scanf() statement to store user input for the number of values they wish to process.

4. (Loop structure): Create a looping structure to catch any user input that may be out of the arrays storage range. If the user types in a value less than 0 OR greater than 10 print out a message telling the user that they entered an invalid number. Don’t forget to prompt the user to enter a valid number and store that value just like you did not steps 1 and 2 above.

5. (Loop structure): Now, you will write a loop structure to store the user input of values to be averaged into each element of the array.

To begin with, your loop structure will look like this –

6. (Assignment Statement): Outside of the loop you want to create an assignment statement to compute the averages of the values stored in each array element.

7. (Output): Write a printf() statement to display the value averaging the numbers stored in the array. Remember, the variable sum is declared outside of the loop so it can be referred/used anywhere within the main function. Also, to display the value stored in a variable you need to include a conversion specifier inside of the quotation marks in the printf() and the variable names within the parentheses.

8.(Output Screen) When you execute the program, it should be similar to the following:

Enter the number of values to be averaged:

Explanation / Answer

#include <stdio.h>

int main()
{
//declare an array , num, i-to iterate , sum- store sum
int arr[10], num, i, sum=0;
float average;
  
//read number of values to be averaged until user gives number in range 1-10
do{
printf("Enter Number of values to be averaged:");
scanf("%d", &num);
if(num < 0 || num >10)
printf("Invalid Number. ");
}while(num<0 && num>10);
  
//read numbers of user, store the same to array, compute the sum
for(i=0; i<num ; i++){
printf("Enter the number:");
scanf("%d", &arr[i]);
sum+=arr[i];
}
  
//assign to average variable
average = (float)sum/num;
  
//finally print the result
printf(" Average = %f ",average);
return 0;
}

Sample output:

Enter Number of values to be averaged:5                                                                                                                                               

Enter the number:25                                                                                                                                                                   

Enter the number:25                                                                                                                                                                   

Enter the number:35                                                                                                                                                                   

Enter the number:45                                                                                                                                                                   

Enter the number:23                                                                                                                                                                   

Average = 30.600000

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