Generate Random Numbers and Assign to Days of Week

This is a discussion on Generate Random Numbers and Assign to Days of Week within the C++ Programming forums, part of the General Programming Boards category; Hi, Can anyone help me with my assign...I need to create a C++ program that will generate 7 random numbers ...

  1. #1
    mms
    mms is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Angry 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. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote 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.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just create a char [7][] containing the 7 days and use the indices days[0]...days[6] in printf() statements.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote 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)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    mms
    mms is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    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. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    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. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  2. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 11:40 AM
  3. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  4. optimize random numbers b/w 1 to n w/o repetition
    By powinda in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21