.ll Tele2 3G 11:16 Cloud 1 Write the program in c++ and show a example of output
ID: 3601758 • Letter: #
Question
.ll Tele2 3G 11:16 Cloud 1 Write the program in c++ and show a example of output data 1.given a arrAy a of integers of dimensions n. If onl arey contasa ond odd elements ,get(a1+an) (a2+an-1. 2.given an array of integers if dimension n. All elements of an array with array numbers preceding the first maximum element are multiplied by the maximum 3.given an array of dimension n. Insert after each zero element an element in the middle(or the sum of the middle elements for even n). In the resulting array ,delete the first negative and second positive elements.Do not use an additional array 4.given a matrix of dimension n*n Get a one-dimensional array of the descending diagonals of the matrixExplanation / Answer
Dear student,
Please find solution below in c++.
If having any problem in understanding the codes ,feel free to contact
Answer to first question
#include <iostream>
#include<stdio.h>
using namespace std;
int main() {
// your code goes here
int n; // length of array
cin>>n;
int i;
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int countEven=0;
int countOdd=0;
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
countEven++;
else
countOdd++;
}
int k=n-1;
i=0;
long long mlsum=1;
if(countEven==countOdd)
{
while(i<k)
{
int temp=arr[i]+arr[k];
i++;
k--;
// cout<<temp<<" ";
mlsum=mlsum*temp;
}
cout<<mlsum;
}
else
cout<<"Even count is not equal to odd count ";
return 0;
}
Answer to second question.
#include <iostream>
#include<stdio.h>
#include<limits.h>
using namespace std;
int main() {
// your code goes here
int n; // length of array
cin>>n;
int i;
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int max=INT_MIN;
for(i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
}
for(i=0;i<n;i++)
{
if(arr[i]==max)
break;
}
long long mul=1;
if(i==n-1)
{
cout<<"No preceding element found ";
}
else
{
for(int j=i+1;j<n;j++)
{
mul=mul*arr[j];
}
cout<<mul;
}
return 0;
}
Answer to third question
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<limits.h>
using namespace std;
int main() {
// your code goes here
int n; // length of array
cin>>n;
int i;
int arr[10000]; //declare array of larger size as you have to insert element
memset(arr,0,sizeof(arr));
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int mid=n/2;
int mide;
if(n%2==0)
mide=arr[mid-1]+arr[mid];
else
mide=arr[mid];
for(i=0;i<n;i++)
{
if(arr[i]==0)
{
for(int j=i+1;j<n;j++)
{
arr[j+1]=arr[j];
}
arr[i+1]=mide;
n=n+1;
}
}
for(i=0;i<n;i++)
{
if(arr[i]<0)
{
for(int j=i+1;j<n;j++)
{
arr[j-1]=arr[j];
}
n=n-1;
break;
}
}
int cnt=0;
for(i=0;i<n;i++)
{
if(arr[i]>0)
{
cnt++;
if(cnt==2)
{
for(int j=i+1;j<n;j++)
{
arr[j-1]=arr[j];
}
n=n-1;
break;
}
}
}
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Answer to fourth question
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n; //size of matrix;
int i,j;
cin>>n;
int mat[n][n];
int diag[n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>mat[i][j];
}
}
for(i=0;i<n;i++)
{
diag[i]=mat[i][i];
}
for(i=0;i<n;i++)
{
cout<<diag[i]<<" ";
}
return 0;
}
Thanks !!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.