Question

Exercise #1: Develop a program that prints out the sum of each column of a two-dimensional array The program defines method s

Sample run 1: Enter a value: 9 Enter a value: 1 Enter a value: 2 Enter a value: 4 Enter a value: 3 The entered matrix: CON O

I need this code in C++. Should be able to read from a file as stated in the directions

0 0
Add a comment Improve this question Transcribed image text
Answer #1

i have written code in c++ in codeblocks,its working fine in my ide.ihave attached the output also.if you have any doubt feel free to ask.

CODE 1:taking input from input,txt file.

first make a input,txt file where you are saving your source code.

code:

#include <iostream>

using namespace std;
int* sumColumns(int[3][4]);//prototype of sumColumns() function
int main()
{
FILE *in;
int i=9,j=0;
int a[3][4];
in = fopen("input.txt", "r");//opening input.txt to take input
   if (in == NULL) {
       printf("Cannot open source file.\n");
       exit(1);
   }
   for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
fscanf(in,"%d ",&a[i][j]);//storing values from input.txt int array

}
cout<<"The entered matrix:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
cout<<a[i][j]<<" ";//printing array
cout<<endl;
}
int *sumcolumn=sumColumns(a); //returned array
for(j=0;j<4;j++)
{
cout<<"Sum of column"<<" "<<j<<" "<<sumcolumn[j]<<endl;//printing sum of column
}

fclose(in);

return 0;
}

int* sumColumns(int a[3][4])
{
int i=0,j=0;
int * sumcolumn=new int[4];
for(j=0;j<4;j++)
{
sumcolumn[j]=0;
}

for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
sumcolumn[j]+=a[i][j];

}

return sumcolumn;

}


input:

Help input - Notepad File Edit Format View 9 1 24 2 280 3 3 3 3

output:

The entered matrix: 9 1 24 2 280 3 3 3 3 Sum of column 14 Sum of column 1 6 Sum of column 2 13 Sum of column 3 7 execution ti

CODE2: taking input from user

code:

#include <iostream>

using namespace std;
int* sumColumns(int[3][4]);
int main()
{

int i=0,j=0;
int a[3][4];

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{cout<<"Enter a value:";//taking input
cin>>a[i][j];}
}

cout<<"The entered matrix:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
cout<<a[i][j]<<" ";//printing array
cout<<endl;
}
int *sumcolumn=sumColumns(a); //returned array
for(j=0;j<4;j++)
{
cout<<"Sum of column"<<" "<<j<<" "<<sumcolumn[j]<<endl;//printing sum of column
}

return 0;
}

int* sumColumns(int a[3][4])
{
int i=0,j=0;
int * sumcolumn=new int[4];
for(j=0;j<4;j++)
{
sumcolumn[j]=0;
}

for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
sumcolumn[j]+=a[i][j];

}

return sumcolumn;

}


input/output:

Enter a value:9 Enter a value:1 Enter a value: 2 Enter a value:4 Enter a value: 2 Enter a value: 2 Enter a value: 8 Enter a v

Add a comment
Know the answer?
Add Answer to:
I need this code in C++. Should be able to read from a file as stated...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location...

    SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location of the first largest value in a two-dimensional array. The largest values may appear more than once in the array. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints out...

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT