This hands-on lab allows you to follow and experiment with the critical steps of
ID: 3818706 • Letter: T
Question
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, design (using pseudocode), and implementation with C code. The example provided uses sequential, selection and repetition statements.
Program Description
This program will calculate the average of 10 positive integers. The program will ask the user to 10 integers. If any of the values entered is negative, a message will be displayed asking the user to enter a value greater than 0. The program will use a loop to input the data.
Explanation / Answer
Solution:-
---------------------------------------------------------------------------------------------------
Program Description:-
The below given C program will calculate the sum of 10 positive integers. If any negative integer input by the user then prints the error message and ask for the positive integer.
---------------------------------------------------------------------------------------------------
Steps:-
The program will calculate the average of 10 positive integers. The For loop is used to enter the user input and assigned to variable n. An If loop also used to check user input positive or negative integer. If negative integer input by the user then it prints the error message and ask for the user to input a positive integer otherwise execute the for loop. The sum of 10 positive integers are stored in variable sum and divided by 10 to calculate average.
---------------------------------------------------------------------------------------------------
Pseudo code:-
Declare i, n, sum = 0
Declare float avg
for n=1 to 10
input number
n=i
if n<=0
Print Error message
endif
sum = sum+n
i++
endfor
avg=sum/10.0
Display avg.
---------------------------------------------------------------------------------------------------
The C program:-
---------------------------------------------------------------------------------------------------
/* READ 10 NUMBER FIND SUM AND AVERAGE */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
float avg;
clrscr();
printf("enter the 10 number ");
for (i=1;i<=10;i++)
{
scanf("%d",&n);
}
avg=sum/10.0;
printf("The sum of 10 no =%d The Average of = %8.2f ",sum,avg);
getch();
}
---------------------------------------------------------------------------------------------------
The above given C program is efficient to calculate the average of 10 positive integers using for loop and test the input using if loop. We can implement this by using while loop also. But the implementation is quite easy and understandable. So this code can be used efficiently used to calculate average of 10 numbers.
---------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.