i am having trouble with writing this code. I am in a beginning C plus plus clas
ID: 3708313 • Letter: I
Question
i
am having trouble with writing this code. I am in a beginning C plus plus class. We are studying arrays and i am using Microsoft Visual Studios. with #include<iostream>,#include<iomanip>,#include<cmath>. Thank u
1. let array A-410,3,9,18,22,2,45,27, B--1,-2,-3C-100),200,300) Write a program to: a. Separate array A into two arrays: Array E contains the even values and array O contains the odd values. Display all arrays in a column format. Find the largest element in array A. Finds the sum of all the values of the elements in A. Join arrays A, B, and C into one array D b. c. d. A B C].Explanation / Answer
a)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int A[]={4,10,3,9,18,22,2,45,27},B[]={-1,-2,-3},C[]={100,200,300},E[10],O[10],i,j=0,k=0;
//split array A into even array E and odd Array A
int Asize=sizeof(A)/sizeof(A[0]); //get the size of array A
int Bsize=sizeof(B)/sizeof(B[0]);//get the size of array B
int Csize=sizeof(C)/sizeof(C[0]);//get the size of array C
for(int i=0;i<Asize;++i)
{
if(A[i]%2==0)
E[j++]=A[i];//store even numbers
else
O[k++]=A[i]; //store odd numbers
}
//display all the arrays
i=0;
cout<<"A"<<setw(7)<<" E"<<setw(7)<<" O"<<setw(7)<<" B"<<setw(7)<<" C"<<endl;
while(i<Asize)
{
cout<<setw(7)<<left<<A[i];
if(i<j)
cout<<setw(7)<<left<<E[i];
if(i<k)
cout<<setw(7)<<left<<O[i];
if(i<3)
{
cout<<setw(7)<<left<<B[i]<<setw(4)<<left<<C[i];
}
cout<<endl;
++i;
}
}
Output
A E O B C
4 4 3 -1 100
10 10 9 -2 200
3 18 45 -3 300
9 22 27
18 2
22
2
45
27
b)
#include<iostream>
using namespace std;
int main()
{
int A[]={4,10,3,9,18,22,2,45,27},i,max;
max=A[0];
int Asize=sizeof(A)/sizeof(A[0]); //get the size of array A
for(int i=0;i<Asize;++i)
{
if(max<A[i])
max=A[i];
}
cout<<"Largest element in A is "<<max<<endl;
}
Output
Largest element in A is 45
c)
#include<iostream>
using namespace std;
int main()
{
int A[]={4,10,3,9,18,22,2,45,27},i,sum=0;
int Asize=sizeof(A)/sizeof(A[0]); //get the size of array A
for(int i=0;i<Asize;++i)
{
sum+=A[i];
}
cout<<"Sum is "<<sum<<endl;
}
Output
Sum is 140
d)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int A[]={4,10,3,9,18,22,2,45,27},B[]={-1,-2,-3},C[]={100,200,300};
int Asize=sizeof(A)/sizeof(A[0]); //get the size of array A
int Bsize=sizeof(B)/sizeof(B[0]);//get the size of array B
int Csize=sizeof(C)/sizeof(C[0]);//get the size of array C
//int Dsize=Asize+Bsize+Csize;
int D[15];
int i,k=0;
for(i=0;i<Asize;++i)
D[k++]=A[i];
for(i=0;i<Bsize;++i)
D[k++]=B[i];
for(i=0;i<Csize;++i)
D[k++]=C[i];
cout<<"[";
for(i=0;i<k-1;i++)
cout<<D[i]<<",";
cout<<D[i]<<"]";
}
Output
[4,10,3,9,18,22,2,45,27,-1,-2,-3,100,200,300]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.