sum of row and column

Hi there , i have question about 2d array ,
the question is a summation of each row and column with specific output form , the question exactly is :

Write a program for a mini-market.
Your program should save

how many pieces of each product were sold during the week

and print the result as a table. It should include:

a- How many products sold in each day.

b- How many pieces sold during each week for each product

c- The bestseller product.

i been stuck all night and morning
HELP PLZ.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{int i,j;
	char days[7][5]={"mon","tue","thur","wed","fri","sat","sun"};
	char item[5][8]={"milk","sugar","juice","rice","coffee"};
	int sold[7][5];

	for(i=0;i<7;i++)
	
		{
			cout<<"Please enter the quantity sold for each item in "<<days[i]<<": ";
			
		
			cin>>sold[i][0]>>sold[i][1]>>sold[i][2]>>sold[i][3]>>sold[i][4];
	}
	 cout<<"\t";

   for(int i=0;i<7;i++)
        cout<<days[i]<<"\t";


   cout<<"sum"<<endl;
    
   
   int maxSale=0;
    char product[50];
   
    strcpy(product,item[0]);
	//print the row sum for each day
	 for( j=0;j<5;j++)
    { 
		cout<<item[j]<<"\t";

        for( i=0;i<7;i++)
            cout<<sold[i][j]<<"\t";

       int rowsum=0;
		 rowsum+=sold[0][i];
        cout<<rowsum<<"\n";
		
	 
      if(rowsum>maxSale)
        {
            maxSale=rowsum;
            strcpy(product,item[i]);
        }

       cout<<endl;
    }

   cout<<endl;
    cout<<"sum\t";
	 //print the column sum at each day
    for(i=0;i<7;i++)
    {
        //call getColumnSum at each day
       int sum=0;

   int item;
    for(j=5;j<5;j++)
        sum+=sold[i][j];
cout<<sum<<"\t";
    }
    cout<<endl;
    //product of maximum sale
    cout<<"The best seller product is "<<product<<endl;
    //maximum sale
    cout<<"Qunatity sold "<<maxSale<<endl;





	
	getch();
	return 0;
}
Last edited on
ANYONE :(
the question exactly is :

Don't see a question there.
Roody if you need the assignment I have it completed. I can't just give you the source.

Enter the amount of milk sold on monday: 10
Enter the amount of sugar sold on monday: 20
Enter the amount of juice sold on monday: 90
Enter the amount of rice sold on monday: 30
Enter the amount of coffee sold on monday: 30
Total items sold for the day: 180

Enter the amount of milk sold on tuesday: 10
Enter the amount of sugar sold on tuesday: 20
Enter the amount of juice sold on tuesday: 40
Enter the amount of rice sold on tuesday: 50
Enter the amount of coffee sold on tuesday: 20
Total items sold for the day: 140

Enter the amount of milk sold on wednesday: 10
Enter the amount of sugar sold on wednesday: 20
Enter the amount of juice sold on wednesday: 40
Enter the amount of rice sold on wednesday: 50
Enter the amount of coffee sold on wednesday: 20
Total items sold for the day: 140

Enter the amount of milk sold on thursday: 40
Enter the amount of sugar sold on thursday: 30
Enter the amount of juice sold on thursday: 20
Enter the amount of rice sold on thursday: 40
Enter the amount of coffee sold on thursday: 30
Total items sold for the day: 160

Enter the amount of milk sold on friday: 20
Enter the amount of sugar sold on friday: 10
Enter the amount of juice sold on friday: 20
Enter the amount of rice sold on friday: 10
Enter the amount of coffee sold on friday: 55
Total items sold for the day: 115

Enter the amount of milk sold on saturday: 11
Enter the amount of sugar sold on saturday: 22
Enter the amount of juice sold on saturday: 12
Enter the amount of rice sold on saturday: 5
Enter the amount of coffee sold on saturday: 1
Total items sold for the day: 51

Enter the amount of milk sold on sunday: 2
Enter the amount of sugar sold on sunday: 3
Enter the amount of juice sold on sunday: 4
Enter the amount of rice sold on sunday: 5
Enter the amount of coffee sold on sunday: 6
Total items sold for the day: 20

Mini-market items sold:
milk sold: 103
sugar sold: 125
juice sold: 226
rice sold: 190
coffee sold: 162

The most sold item in the week was: juice(226 sold)
Total items sold in the week: 806
Press any key to continue . . .
closed account (48T7M4Gy)
A small start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	string day[] = { "Mon","Tue","Wed","Thu","Fri","Sat","Sun" };
	int noDays = sizeof(day) / sizeof(string);

	string item[] = { "milk","sugar" };
	int noItems = sizeof(item) / sizeof(string);

	//Daily sales matrix
	int** sold = new int*[noItems]; // rows
	for (int i = 0; i < noDays; ++i)
		sold[i] = new int[noDays]; // columns


	//Input sales
	for (int dayNo = 0; dayNo < noDays; dayNo++)
	{
		cout << "Quantities sold on " << day[dayNo] << endl;
		for (int productNo = 0; productNo < noItems; productNo++)
		{
			cout << "Item: " << item[productNo] << " ";
			cin >> sold[dayNo][productNo];
		}
	}

	// Day heading
	for (int dayNo = 0; dayNo < noDays; dayNo++)
		cout << day[dayNo] << '\t';
	cout << endl;

	//Display sales figures
	for (int productNo = 0; productNo < noItems; productNo++)
	{
		for (int dayNo = 0; dayNo < noDays; dayNo++)
			cout << sold[dayNo][productNo] << '\t';
		cout << endl;
	}
	
	return 0;
}
kemort, your source will produce a run-time error.
closed account (48T7M4Gy)
Where is the runtime error? It tested ok on vs2015. If you think you know where an error is you should point it out.
closed account (48T7M4Gy)
Oh, and it tested ok on the shell. Of course there are no delete's but that,s not a runtime error just sloppiness but demos are always like that sometimes. ;)
Even on shell it stops on friday after item: milk
The code has undefined behavior because the array bounds of sold are exceeded.
This:
1
2
3
4
//Daily sales matrix
	int** sold = new int*[noItems]; // rows
	for (int i = 0; i < noDays; ++i)
		sold[i] = new int[noDays]; // columns 


Should be like this:
1
2
3
4
//Daily sales matrix
	int** sold = new int*[noDays]; // rows
	for (int i = 0; i < noDays; ++i)
		sold[i] = new int[noItems]; // columns 
thank you for your help
i solve everything just the row sum won't work
i need fresh eyes to see where i have gone wrong


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{int i,j;
	char days[7][5]={"mon","tue","thur","wed","fri","sat","sun"};
	char item[5][8]={"milk","sugar","juice","rice","coffee"};
	int sold[7][5]={1,1,3,1,1,1,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1};
	   int rowsum=0,culsum=0;
	/*for(i=0;i<7;i++)
	
		{
			cout<<"Please enter the quantity sold for each item in "<<days[i]<<": ";
			
		
			cin>>sold[i][0]>>sold[i][1]>>sold[i][2]>>sold[i][3]>>sold[i][4];
			
	}*/
	 cout<<"\t";

   for(int i=0;i<7;i++)
        cout<<days[i]<<"\t";


   cout<<"sum"<<endl;
    
   
   int maxSale=0;
    char product[10];
   
    strcpy(product,item[0]);
	//print the row sum for each day
	 for( j=0;j<5;j++)
    { 
		
		cout<<item[j]<<"\t";

        for( i=0;i<7;i++)
		
            cout<<sold[i][j]<<"\t";

		{
		 rowsum+=sold[j][i];
        cout<<rowsum<<"\n";
		rowsum=0;
		}	
		
			
		
	 }
      if(rowsum>maxSale)
        {
            maxSale=rowsum;
            strcpy(product,item[j]);
        }

       cout<<endl;
	 
    

	 
	
  
    cout<<"sum\t";
	 //print the column sum at each day
     for(int i=0;i<7;i++){
	for(int j=0;j<5;j++)
		{
			

			culsum+=sold[i][j];
	}
			cout<<culsum<<"\t";
			culsum=0;
	}
    cout<<endl;
    //product of maximum sale
    cout<<"The best seller product is "<<product<<endl;
    //maximum sale
    cout<<"Qunatity sold "<<maxSale<<endl;


	getch();
	return 0;
}
Getting rid of that god-awful indentation style would probably help.

line 45 is not a part of the for loop.
thanks , won't work :)
Well maybe you should learn more before attempting this. Kemort gave you enough help to start with.
the output form should be in certain way , other ways i won't be stuck like this .
thank you .
closed account (48T7M4Gy)
If all you're talking about is the out of bounds error then that's no big deal and I knew about it because I swapped them around to cut down the amount of input I had to make and to make it quickly look like a table of values. A red flag at the end of the whole deal was trivial then and is now.

Don't get your proverbials in a knot folks, it's no big deal and it doesn't detract from the start the OP has been given.

Once again, feel free to use all, none or bits of my post. I did it mostly for my own interest and because the OP wasn't going anywhere - possibly still the same even now, so no harm.
closed account (48T7M4Gy)
BTW Roody line 8, your days of the week order is up the ...
We never said it was a big deal, so please don't take offense because I pointed out the solution for Roody. Besides I've already done this with ease as posted above..
Last edited on
closed account (48T7M4Gy)
No offense was taken - not even masters at the art of giving it have much effect on me. I noticed your snippets but didn't look closely because I knew what I had done and being 'caught out' was no surprise. Originally, would you believe, I was going to make it a 2 day week to save input rubbish. Maybe I should have.

However, well done on picking it up if you swapped them back again. Great minds think alike. :)
Topic archived. No new replies allowed.