How to use 2D Arrays with different types?

I have this 2D array code that I don't really understand but can somebody tell me how to use both dimensions (NUM_MONTHS & months) in the same function, though they're different types?

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

using namespace std;

const int NUM_MONTHS = 12;
const string months[NUM_MONTHS] = { "January","February","March","April","May","June","July","August","September","October","November","December" };

void getData(const string list[], int listSize);

int main()
{
    const string myList[NUM_MONTHS];
    
    getData(myList, NUM_MONTHS);
	
	return 0; 
}

void getData(const string list[], int listSize)
{
    for(int i = 0; i < listSize; i++)
    {
        cout << "Enter the highest temperature for the month of " << months[i] << ": " << endl;
       
        cout << "Enter the lowest temperature for the month of " << months[i] << ": " << endl;

    }
}
Hello domweng,


I have this 2D array code that I don't really understand


It is very possible that you do not understand this because you do not have a 2D array. Just 1D arrays.

From what I read I would have to guess that you want something like:
 
int temps[NUM_MONTHS][2]{};

Then the function would need nested for loops to deal with the row and column of the array.

"NUM_MONTHS" is a constant variable defined at global scope of the file. That means that any line of code that follows has access to it, so why are you passing a global variable in "main" to your function?

The function only needs 1 parameter, the array.

Something to think about: const string months[NUM_MONTHS] = { "", "January","February",. Sometimes skipping element (0)zero of the array can make the program easier to work with. Here I do not believe it would make any difference.

Andy
Well I’m kind of lost and the teacher has the global arrays set for the assignment, i’m just confused how to use both types as i’m not able to edit the arrays
Hello domweng,

Well there is nothing wrong with lines 7 and 8 they work fine.

I do not know if you were given the code for lines 10 thru the end, but it will not work.

As this is an assignment for school post the instructions that you were first given. What you said from the start does not match the code posted.

For the include file "climits" I would just use the C++ "limits" and and not the C++ version of the C header file "limits.h". Either way there is no code that would use this header file.

Lines 10, 14 and 21 all start with const. I do not know if you realize that these variables can not be changed.

Andy
you have an integer somewhere from 0 to 11 or 1 to 12 or something that represents the month in a year.
you need to do something with it like print ...
cout << "the month is " << months[some_month] << endl;

that is all you probably need to do here.
where you get some_month is in the data somewhere or input from the user etc.
you need to validate that it is in range, and if it turns out the range is 1-12 instead of 0-11, you need to subtract 1 from it:
cout << "the month is " << months[some_month-1] << endl;
because most humans thing 1-12 for months, not 0-11.
Thanks everyone for the help I will read through in the morning. If these points help to understand the question better.

1. all that was given for the assignment was the two constants.
2. i’m really just confused on in my functions if I need two lists made, what data type/s it should be, if I need one [] or two [][], and what to put in those boxes.
Hello domweng,

domweng wrote:

1. all that was given for the assignment was the two constants.


In the following code I take this to meal lines 7 and 9. That works. There is nothing wrong with those lines or having to use them.

Line 9 is to show you that there other ways to write this to make it easier to read.

Line 15 is most likely unfamiliar to you. It creats a new data type like you would use "char" or "int". It does have its advantages, but not necessary to use.

Based on the function name and the code you started with along with the subject line. This is what I am thinking:
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
#include <iostream>
#include <string> 
#include <climits>

using namespace std;

const int NUM_MONTHS = 12;
constexpr int MAXROWS{ 12 }, MAXCOLS{ 2 };
const string months[NUM_MONTHS]
{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

using MYLIST = int[MAXROWS][MAXCOLS];

void getData(MYLIST& myList);  // <--- No need to pass a global variable.

int main()
{
    MYLIST myList{};  // <--- ALWAYS initialize your variables.

    getData(myList);


    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

void getData(MYLIST& myList)
{
    const std::string PROMPTS[]{ "highest", "lowest " };  // <--- Switch names if you want "lowest" first.
                                                          // Same thing for line 46 if you want to use it.

    for (int row = 0; row < MAXROWS; row++)
    {
        std::cout << '\n';

        for (int col = 0; col < MAXCOLS; col++)
        {
            std::cout << " Enter the " << PROMPTS[col] << " temperature for the month of " << months[row] << ": ";
            //std::cout << " Enter the " << (col ? " lowest " : "highest ") << "temperature for the month of " << months[row] << ": ";

            std::cin >> myList[row][col];
        }
    }
}


This gives me this output:

 Enter the highest temperature for the month of January: 95
 Enter the lowest  temperature for the month of January: 10

 Enter the highest temperature for the month of February: 60
 Enter the lowest  temperature for the month of February: 20

 Enter the highest temperature for the month of March:


For the information you are trying to keep track of this makes more sense.

Since your instructions seem to be lacking it would help if you describe what you have learned so far and what you are trying to accomplish with the program, i.e., what this program is trying to teach or the new material you have just learned.

Andy
Well I always show to class and take notes etc. but the teacher sometimes doesn't seem to clearly explain every part and what it does. I come to the forums because she also doesn't seem to like to reply to emails for help and when she does it's pretty vague. She isn't the worst teacher but for my major being csci I want to be able to learn.

But I spent all day figuring out 1D arrays for another assignment pretty much by myself and the teacher usually does a demo but they skipped doing a practice showing of how to do multi-dimensional so just kind of at a loss as of now.

So I've pretty much learned everything in the basic C++ lessons all the way up to these multi-dimensional arrays which annoys me because they are going to be important throughout the rest of my future and I'm struggling with declaring functions using arrays because it wasn't really taught in depth.

But it is also my fault for being lazy and dreading reading so no excuses but that's pretty much what I know so far.

I'm not asking for a full answer just want to understand how to use arrays in functions because these assignments are dreadful being this confused :/
because they are going to be important throughout the rest of my future


Probably not. In real C++ c-style arrays aren't used that much. Other C++ containers are preferred/used.
are you still struggling with arrays to functions?

here...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<string>
#include<iostream>
using namespace std;
string months[12]
{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

string & get_text_month(string * s, int i)
{ //accepts an array (as a pointer, c++ converts for you using array's name) 
  //and a number.  lets say that put in 1, you want  the first month which is [0]
// so -1 for it
    return s[i-1];
}

int main()
{
  cout << "the 10th month is " << get_text_month(months,10) << endl;
}


or generally you tend to pass the array, how big it is (if you need to iterate it):
1
2
3
4
5
6
7
8
void printmonths(string*s, int size)
{
   for(int i = 0; i < size; i++)
     cout << s[i] << endl;
}

printmonths(months, 12);


multiple dimensions are a pain. I tend to avoid them entirely if possible, but they work the same way with a small exception ... you pass it in, along with the dimension sizes for each dimension. The exception is that only 1 dimension can change, the rest need to be constant. That limitation is a nonstop pain that is one of the reasons I avoid them!
Last edited on
Topic archived. No new replies allowed.