Question

In: Computer Science

(a) Write the function rowSum() that receives a(double type) two-dimensional array using vectors and returns a...

(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 vector is the sum of the elements in row one and so on. (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.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;

vector<double> row_Sum(vector<vector<double>> vect)
{
//get the size of the vector
int s=vect.size();
//declare a vector
vector<double> rsum;
for(int i=0;i<s;i++)
{
double sum=0;
//calculate sum of each row
for(int j=0;j<s;j++)
{
sum=sum+vect[i][j];
}
//add sum to vector
rsum.push_back(sum);
}
//return the vector which has sum to the rows
return rsum;
}

int main()
{
//declare a 2-D vector
vector<vector<double>> vec;
int n=5;//gave default size as 5

/*//use below if you need other n value
cout<<"Enter the size of array:";
cin>>n;*/

cout<<"Enter the elements of 2-D vector:"<<endl;
for(int i=0;i<n;i++)
{
vector<double> vecrow;
for(int j=0;j<n;j++)
{
double x;
cin>>x;
//insert the data to each row
vecrow.push_back(x);
}
// push the row vector to 2-d vector
vec.push_back(vecrow);
}
// print the input of 2-d vector
cout<<"The vector contains:\n";
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<vec[i][j]<<" ";
}
cout<<"\n";
}
cout<<"Sum of the rows:\n";
// call the function to get sum of each row as a vector
// pass the 2-d input vector into function
vector<double> res_vec=row_Sum(vec);
//print the sum of each row using vector
for(int i=0;i<res_vec.size();i++)
cout<<res_vec[i]<<" ";
}

comment if any doubts


Related Solutions

Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Create a function to output a one dimensional double array Mwith n elements where the...
Create a function to output a one dimensional double array M with n elements where the first three elements are 1 and each subsequent element is the sum of previous three elements before it. Name the function myArray. Write the function in the correct format to be used to create a Matlab function. Call the function in correct format to output the array with 7 elements.
Write program that pass unsorted one dimensional array and a key to function, the function will...
Write program that pass unsorted one dimensional array and a key to function, the function will search for the key(using linear search) and if it is found the function will sort the array from the beginning to the position of the key, and f it is not found the function will sort all the array Note: please solve it by c , not c++
1) Write a function that receives a pointer to an array along with the size of...
1) Write a function that receives a pointer to an array along with the size of the array, it returns the largest number in the array. 2) Write a function that receives a string and returns the length of it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT