Can I combine a string array and a double array into a 2D array?

I currently trying to do this assignment and what I want to do is display the month with the corresponding integer that the user inputs from highest to lowest by columns and rows. Here's an example of the what I want it to look like.

1
2
3
                 
Example 1:   Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec
              12   11   10   9    8     7     6    5    4     3    2    1


Regardless these numbers could be integers, double, float etc...
Of course it doesn't have to look like this. The integer 12 could be inputted for Dec and be displayed as the month with the highest number. In fact, you could reverse this entire array as an another example. All I want to do is for it to look like example above and keep track of each number from the highest the lowest. For the second part, I'll probably have to use some kind of sorting algorithm which should be pretty easy. The part I'm stuck is combining both the string array and the double array into a 2D 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
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void SelectionSort(string[], double[], int);// Function Protoype.

int main()
{
const int SIZE = 12; /* A constant integer that represent the total
                       amount of months in a year. */

double totalRainfallPerMonth[SIZE]; /* Loop this array to force the user
                                       to enter variables for each
                                       element in this array. */

double totalRainfallPerYear = 0; /* The total amount of rainfall
                                    (in inches) per year. */

 // An array of every month.
string monthArray[SIZE]={"January", "February", "March", "April", "May",
                         "June", "July","August", "September",
                         "October", "November", "December"};

double average; // A variable that holds the average monthly rainfall.

int i; // Will be used as a counter for any loop.

cout << fixed << showpoint << setprecision(2); // Set decimal notation.

for(i=0; i<=11; i++)
{
   // Prompt the user to enter values.
   cout << "Please enter the total rainfall(in inches) for ";
   cout << monthArray[i] << ": ";
   cin >> totalRainfallPerMonth[i];

   while(totalRainfallPerMonth[i] < 0) /* If the user enters a negative
                                          value */
   {
       cerr << "No negative values allowed. "; // Display error message.
       cout << "Please try again. ";
       cin >> totalRainfallPerMonth[i];
   }
}

for(i=0; i<=11; i++)
{
       // Calculate the total rainfall for a year.
       totalRainfallPerYear += totalRainfallPerMonth[i];
}

// Display the total rainfall for a year.
cout << "\nThe total rainfall this year is " << totalRainfallPerYear;
cout << " inches of rain. " << endl;

// Calculate the average monthly rainfall.
average = totalRainfallPerYear / SIZE;

// Display the average
cout << "\nThe average monthly rainfall per month is ";
cout << average;
cout << " inches of rain. " << endl << endl << endl;

cout << "\n" << "Month " << "\t";
cout << "        Rainfall(in inches)" << endl;
cout << "-----------------------------------";

SelectionSort(monthArray, totalRainfallPerMonth, SIZE); /* Call in the
                                                           function. */

return 0;
}
Last edited on
Well after a day or two, I finally got it working. All I need to was create parallel arrays and sort both at the same time; pretty awesome.

Here is how look likes

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
void SelectionSort(string month[], double rain[], int SIZE)
{
	int i;
	int j;

	int min;

	for (i = 0; i < SIZE - 1; i++)
	{
		min = i; // The intial subscript or the first element.

		for (j = i + 1; j < SIZE; j++)
		{
			if (rain [j] > rain[min])  /* if this element is greater,
                                           then it is the new minimum */
			{
				min = j;

                 // swap both variables at the same times
                 double tempDouble = rain[i];
                 rain[i] = rain[j];
                 rain[j] = tempDouble;

		 string tempString = month[i];
                 month[i] = month[j];
                 month[j] = tempString;
			}
		}
	}

	for(i=0; i<=11; i++)
    {
        /* Display the amount of rainfall per month from highest to
           lowest */

        cout << "\n" << month[i] << "\t" << rain[i] << endl;
    }
}
Last edited on
Since you already finished for some fun here is a solution that uses a std::map to solve the problem.


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
#include <iostream>
#include <string>
#include <map>

int main() 
{
	std::map<std::string, int> temp    {{{"Jan.", 1}, 
	                                     {"Feb.", 2},
	                                     {"Mar.", 3},
	                                     {"Apr.", 4},
	                                     {"May.", 5},
	                                     {"Jun.", 6},
	                                     {"Jul.", 7},
	                                     {"Aug.", 8},
	                                     {"Sep.", 9},
	                                     {"Oct.", 10},
	                                     {"Nov.", 11},
	                                     {"Dec.", 12}}};
	                                     
	// Sort by number (Map sorting hackery)
	std::map<int, std::string> months;
	for (auto iter = temp.begin(); iter != temp.end(); ++iter)
	{
		months.insert(std::pair<int, std::string>(iter->second, iter->first));
	}
	
	
	// Standard printing (First the spelling then the numbers)
	for (auto iter = months.begin(); iter != months.end(); ++iter)
	{
		std::cout << iter->second << "  ";	
	}
	
	std::cout << std::endl << " ";
	
	for (auto iter = months.begin(); iter != months.end(); ++iter)
	{
		std::cout << iter->first << "     ";
	}
	
	return 0;
}
Cool, thanks for the reply.
Topic archived. No new replies allowed.