# Thread: Generate Random Numbers and Assign to Days of Week

1. ## Generate Random Numbers and Assign to Days of Week

Hi,

Can anyone help me with my assign...I need to create a C++ program that will generate 7 random numbers and will assign each number to a day of week...Example...
The random numbers are 89, 50, 90, 23, 10, 6, 19. The program must assign 89 to Monday, 50 to Tuesday and so on..then the program must arrange the numbers in descending order together with the day. Thanks...please help.

So far I'm ok with generating the numbers, I am just having problems on how to assign the days of week to the numbers...

Here's what I've done so far ,
Code:
```int NbrCars[LIMIT];
srand(unsigned(time(NULL)));

for (i=0; i<LIMIT; i++)
{
NbrCars[i]=rand() % 101;
}```
I need the part that will assign these numbers to the number of days...thanks

2. The random numbers are 89, 50, 90, 23, 10, 6, 19.
I think you may need to take another look at the assignment if you mean this the way it was stated.

3. Originally Posted by major_small
I think you may need to take another look at the assignment if you mean this the way it was stated.
Let me take his newline out and we'll see if you're still reading it the same way I read it the first few times:
Example...The random numbers are 89, 50, 90, 23, 10, 6, 19.

4. Just create a char [7][] containing the 7 days and use the indices days[0]...days[6] in printf() statements.

5. Originally Posted by SlyMaelstrom
Let me take his newline out and we'll see if you're still reading it the same way I read it the first few times:
I use my timestamp as an excuse. (2:29 AM EST)

6. the program will generate 7 random numbers and those numbers must be assigned to specific days of week. I just gave the numbers 89, 50, 90, 23, 10, 6, 19 as an example. What I need to do is assign these numbers to a specific day like Monday, Tuesday, etc using arrays. I'm having problems with using arrays for string, or maybe I can use enums...please help....

7. Enums would be a dandy idea if only you knew what number rand() was going to generate in advance. Instead, why not just make the random number the first thing in a date string? That way when you start sorting it can sort by the number first.

For instance output could look like:
Code:
```12: Tuesday
21: Wednesday
51: Thursday
. . .```

8. How about a std::map? You will still need to store a list of the 7 days somewhere as a lookup table, but you can use the random number as the key, and loop through the lookup table, to extract the days in order for each number to be paired with the day string.

9. How about this - you just need to figure out how to sort the char array by extracting the number chars and converting them to real numbs, hint look for a delimiter...

Code:
```#include <iostream>
#include <iomanip>
#include <ctime>

#define ROW 7
#define COL 80

using namespace std;

int main()
{
char weekArray[ROW][COL] = {" Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
char randomNumber[ROW][COL];
char unsortedArray[ROW][COL];

int index = 0;
int randNum = 0;

srand(time(NULL));

cout<<endl<<endl;

while(index < ROW)
{
randNum = rand();
itoa(randNum,randomNumber[index],10);
strcat(randomNumber[index],weekArray[index]);
strcpy(unsortedArray[index],randomNumber[index]);

cout<<unsortedArray[index]<<endl;
index++;
}

cout<<endl<<endl;

system("PAUSE");
return (0);

}```

10. From http://www.cplusplus.com/ref/cstdlib/itoa.html :
Portability.
Not defined in ANSI-C. Supported by some compilers.
You'd be better off using stringstreams or something else recommended in the FAQ.

11. Amazing how a simple job gets blown horrendously out of proportion

Code:
```//days of week counter array
int days_of_week[7];

//code to assign random values to each day in the array

//code to print the new array in descending order by iterating over the array 7 times and choosing the next largest element ala selection sort```

Popular pages Recent additions