So heres a portion of my program... the program has several functions (each doing diff. things)


Im having a problem I can not find the way to write the sum of the third row in costOfSports array.

the book says something like this:
for(col=0;col<5;col++)
{
for(row=0;row<3;row++)
{
row_sum[row]=array[row][col];
}
}

//but it is not working... it does not generate bugs but it gives me an "address" instead of a number...
what can I do? any hint? trick? code? thanks to everyone :)

#include<iostream>

using namespace std;
int main()
{
    const int NUM_SCHOOLS=5;
    const int NUM_SPORTS=3;
    enum SportType {FOOTBALL,BASKET,VOLLEYBALL};

	int kidsInSports[NUM_SCHOOLS][NUM_SPORTS]={{5,7,10},
  {2,5,1},				     {20,37,3},
					    {40,2,5},
					    {10,8,5}};				    {40,2,5},
											    {10,8,5}};
                                              
	int costOfSports[NUM_SPORTS][NUM_SCHOOLS]={{25,30,45,50,55},
					    {5,50,10,25,8},
         					{13,70,25,47,5}};

	int row,col;
	int col_sum[5]={0},row_sum[3]={0};	
	double sum;
	

cout<<endl;

//THIS IS THE part im having problems with...
	cout<<"the sum is"<<' ';
	for(row=0;row<3;row++)
	{
			for(col=0;col<5;col++)
		{	
			
		row_sum[3]+=costOfSports[row][col];
		}

  system("PAUSE");
  return 0;
}
	}

Recommended Answers

All 6 Replies

So heres a portion of my program... the program has several functions (each doing diff. things)


Im having a problem I can not find the way to write the sum of the third row in costOfSports array.

the book says something like this:
for(col=0;col<5;col++)
{
for(row=0;row<3;row++) 
{
row_sum[row]=array[row][col];
}
}

//but it is not working... it does not generate bugs but it gives me an "address" instead of a number...
what can I do? any hint? trick? code? thanks to everyone

#include<iostream> 
using namespace std;
int main()
{    
const int NUM_SCHOOLS=5; 
   const int NUM_SPORTS=3;
    enum SportType {FOOTBALL,BASKET,VOLLEYBALL}; 
	int kidsInSports[NUM_SCHOOLS][NUM_SPORTS]={{5,7,10},  {2,5,1},				     {20,37,3},					    {40,2,5},					    {10,8,5}};				    {40,2,5},											    {10,8,5}}; 
	int costOfSports[NUM_SPORTS][NUM_SCHOOLS]={{25,30,45,50,55},					    {5,50,10,25,8},         					{13,70,25,47,5}};
 	int row,col;
	int col_sum[5]={0},row_sum[3]={0};	
	double sum; 
 cout<<endl; //THIS IS THE part im having problems with...	
cout<<"the sum is"<<' ';
	for(row=0;row<3;row++)
	{		
	for(col=0;col<5;col++)	
	{	 	
	row_sum[3]+=costOfSports[row][col];
		}   
system("PAUSE"); 
 return 0;
}

<Delete>
Sorry I was looking at the dimensions of the wrong array.

Discussion seems to be continuing at the OP's other thread on this.

//THIS IS THE part im having problems with...
	cout<<"the sum is"<<' ';
	for(row=0;row<3;row++){
for(col=0;col<5;col++)
{
[B]row_sum[3]+=costOfSports[row][col];[/B]	
} 
  system("PAUSE"); 
 return 0;
}

row_sum[3] is an array of 3 elements, starting with row_sum[0].
You were trying to access the 3rd row which should be row_sum[2], but you called row_sum[3], which is an imaginary 4th element. That's the reason for the adress. Change it to row_sum[2] and it should work properly.

it is adding all the values but it does it like this [0][0]+[0][1]....+[2][4]= 1000s ... it should add only values from [2][0]+[2][1]+[2][2][2][3]+[2][4]= which is 160 :/

gosh :(

got it... :D I had to "initialize" row=2 :P thanks tho I couldnt figure out what was wrong... this book kinda sucks... it has only one dimentional arrays :/ so everything above (multidimentional) is up to our own research? lol.. collllleeggeee zzzz :(

Hi there,

I just caught this question as I came on to ask one of my own. It seems like you and I are programming at about the same level so I grabbed it and gave it a go. So, even though this answer may be a little late for you, it looks to me like you were trying to achieve something like this?:

#include<iostream>
using namespace std;
int main()
{
    const int SPORTS=3;
    const int SCHOOLS=5;
    char *sportsType[3] = {"Football", "Basketball", "Volleyball"};
    //                  ischool  00 01 02 03 04
    int kids[SPORTS][SCHOOLS]={ {05,02,20,40,10}, // isport: 0
                                {07,05,37,02,07}, // isport: 1
                                {10,01,03,05,05}};// isport: 2
    int costs[SPORTS][SCHOOLS]={{25,30,45,50,55},
                                {05,50,10,25,07},
                                {13,70,25,47,05}};
    int total_cost = 0;
    int cost_each_sport[SPORTS] = {0}, expense_each_school[SCHOOLS] = {0};
    for(int isport = 0; isport < 3; isport++)
    {
        for(int ischool = 0; ischool < 5; ischool++)
        {
            cost_each_sport[isport] += costs[isport][ischool];
            expense_each_school[ischool] += costs[isport][ischool];
            total_cost += costs[isport][ischool];
        }
    }
    // Calculations done, now check the results:
    for(int x = 0; x < SPORTS; x++)
    {
        cout << "Cost of " << sportsType[x] << " = " << cost_each_sport[x] << endl;
    }
    cout << endl;
    for(int x = 0; x < SCHOOLS; x++)
    {
        cout << "Allowance of School: " << x << " = " << expense_each_school[x] << endl;
    }
    cout << endl;
    cout << "Total cost = " << total_cost << endl;
    cout << endl;

    return 0;
}

Crits from forum members, both positive and negative, would be appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.