Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Dynamic 1-D array Create three dynamically allocated arrays (x, y, and z) of

ID: 3861826 • Letter: C

Question

c++ Dynamic 1-D array Create three dynamically allocated arrays (x, y, and z) of size N, where N is a number entered by the user. Populate x with even numbers and y with odd number. Add the respective element in x to the respective element in y, and store the result in the respective element in z using the following function, void sum_elements(int x[], int y[], int z[], int size). Static 2-D array Create a 3 x 3 array, and assign 1s to all columns in the first row, 2s to all columns in the second row, and 3s to all columns in the third row. Sum all the elements in the array, and return the sum as an integer using the following function, int sum_elements(int x[][3]). please explain each step/ why you wrote the code as such

Explanation / Answer

Q1

#include <iostream>

using namespace std;

void sum_elements(int x[], int y[], int z[], int size)
{
int i=0;
for(i=0;i<size;i++)
z[i] = x[i] + y[i];
  
  
cout<<"Final array is "<<endl;
for(i=0;i<size;i++)
cout<<z[i]<<" ";
}
int main()
{
int* a = NULL;
int* b = NULL;
int* c = NULL;
int size;
cout<<"Enter size of array "<<endl;
cin>>size;

a = new int[size];
b = new int[size];
c = new int[size];
int e=1,f=2,i=0;

for(i=0;i<size;i++)
{
a[i] = f;
f = f+2;
}

for(i=0;i<size;i++)
{
b[i] = e;
e = e+2;
}

sum_elements(a,b,c,size);
return 0;
}

Q2

#include <iostream>

using namespace std;

int sum_elements(int **x)
{
int sum=0,i=0,j=0;
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
sum = sum + x[i][j];
}
}
return sum;
}
int main()
{
int i=0,j=0;
int** ary = new int*[3];
for( i = 0; i <3; ++i)
ary[i] = new int[3];
  
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
ary[i][j] =i+1;
}
}

int sum = sum_elements(ary);
cout<<"Total sum is "<<sum<<endl;
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote