13 Replies - 2968 Views - Last Post: 22 April 2011 - 10:32 AM Rate Topic: -----

#1 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 12:50 PM

I got the array to Initialize and to sum the whole array, I thought I could use the same loop I used to sum the array to sum columns and rows by entering their index numbers this isn't working the code compiles and runs but the answers are way off
#include <iostream>
#include<iomanip>
using namespace std;

const int COLS =4;
const int ROWS =3;

void showArray(int[][COLS],int);

	int main ()
{
	int table[ROWS][COLS] = {{23,27,56,12},{34,89,41,39},{29,11,99,52}};
	int total =0;int numbers;
	int row = 0;int count;
	int element;int col = 0;
		
	cout << "The array is \n";
		
	showArray (table,ROWS);	

	total = 0;
	for (int row = 0; row < ROWS; row++)
	{
	for(int col =0; col < COLS;col++)
				total += table [1][2];
		}

		cout << " The element in row 2 column 3 is   " << total << endl;
	
	total =0;
	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLS; col++)
			total +=table [row][col];
	}
	cout << "The sum total is  " << total << endl;


	total= 0;
		for(int row  =0;row<ROWS;row++)
		{
			for(int col =0; col < COLS;col++)
				total += table [1][0];
		}

		cout << " The sum of row 2 is   " << total << endl;

		total = 0;
		for(int row = 0; row < ROWS; row ++)
		{
			for(int col =0; col < COLS;col++)
				total += table [0][3];
		}

		cout << " The sum of column 3 is   " << total << endl;


	return 0;
	}

void showArray (int array[] [COLS],int rows)
{
	for (int x =0; x <rows; x++)
	{
		cout << " myArray[]";
		for (int y =0; y < COLS; y++)
		{
			
			cout <<   setw(4)  <<  array [x][y] << " ";
		}
		cout << endl;

	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ given a 3by 4 array you are asked to sum rows and columns, etc

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:03 PM

The sum of a particular column, say column 0, is obtained by summing table[row][0] while iterating through all the rows.

If you want to sum all the columns "at once", you need a 1 x COLS array, and while you iterate through the entire array add the value of each element table[row][col] to sums[col].
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:03 PM

explain what is actually happening as apposed to what you expect to happen. show us the code that you expect to total the columns and tell us what output your getting rather than the desired output.
Was This Post Helpful? 0
  • +
  • -

#4 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:05 PM

What you really want, to sum all values together or to add rows alone and column alone or do what matrix does? (adding a corresponding points)?
Was This Post Helpful? 0
  • +
  • -

#5 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:39 PM

I have to sum row 2 and sum column 3 seperately 2 answers when I try to sum row 2 I get 27, when I try to sum the column 3 I get number 12 hereh are my loops
total= 0;
		for(int row  =0;row<ROWS;row++)
		{
			for(int col =0; col < COLS;col++)
				total += table [row][1];
		}

		cout << " The sum of row 2 is   " << table[row][1] << endl;

		total = 0;
		for(int row = 0; row < ROWS; row ++)
		{
			for(int col =0; col < COLS;col++)
				total += table [col][3];
		}

		cout << " The sum of column 3 is   " << table[col][3] << endl;

Was This Post Helpful? 0
  • +
  • -

#6 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:46 PM

If you are summing only row or column, then you need only one for: That is, one index will not be changing.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:49 PM

Let's just look at this one:
total= 0;
for(int row  =0;row<ROWS;row++)
{
    for(int col =0; col < COLS;col++) {
        total += table [row][1];
    }
}
cout << " The sum of row 2 is   " << table[row][1] << endl;


What is that doing? The outer loop is iterating through ALL of the rows, and within that loop, for every row (i.e., three times since there are three rows), it iterates through all of the columns.

What happens when row == 0 (the first iteration of the outer loop)?
Since there are 4 columns, it adds the value in table[0][1] to total four times!
Then, when row == 1, it adds the value in table[1][1] to total four times.
Then, when row == 2, it adds the value in table[2][1] to total four times.

Is that what you wanted?
Was This Post Helpful? 1
  • +
  • -

#8 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 01:58 PM

View Postr.stiltskin, on 21 April 2011 - 01:49 PM, said:

Let's just look at this one:
total= 0;
for(int row  =0;row<ROWS;row++)
{
    for(int col =0; col < COLS;col++) {
        total += table [row][1];
    }
}
cout << " The sum of row 2 is   " << table[row][1] << endl;


What is that doing? The outer loop is iterating through ALL of the rows, and within that loop, for every row (i.e., three times since there are three rows), it iterates through all of the columns.

What happens when row == 0 (the first iteration of the outer loop)?
Since there are 4 columns, it adds the value in table[0][1] to total four times!
Then, when row == 1, it adds the value in table[1][1] to total four times.
Then, when row == 2, it adds the value in table[2][1] to total four times.

Is that what you wanted?



So whatyou are saying is I don't need the outher loop just the inner since I only want the sum of a single row or column
Was This Post Helpful? 0
  • +
  • -

#9 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 02:06 PM

I removed the outer loop so it wouldn't addd every thing 4 times but it still just showing the first number in the column



for(int col =0; col < COLS;col++)
				total += table [col][2];
		

		cout << " The sum of column 3 is   " << table[col][2] << endl;

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 02:50 PM

If you want to sum a column, you have to loop through all of the rows, adding the amount that is in that particular column on each row.

Since you want a particular column, that column number is a constant. The row is the variable that will take on different values.

And remember that your array is table[ROWS][COLUMNS]: the first dimension is always going to be the row number, the second dimension is the column number.
Was This Post Helpful? 1
  • +
  • -

#11 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 03:50 PM

Thank you I learned alotabout array and how they work.

just a question in my output each line of the array starts like this:
myArray[] 22 11 34 55
myArray[] 55 99 65 19

how would you get the myArray[] to output myArray[0],myArray[1],myArray[2] instead of myArray[]

would this be done in the show array function

Thanks again
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 03:57 PM

MyArray[0] is the first row of the 2-dimensional array, so in your showArray loop that prints the array, as soon as you enter the outer loop
for( int row == ...
you want to print "MyArray[row]"
and immediately after that comes the inner loop in which you will print the value for each column of that row.
Was This Post Helpful? 0
  • +
  • -

#13 tomdog51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 30-November 10

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 21 April 2011 - 11:57 PM

What does int row equel...for( int row == my codewas
for (int x =0; x <rows; x++)

Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: C++ given a 3by 4 array you are asked to sum rows and columns, etc

Posted 22 April 2011 - 10:32 AM

Obviously I meant =.

    for(int row = 0; row < ROWS; row++) {
        cout << "MyArray[" << row << "]: ";
        for(int col = 0; col < COLS; col++) {
            // and so on ...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1