Allowance Program(Two Dimensional Array)

I'm currently stuck with this problem. I am to input my allowance for 7 days and 4 weeks, while using a two dimensional array. While I am entering my allowance, the program must show what day it is and what week
Example:

Week 1
Sunday:50
Monday: 100
Tuesday: 200
Wednesday: 50
Thursday: 20
Friday:70
Saturday:50
Week 2:
Monday:50

And so on...

after the input, the program will then display all of my allowances in a two dimensional array pattern and must calculate and add all of my allowances, in both rows and columns.

I'm having a hard time figuring what to do. Can anyone enlighten me about this problem?
1
2
3
4
5
6
7
int allowance [7][4];
for (i=0; i <4; i++)
   for(j=0; j<7;j++)
      {
      cout << "Enter daily allowance: ";
      cin >> allowance [j][i];
      }

That should get you started.
Last edited on
Hint about the day and week part: Array element positions start counting from 0 and end one less then the total number of elements.
Delete parts you don't need. Additionally you can try to explain how "fancy way to find max number width" works.
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
#include <iostream>
#include <string>
#include <cmath>   //for log10()
#include <iomanip> //for setw()


int main()
{
    std::string weekday[7] = {"Monday", "Tuesday", "Wednesday", "Thursday",
                              "Friday", "Saturday", "Sunday"};
    std::string weekshort[7] = {"M", "T", "W", "Th", "F", "S", "Su"};
    int allowance[4][7] = {{0}};

    //input
    for(int week = 0;week < 4; ++week) {
        std::cout << "Week " << (week + 1) << std::endl;
        for(int day_of_week = 0; day_of_week < 7; ++day_of_week) {
            std::cout << weekday[day_of_week] << ": ";
            std::cin >> allowance[week][day_of_week];
        }
    }
    //Find max allowance value
    int max = allowance[0][0];
    for(int i = 0; i < 4; ++i)
        for(int j = 0; j < 7; ++j)
            if (allowance[i][j] > max)
                max = allowance[i][j];
    int size = std::log10(max) + 2; //fancy way to find number width

    //Outputting table header:
    std::cout << " ";
    for(int i = 0; i < 7; ++i)
        std::cout << std::setw(size) << weekshort[i];
    std::cout << std::endl;

    //Outputting table:
    for(int i = 0; i < 4; ++i){
        std::cout << (i + 1);
        for(int j = 0; j < 7; ++j)
            std::cout << std::setw(size) << allowance[i][j];
        std::cout << std::endl;
    }

    return 0;
}

Last edited on
That code you gave looks a lot like what I'm currently stuck with. Thing is, for every input I give, there must be a day that corresponds to it.
Like, for example:
Monday = 100
Tuesday = 50


That's the part where I'm having trouble. I can't figure out how to automatically display the days before I input a value.
Did you try my code? Lines 18 and 19 should help you with this problem.
Yes, I have already tried it. It works well until the "fancy way to find number width part".
I've PM'ed you about it. Did you receive it?
Modified program a little. Now with sum of allowance!
EDIT: Faster way to calculate columns width, fancier automatic column widt detection, support negative numbers.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>

const unsigned TOTAL_WEEKS = 4;

int main()
{
    std::string weekday_names[7] = {"Monday", "Tuesday", "Wednesday", "Thursday",
                                    "Friday", "Saturday", "Sunday"};
    std::string weekday_shortnames[7] = {"M", "T", "W", "Th", "F", "S", "Su"};
    int allowance[TOTAL_WEEKS][7];

    //input
    for(unsigned week = 0;week < TOTAL_WEEKS; ++week) {
        std::cout << "Week " << (week + 1) << std::endl;
        for(unsigned day_of_week = 0; day_of_week < 7; ++day_of_week) {
            std::cout << weekday_names[day_of_week] << ": ";
            std::cin >> allowance[week][day_of_week];
        }
    }
    //calculate max allowance
    int weektotal[TOTAL_WEEKS] = {0};
    int weekdaytotal[7] = {0};
    for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
        for(unsigned j = 0; j < 7; ++j) {
            weektotal[i] += allowance[i][j];
            weekdaytotal[j] += allowance[i][j];
        }

    //Find optimal table columns width
    int size[9];
    size[0] = std::to_string(TOTAL_WEEKS).length();

    for(unsigned j = 0; j < 7; ++j) {
        int max = std::numeric_limits<int>::min();
        for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
            if (max < allowance[i][j])
                max = allowance[i][j];
        if (max < weekdaytotal[j])
            max = weekdaytotal[j];
        size[j + 1] = std::to_string(max).length() + 1;
        size[j + 1] = std::max(size[j + 1], 3);
    }

    int max = std::numeric_limits<int>::min();
    for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
        if (max < weektotal[i])
            max = weektotal[i];
    size[8] = std::to_string(max).length() + 1;
    size[8] = std::max(size[8], 4);

    //Outputting table header:
    std::cout << std::setw(size[0]) << " ";
    for(unsigned i = 0; i < 7; ++i)
        std::cout << std::setw(size[i + 1]) << weekday_shortnames[i];
    std::cout << std::setw(size[8]) << "Ttl";
    std::cout << std::endl;

    //Outputting table:
    for(unsigned i = 0; i < TOTAL_WEEKS; ++i){
        std::cout << std::setw(size[0]) << (i + 1);  //Week number
        for(unsigned j = 0; j < 7; ++j) //Outputting allowance value for a week
            std::cout << std::setw(size[j + 1]) << allowance[i][j];
        std::cout << std::setw(size[8]) << weektotal[i]; //Outputting total
        std::cout << std::endl;
    }
    //Footer (Weekday totals)
    std::cout << std::setw(size[0]) << " ";
    for(int i = 0; i < 7; ++i)
        std::cout << std::setw(size[i + 1]) << weekdaytotal[i];
    std::cout << std::endl;

    return 0;
}

Output:

Week 1
Monday: 1
Tuesday: 5
Wednesday: 10
Thursday: 50
Friday: 100
Saturday: 500
Sunday: 1000
Week 2
Monday: 1
Tuesday: 5
Wednesday: 10
Thursday: 50
Friday: 100
Saturday: 500
Sunday: 1000
Week 3
Monday: 1
Tuesday: 5
Wednesday: 10
Thursday: 50
Friday: 100
Saturday: 500
Sunday: 1000
Week 4
Monday: 1
Tuesday: 5
Wednesday: 10
Thursday: 50
Friday: 100
Saturday: 500
Sunday: 1000
   M  T  W  Th   F    S   Su  Ttl
1  1  5 10  50 100  500 1000 1666
2  1  5 10  50 100  500 1000 1666
3  1  5 10  50 100  500 1000 1666
4  1  5 10  50 100  500 1000 1666
   4 20 40 200 400 2000 4000
Last edited on
Topic archived. No new replies allowed.