You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. A simple program that shows how to access single elements and all the elements in an array.
// How to access a declared array in c++
#include<iostream>usingnamespacestd;intmain() {
int marks[] = {47,34,372,62,52,42,52,62};
//accessing single element
cout << marks[3] << endl; //47//accessing all elementsfor(int i=0; i < 8; i++) {
cout << marks[i] << "";
}
return0;
}
2. Program that finds the sum of all the elements in an array using for loop
// Find the sum of all the elements of an array
#include<iostream>usingnamespacestd;intmain() {
int marks[] = {47,34,372,62,52,42,52,62};
int sum = 0;
// sum of all the elements in arrayfor(int i=0; i < 8; i++) {
sum += marks[i];
}
cout << "The sum of all elements is " << sum;
return0;
}