Subscribe Now: standard

Saturday, October 24, 2009

using random() in c++ to asign values randomly


hi all,
today the number of programs that make use of randomly generated numbers are huge, They range from ticket reservation program to Programs that generate random encrypted keys and passwords.
c++ has  a have an inbuilt random number generating function called random found in
the stdlib.h header file.
Here we will be using it to write a program that assign duty to 7 doctors over 7 days a week such that non of them work on the same day and no doctor works 2 days a week. please replace the () in red with < and>
========================CODE======================

#include(iostream.h)

#include(stdlib.h)

        #include(string.h)
struct doc{
    char dname[25];
    int did;
    int wnum;
};

int main()
{
    int dnum[7]={1,2,3,4,5,6,7};
    doc d[7];
    for(int i=0;i<7;i++)
    {
        cout<<"Enter the Details:\n";
        cin.getline(d[i].dname,25);

        cin>>d[i].did>>d[i].wnum;
    }
    long x,j=0;
    for(int i=0;i<7;i++)
    {
        j=0;
        while(j==0)
        {
           
                    x= random(7) + 1;
                    
            if(dnum[x-1]==x)
            {
                j=1;
                cout<<<" "<<<" "<
                                switch (x)
                                {
                                    case '1': cout<<" \nMONDAY";
                                    break;
                                     case '2': cout<<" \nTUESDAY";
                                     break;
                                      case '3': cout<<" \nWEDNESDAY";
                                      break;
                                       case '4': cout<<" \nTHURSDAY";
                                       break;
                                        case '5': cout<<" \nFRIDAY";
                                        break;
                                         case '6': cout<<" \nSATURDAY";
                                         break;
                                          case '7': cout<<" \nSUNDAY";
                                          break;
                                }
                dnum[x-1]= -2;
            }
        }
    }


        return 0;
}

===================END=======================

If you wnat any help feel free to email me at blaise.crowly@gmail.com

Blaise M Crowly
http://blaisemcrowly.co.cc

No comments:

Post a Comment