C++ Arrays

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Array in CPP
#include<iostream>
using namespace std;
int main()
{
    system("cls");
    int arr[5]={10,20,5,40,25};   // 1D Array
    int i;
    cout<<arr[9];      //out of index bound
    // for(i=0;i<5;i++)
    // {
    //     cout<<arr[i]<<"   ";
    // }
   
   
    return 0;
}
/*
   arr[0]-->  10
   arr[1]-->  20
   arr[2]-->  5
   arr[3]--> 40
   arr[4]--> 25
*/

Program 2

// Array in CPP
#include<iostream>
using namespace std;
int main()
{
      int arr[10],i,sum=0;
      system("cls");
      cout<<"Enter 10 elements: ";
      for (int i = 0; i < 10; i++)
      {
        cin>>arr[i];
      }
     cout<<"Elements of Array: ";
      for (int i = 0; i < 10; i++)
      {
         cout<<arr[i]<<"\t";
         sum+=arr[i];
      }
      cout<<"\n Sum of Elements: "<<sum;
}

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *