Populating an Array

Hey everyone, I need help in populating my Days[] array. I have an assignment that requires me to write a program that generate random months and days. I can generate the months, it's just the dates in particular. That's where Days[] comes in. I need the NumDays[] array to determine the range of input to days[] array. So, for NumDays[1] = 31, Days[] = {1,2,3,4....31}.
I know I would need a for loop to populate it, but I don't know how to do it. I'm sorry if the way i'm asking this is confusing.

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
  int main()
{
	const int TOTAL_MONTHS = 12;
	int months[TOTAL_MONTHS] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	int num, year,search;

	cout << "Enter the number of dates to generate: ";
	cin >> num;

	cout << "Enter year: ";
	cin >> year;

	if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0))
	{
		int numDays[TOTAL_MONTHS] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                //int Days[] = {0}; 
		//for(int i = 0; i < numDays[i]; i++}
               //Is that part is right? 
	}
	else
	{
		int numDays[TOTAL_MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		//for loop that would populate Days[] here
	}
	cout << "Enter a date to search for in mm/dd format: ";
	cin >> search;
	//binary_search
	return 0;
}
Last edited on
Write your question here.

What is your question?
Sorry about that, I edited it so now my question is there.
Okay, I think I'm one step closer to my solution. Can someone tell me if what I've done so far is right? I had to move the leap year validation into my random number generator function, so it looks different from what I originally posted:

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
77
78
const int TOTAL_MONTHS = 12;
	if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0))
	{
		int numDays[TOTAL_MONTHS] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (numDays[0] || numDays[2] || numDays[4] || numDays[6] || numDays[7] || numDays[9] || numDays[11])
		{
			Days[31];
			for (int i = 0; i < 31; i++)
			{
				Days[31] = i;
			}
		}
		else if (numDays[3] || numDays[5] || numDays[8] || numDays[10])
		{
			Days[30];
			for (int i = 0; i < 30; i++)
			{
				Days[30] = i;
			}
		}
		else
		{
			Days[29];
			for (int i = 0; i < 31; i++)
			{
				Days[29] = i;
			}
		}
		for (int i = 0; i < num; i++)
		{
			int index = rand() % 12;
                      /*int day_index = rand() % Days[]  Compiler error - Currently working on fixing it*/
			cout << months[index] << ' ';
		}
		cout << endl;
	}
	else
	{
		int numDays[TOTAL_MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (numDays[0] || numDays[2] || numDays[4] || numDays[6] || numDays[7] || numDays[9] || numDays[11])
		{
			Days[31];
			for (int i = 0; i < 31; i++)
			{
				Days[31] = i;
			}
		}
		else if (numDays[3] || numDays[5] || numDays[8] || numDays[10])
		{
			Days[30];
			for (int i = 0; i < 30; i++)
			{
				Days[30] = i;
			}
		}
		else
		{
			Days[29];
			for (int i = 0; i < 31; i++)
			{
				Days[29] = i;
			}
		}
		for (int i = 0; i < num; i++)
		{
			int index = rand() % 12;
			cout << months[index] << ' ';
		}
		
		for (int i = 0; i < num; i++)
		{
			int index = rand() % 12;
			cout << months[index] << ' ';
		}
		cout << endl;
	}



So far, the code only shows that I'm randomly generating the months and not the dates, I'm still working on the dates part.
Last edited on
Okay, the code I have above ^ does populate the Days[] array, which yes, i guess did kinda solve my original problem. The other problem I'm having now is trying to generate the random dates 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
for (int i = 0; i < num; i++)
{
int index = rand() % 12; //this works to display months
/*
i've tried many different approaches to get the days of the month, 
It will compile and run, however, once it reaches the point where it should generate 
both the months and days, it will stop working and quit.

Example

int index = rand() % 12; //this works to display months
if (index == 1 || index == 3 || index ==5 || index == 7 || index == 8 || index == 10 || index == 12)
{
   day_index = rand() % 31;
cout << months[index] << "/" << days[day_index] << ' ';
}

I'm assuming that the error is that days[], isn't initialized like i hoped it would have 
been in the earlier if-else- statements. 
If this is true, then I guess I never solved my original problem.
How would I go about fixing this? 

*/

cout << months[index] << ' ';
}
Last edited on
Still working on this, I believe I'm getting closer to solving this I am now able to generate the random days, with one slight problem. I know that there is something wrong with the input validation because in one run, february came out with 30 days instead of 29 or 28. Also, sometimes the loop ends early.
In my main function, it prompts the user to enter the number of dates they would like to generate (num) and that number is then passed into my void random number generator function.
I've found that sometimes, ill set num to 5, but it will only display 4 months and dates, and other times, it will complete all 5. Which line is causing this 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
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
77
78
79
80
81
#include <iostream>
#include <ctime>
using namespace std;
 int main()
{
	const int TOTAL_MONTHS = 12;
	int months[TOTAL_MONTHS] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	int num, year,search;

	cout << "Enter the number of dates to generate: ";
	cin >> num;

	cout << "Enter year: ";
	cin >> year;
}


void random_dates (int months[], int numDays[], int Days[], num, year)
{
	srand((unsigned)time(0));
	const int TOTAL_MONTHS = 12;
	if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0))
	{
		int numDays[TOTAL_MONTHS] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		
		for (int i = 0; i < num; i++)
		{
			int index = rand() % 12;
			if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12)
			{
				int Days[31] = { 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 };
				int day_index = rand() % 31;
				cout << months[index] << "/" << Days[day_index] << ' ';
			}
			else if (index == 4 || index == 6 || index == 9 || index == 11)
			{
				int Days[30] = { 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 };
				int day_index = rand() % 30;
				cout << months[index] << "/" << Days[day_index] << ' ';
			}
			else if (index ==2)
			{
				int Days[29] = { 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};
				int day_index = rand() % 29;
				cout << months[index] << "/" << Days[day_index] << ' ';
			}
		}
		cout << endl;
	}
	else
	{
		int numDays[TOTAL_MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		{
			for (int i = 0; i < num; i++)
			{
				int index = rand() % 12;
				if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12)
				{
					int Days[31] = { 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 };
					int day_index = rand() % 31;
					cout << months[index] << "/" << Days[day_index] << ' ';
				}
				else if (index == 4 || index == 6 || index == 9 || index == 11)
				{
					int Days[30] = { 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 };
					int day_index = rand() % 30;
					cout << months[index] << "/" << Days[day_index] << ' ';
				}
				else if (index == 2)
				{
					int Days[28] = { 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 };
					int day_index = rand() % 28;
//int day_index = rand() % ((28 - 1) + 1); I've tried this as well, hoping it would limit February to 28 days, but it will still surpass it and go to 30 days.
					cout << months[index] << "/" << Days[day_index] << ' ';
				}
			}
		}
		cout << endl;
	}
	cout << endl;
}
Last edited on
Topic archived. No new replies allowed.