write a C program to compute average?
write a program that accepts n numbers from the user (where n <=60, n >=2). The numbers are read into an array. The program then computes the averageof the n numbers and displays it on screen "The average of your numbers is: [computed average]"
Include explicit instructions for the user.
Comments
//Average of numbers
#include<conio.h>
#include<stdio.h>
void main(){
int n,i;
int array[60];
float sum=0.0,avg;
while(n<2||n>60){
clrscr();
printf("Enter the total number of values: ");
scanf("%d",&n);
if(n<2||n>60){
printf("\n\nValue should be between 2 and 60...\n\nPress any key to enter again...");
getch();
}
}
for(i=0;i<n;i++){
printf("Value %d: ",i+1);
scanf("%d",&array[i]);
sum+=array[i];
}
avg=sum/n;
printf("\n\nThe The average of your numbers is: %f",avg);
getch();
}
void main()
{
int num[60]; //declare an array for 60 elements
int i,entries=0,sum=0,avg=0;
char c;
for(i=0;i<60;i++)
num[i]=0; //clear all the junk values in the array
for(i=0 ;i<60;i++)
{
printf("Enter the number");
scanf("%d",&num[i]);
if(i>=1) //after entering the second element in the array
{
printf("Do u want to stop entering values( Y | N )");
scanf("%c",&c);
}
if(c=='y' || c=='Y')
break; //break out of the loop if yeas
}
entries = i+1; // since i starts from 0.
for(;i>=0;i--)
{
sum=sum+num[i];
}
avg=sum/entries;
printf("The average of your numbers is : %d",avg);
getch();
}
int *n = new int[60];
int avg = 0;
int count = 0;
for (int x = 0; x < 60; x++)
{
cin >> (*n);
if (! (*n))
break;
*n++;
}
while (*n)
{
avg += (*n);
count++;
}
avg /= count;
printf("Avg. is:\t%f", avg);
Something like that. That way you don't have a million 'for' loops. LoL. =P Optimization is key to a successful app.
include <iostream> using namespace std; int main() { int number,sum,max,i; float ave; cout<<"Enter 3 integers: "; cin>>number; sum=number; max=number; for(i=2;i<=3;i++) { cin>>number; sum+=number; if(max<number) max=number; } ave=sum/3.0; cout<<"sum, ave, max = "<<sum<<", "<<ave<<", "<<max<<endl; system("pause"); return 0; }