125k views
1 vote
P3 – Work with two dimensional arrays (80 points) 3. (a) Write the function rowSum() that receives a(double type) two-dimensional array using vectors and returns a vector consisting of the sum of elements in each row of the two-dimensional structure. The value of the returned vector at index zero is the sum of the elements in row zero of the two-dimensional structures. At index one of the returned vectors is the sum of the elements in row one and so on. [40pts] (b) Write a main function to test rowSum () function. Create a two-dimensional vector of at least size (5 x 5). Display the array and the content of the returned vector. [40pts]

User Feihtthief
by
6.3k points

1 Answer

4 votes

Answer:

// Here is code in c++.

#include <bits/stdc++.h>

using namespace std;

// part(a):

// function that return sum of each row as a vector

vector<double> row_Sum(vector<vector<double>> vect)

{

// find the size of 2-d vector

int m=vect.size();

// vector that hold the sum of each row

vector<double> r_sum;

for(int a=0;a<m;a++)

{

double tot=0;

// calculate sum of each row

for(int b=0;b<m;b++)

{

tot=tot+vect[a][b];

}

// push sum to the vector

r_sum.push_back(tot);

}

// return the vector which store the sum of each row

return r_sum;

}

//part(b):

// driver function

int main()

{

// create a 2-d vector

vector<vector<double>> two_d_vec;

int n=5;

//cout<<"Enter the size of array:";

//cin>>n;

cout<<"Enter the elements of 2-d array:"<<endl;

for(int c=0;c<n;c++)

{

// vector to read the elements of row

vector<double> row_vec;

for(int d=0;d<n;d++)

{

double x;

cin>>x;

// insert the data to each row

row_vec.push_back(x);

}

// push the row vector to 2-d vector

two_d_vec.push_back(row_vec);

}

// print the input of 2-d vector

cout<<"content of array is\\";

for(int a=0;a<n;a++){

for(int b=0;b<n;b++){

cout<<two_d_vec[a][b]<<" ";

}

cout<<"\\";

}

cout<<"Row sum:\\";

// call the function to get sum of each row as a vector

// pass the 2-d input vector into function

vector<double> sum_of_row=row_Sum(two_d_vec);

// print the sum of each row

for(int i=0;i<sum_of_row.size();i++)

cout<<sum_of_row[i]<<" ";

}

Step-by-step explanation:

two-dimensional vector is basically vector of vectors.In the main create a 2-dimensional vector.Then in the first for loop, create a one- dimensional vector to read the elements of each row.Read the elements of each row in a vector then push that 1-d vector into 2-d vector.This will continue for 5 times as there is size of 2-d array is 5x5.Call the function "row_Sum()" with 2-d array as parameter.It will calculate the sum of each row and push it into a 1-d vector.Then it will return the vector which contains the sum of each row of 2-d array.

Output:

Enter the elements of 2-d array:

1 2 3.4 7 5.5

4 6 8 1.1 9

3.5 8 9 11 13

1.4 7 3 8.8 10

4.1 7 8 9 11

content of array is

1 2 3.4 7 5.5

4 6 8 1.1 9

3.5 8 9 11 13

1.4 7 3 8.8 10

4.1 7 8 9 11

Row sum:

18.9 28.1 44.5 30.2 39.1

User James Ching
by
5.6k points