After entering the values display the following statistics: the third element in the array and the average of all the numbers.
Write a complete C++ program that will let a user input 8 integer values into an array.?
#include%26lt;iostream.h%26gt;
void main()
{
int a[8],i,sum=0;
float avg;
cout%26lt;%26lt;"Enter The 8 numbers :";
for(i=0;i%26lt;8;i++)
{
cin%26gt;%26gt;a[i];
sum+=a[i];
}
avg=sum/8;
cout%26lt;%26lt;"Third Element in the array"%26lt;%26lt;a[2];
cout%26lt;%26lt;"\nAverage of 8 numbers :"%26lt;%26lt;avg;
}
Reply:#input%26lt;stdio.h%26gt;
void main()
{
int i,a,iArr[10];
a= 0;
for(i=0;i%26lt;8;i++)
{ scanf("%d",%26amp;iArr[i]); a += iArr[i]; }
printf("\n\n\t The third number was %d and the average is ", iArr[2] a/8);
// c arrays are 0 based so third element will be index 2
}
Reply:if a[8] is the array u have defined. then follow the below
cout%26lt;%26lt;a[2]; ---- to display 3rd element
b=0;
for(i=0;i%26lt;8;i++)
b+=a[i]; ----- to find the sum of all the numbers in the array
cout%26lt;%26lt;b/8; ---- displays the average of the array
try this....
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment