Array Basics in C++ : Find Sum

Video tutorial to show the basic use of arrays: Initialization, Declaration, Getting values from the users, finding sum of all the array elements etc.

Array is a collection of homogeneous data items.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream .h>
#include<conio .h>
 
void main()
{
  //  int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
 
  int a[5], sum = 0;
  clrscr();
 
  cout< <"Enter 5 numbers\n";
  for(int i=0; i&lt;5; i++)
   cin>>a[i];
 
  cout< <"Input array is..\n";
  for(i=0; i&lt;5; i++)
  {
   cout<<a[i]<<endl;
   sum = sum + a[i];  // sum += a[i];
  }
  cout<<"Sum of array elements: "<<sum;
 
  getch();
 
}

In this program we take input from the user and display the user entered numbers and find the sum of all the array elements and display it as well.

Elements are stored from the index number 0. i.e., if the array size is 5, the values will be stored in a[0], a[1], a[2], a[3], a[4];

Ex:
int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
float a[5] = { 1.1, 2.0, 3.3, 1.3, 5.6 };

Video Tutorial: Array Basics in C++ : Find Sum


[youtube https://www.youtube.com/watch?v=BfAa2koJv64]

YouTube Link: https://www.youtube.com/watch?v=BfAa2koJv64 [Watch the Video In Full Screen.]



Output:
Enter 5 numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Sum of array elements: 15