Problem with Matrix

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Problem with Matrix

Postby nickd1085 » Thu Apr 21, 2011 3:34 pm

Hey guys,

Im having issues with my class assigment. I have a mac and not a PC so I can only compile when Im at school.

This is the assignment:

Write a test program that reads a 4-by-4 matrix and displays the sum of all its elements. Heres a sample run:

Heres the code I have but its not doing what I want it to do.

Code: Select all
#include <iostream>
using namespace std;

const int COLUMN_SIZE = 4;

int sum(const int a[] [COLUMN_SIZE], int rowSize)
{
    int total = 0;
    for (int row = 0; row < rowSize; row++)
    {
        for (int column = 0; column < COLUMN_SIZE; column++)
        {
            total += a[row][column];
        }
    }

    return total;
}
int main()
{
    int m[4][4]=
    {
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,16}
    };
    cout<< "Sum of the matrix"<< sum(m,4) << endl;
    return 0;
}


Its only displaying the matrix and not asking what to input. Assingment is late so any help would be great! Thanks man!

Nick
nickd1085
 
Posts: 4
Joined: Tue Apr 12, 2011 5:38 am

Re: Problem with Matrix

Postby ventsyv » Fri Apr 22, 2011 10:33 am

Your total function looks fine. To get the input, just do a double loop and ask the user to enter a number for each cell. It's very similar to the total function.
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests