0% found this document useful (0 votes)
101 views3 pages

C++ Array Operations and Examples

The document contains 7 code snippets that demonstrate basic C++ array operations including: 1) Reading and printing arrays in reverse order 2) Adding two arrays element-wise and printing the sum array 3) Multiplying two arrays element-wise and printing the product array with commas 4) Summing the elements of a single array 5) Finding the smallest and largest elements of a single array 6) Searching an array for a given element and printing its position 7) Reading and printing a 2D array
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views3 pages

C++ Array Operations and Examples

The document contains 7 code snippets that demonstrate basic C++ array operations including: 1) Reading and printing arrays in reverse order 2) Adding two arrays element-wise and printing the sum array 3) Multiplying two arrays element-wise and printing the product array with commas 4) Summing the elements of a single array 5) Finding the smallest and largest elements of a single array 6) Searching an array for a given element and printing its position 7) Reading and printing a 2D array
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q:1

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5];
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=4;i>=0;i--)
cout<<a[i];
cout<<endl;
system("pause");
}

Q:2
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],b[5],sum[5];
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=0;i<5;i++)
cin>>b[i];
for(int i=0;i<5;i++)
sum[i]=a[i]+b[i];
for(int i=0;i<5;i++)
cout<<sum[i];
cout<<endl;
system("pause");
}

Q:3
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],b[5],prod[5];
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=0;i<5;i++)
cin>>b[i];
for(int i=0;i<5;i++)
prod[i]=a[i]*b[i];
for(int i=0;i<5;i++)
cout<<prod[i]<<",";
cout<<endl;
system("pause");
}

Q:4
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],b=0;
for(int i=0;i<5;i++)
{
cin>>a[i];
b+=a[i];
}
cout<<"Sum is: "<<b;
cout<<endl;
system("pause");
}

Q:5
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],smallest,largest;
cin>>a[0];
smallest=largest=a[0];
for(int i=1;i<5;i++)
{
cin>>a[i];
if(a[i]<smallest)
smallest=a[i];
if(a[i]>largest)
largest=a[i];
}
cout<<"Smallest: "<<smallest<<"\n"<<"Largest: "<<largest;
cout<<endl;
system("pause");
}
Q:6
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],n;
for(int i=0;i<5;i++)
cin>>a[i];
cout<<"Enter number to find its position: ";
cin>>n;
for(int i=0;i<5;i++)
if(n==a[i])
cout<<"The position is: "<<i;
cout<<endl;
system("pause");
}

Q:7
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[3][2];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
cin>>a[i][j];
}
cout<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
cout<<a[i][j];
}
cout<<endl;
system("pause");
}

You might also like