Array Daily Challenge
Array Daily Challenge
PSCPP_DailyChallenge_Arrays
Attempt : 1
Total Mark : 30
Marks Obtained : 0
Section 1 : Coding
1. Count the number of pairs in an integer array whose sum equals the
given sum (all elements are unique).
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int sum,n,j,i,num=0;
int arr[n];
cin>>sum>>n;
for(i=0;i<n;i++)
{
cin>>arr[n];
}
for(j=0;j<n/2;j++)
{
if(arr[i]+arr[j]==sum)
{
num=num+1;
}
}
cout<<num;
}
2. Problem Statement
Given an array of elements , reverse the array
Array : 1,2,3,4,5
The output array should be : 5,4,3,2,1
Note: Get the Input Dynamically, Size of the array will Not be Provided.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
int arr[5];
for(i=0;i<5;i++)
{
cin>>arr[i];
}
for(i=4;i>=0;i--)
{
cout<<arr[i]<<" ";
}
}
3. Problem statement
Manju wants to perform certain operations in an array to clear the coding
round. Write a program to obtain a matrix and perform the following
operations:
Sum of diagonal elements of a matrix.
Sum of row elements of a two-dimensional array
Sum of column elements of a two-dimensional array.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,m,i,j,diagsum=0,row1=0,row2=0,row3=0;
int a[i][j];
cin>>n>>m;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
{
diagsum=diagsum+a[i][j];
}
else
{
diagsum=diagsum+0;
}
}
}
cout<<"Diagsum= "<<diagsum<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==0)
{
row1=row1+a[i][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==1)
{
row2=row2+a[i][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==2)
{
row3=row3+a[i][j];
}
}
}
// cout<<"Diagonal Elements : "<<diagsum<<endl;
cout<<"Row 1 : "<<row1<<endl;
cout<<"Row 2 : "<<row2<<endl;
cout<<"Row 3 : "<<row3<<endl;
}