Arrays

For example, there is a total of 12 months and the user will select the months using number 1-12 and then select the days using number as well in order to view the average temperature for the day itself. How do you put the months, days and the data in arrays?
1
2
float January = {24.5, 28.4, 23.5.............};
float February = {26.7, 26.1, 27.2...........};

Is the code above correct? And how do you code the program using stdio.h?
It's nearly there. See:
http://www.cplusplus.com/doc/tutorial/arrays/

Are you doing C or C++? If it's C then IO like this:
http://en.wikibooks.org/wiki/C_Programming/Simple_input_and_output
(and #include<stdio.h>)

If it's C++ then IO like this:
http://www.cplusplus.com/doc/tutorial/basic_io/
(and #include<iostream>)
Last edited on
I am doing C btw. But I still don't know how to make the code. If my above code are correct then how do I continue from here.
1
2
3
4
5
6
int month, day;
printf("Enter the month (1-12) for January to December: ");
scanf("%d", month);
printf("Enter the selected day: ");
scanf("%d", day);
printf("The average temperature for.............................................");

I am stuck there with the ......................................
I want it to show something like this.
For example the user key in 1 for the month and 21 for the day. It should show something like this "The average temperature for 21 January 2013 is 27.3."
Any help with this? Please... I'm just a beginner in programming. Doing this as my homework. Thanks!
Last edited on
sounds like you need a 2d array then. the first dimension being the month (0 - 11), and the second month. Then you need to use the 2 number you get from the user as look up coordinates.
take a look here:
http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

edit: to simplify things i'd make it work with just one month first (i.e. one array) and get the user to enter just a day. (you will of course have to take into account months having different number of days in them).
Last edited on
/Sorry, but I still don't understand how to do it. Maybe you can show me the method to do it? I change the code to something like this but there is error in both of them.
1
2
char month[] = { January, February, March,................};
float months[][] = { 24.5, 28.4, 23.5,...........}, {26.7, 26.1, 27.2,...........},{...............}.....;

Can you help me to solve the error as I am stuck here for quite a while now. For January there is a total of 31 day, February there is a total of 28 days and so on. Also, I could't display the message which says "The average temperature for 21 January 2013 is 27.3." Any help will be appreciated. Thanks!
Last edited on
firstly "January" is not a character, it's a string. maybe as a first step get the user to enter the month number?

here's a small example using 1 array (i've used c++ io simply because i don't like c IO so you'll have to change that)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>

// Example program
int main()
{
	int day = 0;
	double Jan[] = { 12.5, 13.5, 15.9, 1.6 };

	std::cout << "Enter the selected day: ";
	std::cin >> day;

	int index = day - 1;

	if (index < 4)
	{
		std::cout << "Rainfall on that day: " << Jan[index];
	}
	else
	{
		// too big a number
	}

	return 0;
}


basically i dont think there's a 'nice' way to do this in C. You might even need another array (or enum if you can use them in C?), to store how many days are in each month.
Thanks you for your explanation! I think I understood what you have just said. Here's another question. How to make a loop so that when user enter a invalid day or month, it will prompt them to re-enter the number again? I don't know whether to use do-while loop, for loop or while loop.
Well, when it comes to input validation, you can't assume that the users will incorrectly enter numbers x amount of times and then enter what you want. In that case, you would want to use a while loop.
i'd use a do.. while because you know you'll have to ask the user at least once. and a do..while naturally does this.
How do you do the do-while loop? I don't know what to put for the while ending statement.
Like you said, there are 12 months in a year. Once the user have finished inputting data for each of those months and the user doesn't want to enter anymore data, the do-while should break because the while statement is now false.
Okay, I understood the month part. But how about the day part? There are different number of days in each month so how do I do that?
Tell the user how many days would he like to enter data on for that month . For example, for February I want to input data on the 5, 15, 24 and 27. So I would that I would like to input data for 4 days then move on to the next month until the while loop exceed 12. If you have to input data for every day of the month, then you'll have to do something different.
For mine I think is input data for every day of the month. So every month will have different number of days. For example on January, there will be 31 days and on February, there will be 28 days. So how do I do the while loop?
If that's the case then you can create a separate array(dayArray) that follows each month. You can then have two for loops that run on two separate things. The outer for loop will be for how many months there are in a year. The inner loop represents how many days that particular month has. Once the inner loop has finish one entire iteration, the outer loop will move on to the next month and then the inner loop will execute again. Repeat this until you exceed 12 months.
Last edited on
I don't quite understand what you have just said but could you show me an example so that I can understand better. Thanks!
1
2
3
4
5
6
7
8
for(int i=0; i< insert number of months in a year here; i++)
{
    for(int j =0; j< insert number of days for this month here; j++)
        {
             // ask to the user to input data for this particular month.
        }
       cout << endl; // creates a new line after you're done with each month
}


Last edited on
The code that you just gave is asking user to input data. But I already got the data for all the month. I just need to show them the data when they had selected the month and day.

So for example, if they select 21 as the day and 2 as the month, it will show them the data for 21 Feb. However, if they select 29 as the day and 2 as the month, it will show them the day that they had selected is in valid as there are only 28 days in Feb. The user will then have to keep trying until they had key in a valid day.
@darkmage06

You could do it this way.

[In C++]
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
#include <iostream>
#include <string>

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

// Example program
int main()
{
	int day, month;
	string Rain[4][5] = {
		{ "January", "24.5", "28.4", "23.5", "21.8" },
		{ "February", "20.2", "13.8", "2.9", "-.1" },// "-.1" would be on 29th, 30th and 31st
		{ "March", "14.5", "24.7", "19.5", "28.1" },
		{ "April", "19.6", "14.9", "9.3", "22.2" }
		// Have each month have 31 days data, with a -.1 in calendar day past actual days in the given month
	};

	do
	{
		cout << "Enter the selected month (1 to 4 only ): ";
		cin >> month;
		if (month < 1 || month >4)
			cout << "Sorry, that was an invalid month. Please try again." << endl;
	} while (month < 1 || month >4);
	month--;// Subtract 1, to have 0 to 11 for month

	do
	{
		cout << "Enter the selected day (1 to 4 only ): ";
		cin >> day;
		if (Rain[month][day] == "-.1")
		{
			cout << "Sorry, the month selected, does not have " << day << " days. Try again." << endl;
			day = 0;// resets day to stay in whiile loop
		}
		if (day > 4)
			cout << "Sorry, that was an invalid day for the month of " << Rain[month][0] << endl;
	} while (day < 1 || day > 4);


	cout << "The average rainfall for " << Rain[month][0] << " " << day << " was " << Rain[month][day] << " inches." << endl;

	cin >> day; // Just to stop console from closing.
	
	return 0;
}


[ Or using C ]

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

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

// Example program
int main()
{
	int day, month;
	string Rain[4][5] = {
		{ "January", "24.5", "28.4", "23.5", "21.8" },
		{ "February", "20.2", "13.8", "2.9", "-.1" },// "-.1" would be on 29th, 30th and 31st
		{ "March", "14.5", "24.7", "19.5", "28.1" },
		{ "April", "19.6", "14.9", "9.3", "22.2" }
		// Have each month have 31 days data, with a -.1 in calendar day past actual days in the given month
	};

	do
	{
		printf("Enter the selected month (1 to 4 only ): ");
		cin >> month;
		if (month < 1 || month >4)
			printf("Sorry, that was an invalid month. Please try again.\n");
	} while (month < 1 || month >4);
	month--;// Subtract 1, to have 0 to 11 for month

	do
	{
		printf("Enter the selected day (1 to 4 only ): ");
		cin >> day;
		if (Rain[month][day] == "-.1")
			printf("Sorry, the month selected, does not have %d days. Try again.\n", day);
			
		if (day > 4)
			printf("Sorry, that was an invalid day for the month of %s\n", Rain[month][0].c_str());
	} while ((day < 1 || day > 4) || Rain[month][day] == "-.1");


	printf("The average rainfall for %s %d, was %s inches.\n", Rain[month][0].c_str(), day, Rain[month][day].c_str());
	cin >> day;
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.