Explain your code with comments. Solve in C++.
Write a function named myFunc3() that takes a 2D integer array NUMBERS[][50], and it size n and m. Then the function will print the sum of each row in one line.
explanation is explained with in the comment in the code.
Code:
----------------------------------------------
#include<iostream>
using namespace std; //headers for input and output
void myFunc3(int numbers[][50],int n,int m) //function definition
with passed arguments
{
int s=0; //variable declaration
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
s=s+numbers[i][j]; //summation of row elements
}
cout<<i+1<<"th row sum:"<<s<<" ";
//displaying the summation of each row
s=0;
}
}
int main()
{
int n,m,numbers[50][50]; //variable declaration
cout<<"enter n,m values \n";
cin>>n>>m; //read the user input for array
dimensions
cout<<"enter array values:";
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>numbers[i][j]; //read array values
}
}
myFunc3(numbers,n,m); //function called from main()
}
---------------------------------
Explanation :
In the main function, the array values are read from the user and the function call myFunc3() is called by passing the array and array dimensions as arguments.
In the function call ,with the loop iteration add the values and read it to s variable.
Print the sum and make it 0 after printing because it doesn't affect the next row sum.
At last sum of all rows printed in a line.
--------------------------------
You can alter the comments and print or display statements as per requirements.
--------------------------------
Kindly comment for queries if any, upvote if you like it.
Thankyou.
Get Answers For Free
Most questions answered within 1 hours.