Trying to pass a row of a 2d array to a 1d array

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
// This program calculates and displays the total rainfall for a year.
// This is a modified version of BE0702.
// This version will sort data in addition to its original functions.

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <vector>

using namespace std;

int main(){
    float rainfall[12], Total = 0;
    int MostCount, LeastCount, MaxValue, MaxAddress;
    int n1, n2;
    char MonthHolder[10];
    char month[12][10] = {"January", "February", "March",
                          "April", "May", "June",
                          "July", "August", "September",
                          "October", "November", "December"};

    for (n1 = 0; n1 < 12; n1++){
        cout.setf(ios::left);
        cout << "Enter the rainfall for " << setw(10) << month[n1] << ":  ";
        cin >> rainfall[n1];

        if (rainfall[n1] < 0){
            cout << "Invalid Entry." << endl;
            exit(0);
        }
    }
        MostCount = 0;
        LeastCount = 0;

    for (n2 = 0; n2 < 12; n2++){
        Total = Total + rainfall[n2];

        if (rainfall[n2] > rainfall[MostCount])
            MostCount = n2;
        if (rainfall[n2] < rainfall[LeastCount])
            LeastCount = n2;
    }

    cout << "The total rainfall for the year was " << Total << endl
         << "The average rainfall per month was " << Total / 12 << endl
         << "The month with the most rainfall was " << month[MostCount] << endl
         << "The month with the least rainfall was " << month[LeastCount] << endl;



        for (n1 = 0; n1 < 11; n1++){
            MaxValue = rainfall[n1];
            MaxAddress = n1;
            for (n2 = n1 + 1; n2 < 12; n2++){
                if (rainfall[n2] > MaxValue){
                    MonthHolder = month[n2];      // Invalid array assignment.
                    MaxAddress = n2;
                }
            }
            month[n1] = month[MaxAddress];        // Invalid array assignment.
            month[MaxAddress] = MonthHolder;      // Invalid array assignment.
        }

    cout << "The months sorted by rainfall follow from highest to lowest." << endl;
    for (n1 = 0; n1 < 12; n1++)
        cout << month[n1] << endl;

    return 0;
}


I am having trouble trying to find the correct syntax for this.
In this case you can use simple pointer:
1
2
3
4
5
    const char *MonthHolder;
    const char *month[] = {"January", "February", "March",
                          "April", "May", "June",
                          "July", "August", "September",
                          "October", "November", "December"};
this should solve your problem
Arrays have no the assignment operator. So as for character arrays you can copy them by using standard C functions as for example strcpy. To use these functions you should include header <cstring>
In this case your code will look the following way

strcpy( MonthHolder, month[n2] );
Last edited on
Thanks for the responses. I ended up doing strcpy. I might have used pointers, but it is not something I have learned yet. I have only to write four more programs before I start learning about them, and I am kind of keeping it a mystery on purpose.

My final code ended up looking like this:
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
// This program calculates and displays the total rainfall for a year.
// This is a modified version of BE0702.
// This version will sort data in addition to its original functions.

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <cstring>

using namespace std;

int main(){
    float rainfall[12], Total = 0, RainHolder;
    int MostCount, LeastCount, MaxValue, MaxAddress;
    int n1, n2;
    char MonthHolder[10];
    char month[12][10] = {"January", "February", "March",
                          "April", "May", "June",
                          "July", "August", "September",
                          "October", "November", "December"};

    for (n1 = 0; n1 < 12; n1++){
        cout.setf(ios::left);
        cout << "Enter the rainfall for " << setw(10) << month[n1] << ":  ";
        cin >> rainfall[n1];

        if (rainfall[n1] < 0){
            cout << "Invalid Entry." << endl;
            exit(0);
        }
    }
        MostCount = 0;
        LeastCount = 0;

    for (n2 = 0; n2 < 12; n2++){
        Total = Total + rainfall[n2];

        if (rainfall[n2] > rainfall[MostCount])
            MostCount = n2;
        if (rainfall[n2] < rainfall[LeastCount])
            LeastCount = n2;
    }

    cout << "The total rainfall for the year was " << Total << endl
         << "The average rainfall per month was " << Total / 12 << endl
         << "The month with the most rainfall was " << month[MostCount] << endl
         << "The month with the least rainfall was " << month[LeastCount] << endl;



        for (n1 = 0; n1 < 11; n1++){
            MaxValue = rainfall[n1];
            MaxAddress = n1;
            strcpy(MonthHolder, month[n1]);
            RainHolder = rainfall[n1];
            for (n2 = n1 + 1; n2 < 12; n2++){
                if (rainfall[n2] > MaxValue){
                    strcpy(MonthHolder, month[n2]);
                    RainHolder = rainfall[n2];
                    MaxAddress = n2;

                }
            }
            strcpy(month[MaxAddress], month[n1]);
            strcpy(month[n1], MonthHolder);
            rainfall[MaxAddress] = rainfall[n1];
            rainfall[n1] = RainHolder;
        }

    cout << "The months sorted by rainfall follow from highest to lowest." << endl;
    for (n1 = 0; n1 < 12; n1++)
        cout << month[n1] << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.