C++ or C program to calculate sum and average of elements stored in 1D array.

file name:- sumavg1d.cpp

#include<iostream.h>
#include<conio.h>
#define N 10
void main(){
int list[N],i,sum=0;
float avg=0;
clrscr();
cout<<"Enter Elements of array List"<<endl;
for(i=0;i<N;i++){
cout<<"Enter "<<i+1<<" element=";
cin>>list[i];
sum=sum+list[i];
}
avg=(float)sum/N;
cout<<"Sum of 1D array List="<<sum<<endl<<"Average of 1D array List="<<avg<<endl;
getch();
}

file name:-sumavg1d.c 

#include<stdio.h>
#include<conio.h>
#define N 10
void main(){
int list[N],i,sum=0;
float avg=0;
clrscr();
printf("Enter Elements of Array LIST\n");
for(i=0;i<N;i++){
printf("enter %d element=",i+1);
scanf("%d",&list[i]);
sum=sum+list[i];
}
avg=(float)sum/N;
printf("Sum of 1D array list=%d\nAverage of 1D array list=%.2f",sum,avg);
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...