Thread: Random Problem?

  1. #1
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41

    Question Random Problem?

    Okay i have this problem with the output, it displays all off the random numbers in their array except for the last array. The last array is filled with pretty much nonsense. I have tried a few things but cannot get it to work. Another problem is the random numbers are the exact same every time i run the program, such as array 1 will be 22 and array 2 will be 56 every single time i run the program.

    PHP Code:
    /****************************************************
              Random Lists - awlists.cpp
    -----------------------------------------------------
    Using 1D arrays and looping structures, display the
    contents of an array filled with random numbers.
    -----------------------------------------------------
                  Coded by: Andrew Weir
                   November 20th,2002
    ****************************************************/

    #include<iostream.h>
    #include<iomanip.h>
    #include<conio.h>
    #include<string.h>
    #include<math.h>
    #include<time.h>
    #include<stdlib.h>

    struct info
    {
            
    char userResponse;
            
    //used to repeat the program
            
    int iCounterrowCounterarrayNumberarrayActual[30], arrayCounterrandomNumber;
            
    //iCounter used for the loop in the outputForm function
            //rowCounter used to make sure it prints 5 items per row
            //arrayNumber used for the user input for amount of arrays
            //arrayActual is the actual array holding the random numbers
            //arrayCounter is used in the rndmProcedure function in the for loop
            //randomNumber is used to generate the random numbers
            
    string firstNamelastNamefullName;
    };

    info vars;

    void fowlerStatement()
    {
            
    gotoxy(25,2);
            
    cout<<"Random Lists";
            
    gotoxy(25,3);
            
    cout<<"~~~~~~~~~~~~";
            
    gotoxy(5,5);
            
    cout<<"This program will ask the user to input a number";
            
    gotoxy(5,6);
            
    cout<<"of arrays while the computer randomly fills each";
            
    gotoxy(5,7);
            
    cout<<"          array with a random number.";
            
    //gotoxy(5,8);
            //cout<<"         The number is between 1-10.";
            
    gotoxy(5,10);
            
    cout<<"This program was written in C++ by: Andrew Weir";
            
    getch();
    }

    void inputForm(infovars)
    {
            
    clrscr();
            
    gotoxy(25,2);
            
    cout<<"Random Lists";
            
    gotoxy(25,3);
            
    cout<<"~~~~~~~~~~~~";
            
    gotoxy(5,5);
            
    cout<<"Please enter your first name: ";
            
    cin>>vars.firstName;
            
    gotoxy(5,7);
            
    cout<<"Please enter your last name: ";
            
    cin>>vars.lastName;
            
    vars.fullName vars.lastName+", "+vars.firstName;
            
    gotoxy(5,9);
            
    cout<<"Please enter the amount of numbers to be generated: ";
            
    cin>>vars.arrayNumber;
            if(
    vars.arrayNumber>30)
            {
                    do
                    {       
    gotoxy(42,10);
                            
    cout<<"             ";
                            
    gotoxy(5,10);
                            
    cout<<"Please enter a number lower than 30: ";
                            
    cin>>vars.arrayNumber;
                    }
                    while(
    vars.arrayNumber>30);
            }
    }

    void rndmProcedure(infovars)
    {
            
    randomize;
            for(
    vars.arrayCounter 0vars.arrayCounter != vars.arrayNumbervars.arrayCounter++)
            {
                    
    vars.randomNumber random(99)+1;
                    
    vars.arrayActual[vars.arrayCounter] = vars.randomNumber;
            }
    }

    void outputForm(infovars)
    {
            
    clrscr();

            
    /*gotoxy(1,1);
            time_t t = time(NULL);
            cout<<ctime(&t);
            gotoxy(25,3);
            cout<<"Random Lists";
            gotoxy(25,4);
            cout<<"~~~~~~~~~~~~";
            gotoxy(5,6);
            cout<<"Name of client: ";
            cout<<vars.fullName;
            gotoxy(40,6);
            cout<<"Array # Limit: ";
            cout<<vars.arrayNumber;*/
            
    vars.rowCounter=0;
            
    vars.iCounter=0;

            do
            {
                    
    vars.iCounter++;
                    
    vars.rowCounter++;
                    
    cout<<setiosflags(ios::right);
                    
    //if(vars.iCounter!=vars.arrayNumber)
                    //{
                            
    cout<<vars.arrayActual[vars.iCounter]<<" ";
                    
    //}
                    
    if(vars.rowCounter>=6)
                    {
                            
    cout<<endl;
                            
    vars.rowCounter=0;
                    }
            }
            while(
    vars.iCounter!=vars.arrayCounter);
            
    getch();
            
    //gotoxy(15,12);
            //cout<<"-Thank You for using Random Lists-";
    }

    main()
    {
            
    info vars;
            
    fowlerStatement();
            do
            {
                    
    inputForm(vars);
                    
    rndmProcedure(vars);
                    
    outputForm(vars);
                    
    gotoxy(15,14);
                    
    cout<<"Would you like to try agin (y/n): ";
                    
    cin>>vars.userResponse;
            }
            while(
    vars.userResponse=='y' || vars.userResponse=='Y');

    Any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Looks to me like you've got your do-while loop backwards. You have:
    Code:
    do {
        vars.iCounter++;
        vars.rowCounter++;
        cout<<setiosflags(ios::right);
        cout<<vars.arrayActual[vars.iCounter]<<" ";
        if(vars.rowCounter>=6) {
            cout<<endl;
            vars.rowCounter=0;
        }
    } while(vars.iCounter!=vars.arrayCounter);
    which means that you are calling for array elements 1 to n for an n-sized array. The problem with that is that a n-sized array has indices from 0 to n-1, so you are skipping the first and overrunning the last element. When you call for array[n] you are retrieving whatever happens to be living in that memory slot, which is probably garbage. You should write the loop like this:
    Code:
    while (vars.iCounter != vars.arrayCounter) {
        cout<<setiosflags(ios::right);
        cout<<vars.arrayActual[vars.iCounter++]<<" ";
        if(vars.rowCounter++ >= 6) {
            cout<<endl;
            vars.rowCounter=0;
        }
    }
    (note that I have moved some of the increments to inside some other expressions). Try this and see if it works.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    for the other part look up use of srand() to seed the random generator. A search of the board or using your compiler's help section (if it has one) should provide you with the information you need.

  4. #4
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    awesome
    thanx a lot that helped!

  5. #5
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    got it!
    srand(time(NULL));

    thanx for all the help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with random numbers
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2007, 10:30 AM
  2. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  3. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM
  4. random problem
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2002, 02:39 PM
  5. Random things are fine, just one problem with them.
    By DarkSFK in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2002, 08:40 AM