add integers in an array

Write a program that add all the elements of an integer array. It should be passed to a function. The function returns the value of the summation.

#include<iostream>
using namespace std;
int sum(int[],int);
int main(){
	int s[]={1,2,3,6,8,8,7,8};
	int size = sizeof(s)/sizeof(int);
	cout<<"The numbers are ";
	for(int i=0;i<size;i++){
		cout<<s[i]<<" ";
	}
	cout<<endl<<"The sum of integers in array is "<<sum(s,size);
	return 0;
}
int sum(int s[],int size){
	int sum=0;
	for(int i=0;i<size;i++){
		sum+=s[i];
	}
	return sum;
}
output of the above program. add integers in an array
output of the above program. add integers in an array

Write a program that finds the required value in an array.

Leave a Reply