Need help on 2D arrays

Good day to all,

I'm quite a beginner to C++ and require some help on my codings. Hope you guys can help me out on this. The following will be my code. I'm actually doing a 2D array and everything seems to fine. However on the final part, I would like to obtain the total value of the monthlydonation for the 3 months as well as the dailydonation so the code i was thinking is something like total=monthlydonation[]+dailydonation[][]. However I got no idea how to code it. I really appreciate if some kind souls out there can help me out on this.




#include <iostream>
#include <string>
using namespace std;


int main() {
const int ROW=5; //number of days
const int COL=3; //number of months

double day[ROW][COL];
double month[COL];

string months[3];
months[0]="January";
months[1]="February";
months[2]="March";


for(int col=0;col<COL;col++){
double totalMon =0;
for(int row=0;row<ROW;row++){

cout<<"Enter amount of sales";
cout<<" for "<<months[col];
cout<<" day "<<(row+1)<<": $";
cin>>day[row][col];
totalMon += day[row][col];

}
month[col]=totalMon;
}
cout<<endl;

//write code to print
for(int col=0;col<COL;col++){
for(int row=0;row<ROW;row++){
if(day[row][col]>1500){
double dailydonation = day[row][col]*0.05;
cout<<months[col]<<" Day "<<(row+1)<<" plegded daily donation: $"<<dailydonation<<endl;

}else{
double dailydonation = 0;
cout<<months[col]<<" Day "<<(row+1)<<" plegded daily donation: $"<<dailydonation<<endl;
}

}

cout<<endl;

}


for (int i=0;i<COL;i++){
cout<<"First 5 day total sales";
cout<<" for the month "<<months[i]<<": $"<<month[i]<<endl;

if (month[i]>12000){
double monthlydonation = month[i]*0.05;
cout<<months[i]<<" pledged monthly donation : $"<<monthlydonation<<endl;
}else{
double monthlydonation = 0;
cout<<months[i]<<" pledged monthly donation : $"<<monthlydonation<<endl;
}
cout<<endl;


}

cin.ignore(2);
}
Last edited on
closed account (N36fSL3A)
http://www.cplusplus.com/forum/articles/17108/
=\ I don't get it from that article. anyone can provide me more helps??
closed account (N36fSL3A)
The article is supposed to tell you multidimensional arrays are evil, hence the name.

Figure out a alternate way to solve the problem, unless this is a homework problem.
Hi Lumpkin,
yes this is 1 of my assignment and i'm at 3/4 done just that I need to show the final value of the total sum of monthlydonation and total dailydonation. I'm require to use multidimensional arrays so I wont be able to use any alternative.
Correct me if I'm wrong:
You want to calculate the total sum of monthly and daily donations?

Well, firstly your monthlydonation variable is scoped inside your if statements. That means you won't be able to use it outside for finding the total sum.

The simplest way is to sum up everything by iterating through all the values.

1
2
3
4
5
6
7
8
9
double total_donation(0.0);

for(size_t y(0); y < COL; ++y){
   total_donation += months[y];

   for(size_t x(0); x < ROW; ++x){
      total_donation += day[y][x];
   }
}


Simple as that.

For future reference, (as others have mentioned) multi-dimensional arrays can easily become hard to work with. Try using a 1D array and treat it as a multidimensional one.
For instance:
1
2
3
4
const size_t COLS(100), ROWS(100);
int int_arr[COLS * ROWS];
for(size_t I(0); I < COLS * ROWS; ++I)
   int_arr[I] = I*10;
Hi Daleth, thanks for the help. I have try playing around with your code but i'm still kinda stuck. What I would like to have is the total daily donation for the 5 days of the month individually. As of now your code is just incrementing every day.

And yes, I saw you commenting this,

"Well, firstly your monthlydonation variable is scoped inside your if statements. That means you won't be able to use it outside for finding the total sum."

Can you tell me is there any other way to make this variable outside of the if statement. As you can see my monthlydonation will execute depends on the condition if total of the month exceed 12000. So I don't really know how to resolve it =\ Same goes for my dailydonation.


This is actually the question.

ABC is a newly established confectionary shop in Seng Tao Road. To improve its image in the area of social responsibility, the boss would like to pledge donation to the Children Cancer Society based on sales performance. He would like to track the sales of the first 5 days of each of the month of January, February and March. He has pledged to donate 5% of the daily sales if a day’s sales volume exceeds $1,500.
In addition, he has pledged another 5% if the total of a month’s 5-day total sales exceeds $12,000.

Demonstrate your program with the following test data which should produce a total donation. Format your output with 2 decimal places. The total donation consists of $3,030 from the daily sales and $2,610 from the monthly sales.


Please don't mind on the total donation value from daily sales and monthly sales as it solely depends on the user that key in the value.
Last edited on
What I would like to have is the total daily donation for the 5 days of the month individually.

So, you are trying to find the daily donation sum for each month?
You would do almost the same thing as before, except store the results in an array.
1
2
3
4
5
6
7
double monthly_donation_sums[COLS] = {0};
   //Not sure if I initialized the array correctly, so be sure to check
for(size_t y(0); y < COLS; ++y){
   for(size_t x(0); x < ROW; ++x){
      monthly_donation_sums[y] += day[y][x];
   }
}


Can you tell me is there any other way to make this variable outside of the if statement.

Simply declare the variable outside of the scope (before the if statement).
So right now you have:
1
2
3
4
5
6
7
if(/*...*/){
 double monthlydonation = 0;
//...
}else{
 double monthlydonation = 0;
//...
 }

You would just change it to:
1
2
3
4
5
6
double monthlydonation = 0;
if(/*...*/){
//...
}else{
//...
}

Now you would be able to use monthlydonation outside of the if statement.
Topic archived. No new replies allowed.