C++ arrays and random numbers

Discussion in 'Mac Programming' started by iBookG4user, Sep 20, 2009.

  1. iBookG4user macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #1
    They do put the tutors for C++ at the most inconvenient times at my college... Anyways, the instructor posted the following directions:

    "Write a function that will fill an array with 25 random numbers between 1 and 10. Then write a function that will print out the array. Write some code in main to test your functions. Sometimes this test code is called a *driver

    Do not use global variables here. Pass the array to the function through the parameter list"

    My current code does not result in any errors int he debugger, but it doesn't output the array. And I don't know what he means by writing the driver, what am I supposed to test? You've all proved to be exceedingly helpful in the past and I'm sure you'll help point me in the right direction this time :)

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    double print(double array);
    double produce();
    int main()
    {
    	double produce();
    	double print();
    }
    
    double produce()
    {
    	srand(time(0));
    	double array[25];
    	for(int i=0; i<25; i++)
    	{ 
            array[i] = (rand()%10)+1; 
            cout << array[i] << endl;
    	}
    	return *array;
    	
    }
    
    double print (int array)
    {
    	cout << array << endl;
    }
    
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    A few things: Your array should be of type int[], not double[]. You need to accept an int array to produce and print. You need to declare an int[] in main, pass it to produce, fill it in there, then pass it to print. The << operator of ofstream does not deal with arrays as you'd hope, so you'll need to loop to display in print.

    Also, and i'm sure it's an honest mistake, i'm sure your professor meant pseudo-random.

    Also, in main, you are declaring new double()s, not invoking produce and print. Take the double away there, and pass in an argument as discussed above, and change the return type of both of these to void.

    -Lee
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    How would you write a function prototype for the function "produce"?
    And how would you write a call to the function "produce"?

    And inside your "main" function, which of the two have you written, a function prototype or a function call?
     
  4. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #4
    Ok, I made the changes that you suggested and the code does look more logical. Although now I am getting three errors in the for loops.

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    double print(int array);
    double produce(int array);
    int main()
    {
    	int array[25];
    	produce(*array);
    }
    
    double produce(int array)
    {
    	srand(time(0));
    	for(int i=0; i<25; i++)
    	{ 
            array[i] = (rand()%10)+1; 
            cout << array[i] << endl;
    	}
    	print(array); 
    	
    }
    
    double print (int array)
    {
    	for(int i=0; i<25; i++)
    	{ 
            cout << array[i] << endl;
    	}
    	return array;
    }
    
     

    Attached Files:

  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Change the argument type of produce to int[] or int *. Right now it's just int, so you can't use [], etc. Same goes for print. Also, you don't need to return anything from produce or print. Change their return type to void.

    I think the intent is for you to call print back in main, which should be fine.

    Also, when you pass array to produce, just use array not *array.

    -Lee
     
  6. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #6
    It worked :D
    Now, do you know what he meant by writing the code to test the functions? What does he mean by that? And thanks so much for your help so far!

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    double print(int *array);
    double produce(int *array);
    int main()
    {
    	int array[25];
    	produce(array);
    	print(array);
    }
    
    double produce(int *array)
    {
    	srand(time(0));
    	for(int i=0; i<25; i++)
    	{ 
            array[i] = (rand()%10)+1; 
            cout << array[i] << endl;
    	}
    	return *array;
    	
    }
    
    double print (int *array)
    {
    	for(int i=0; i<25; i++)
    	{ 
            cout << array[i] << endl;
    	}
    	return *array;
    }
    
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    The specification said the numbers in the array should be between 1 and 10. You could write a test that confirms that.

    It also said the numbers should be random. While randomness can be harder to test for than it seems, simple non-randomness can be easy to test for. The simplest is to confirm that all the numbers aren't the same value. More complex tests of randomness use statistical analyses.

    Another test you can write is to confirm the produce() function returns different values each time it's called. This is a simple form of statistical analysis, i.e. ensuring that consecutive invocations have independent results.

    You should also consider initializing the array with known invalid values, such as -1 or 0, before calling produce(). Otherwise an uninitialized local array may already contain unpredictable values, which a test could interpret as random even if produce() does nothing at all.

    As a specification, the statement of work is somewhat vague. For one thing, it doesn't specify what tests it has to pass. Believe me, if you leave it to most programmers to choose the qualification tests, it's a big mistake. The code might work in only one case, which was the case the programmer used to develop the code. If that seems crazy, I can assure you it's happened.
     
  8. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #8
    That was simple enough, thanks! :)
    This worked a treat:
    Code:
    if (print(array) < 1 || print(array) >10) // driver
    	{
    		cout << "Numbers are not between 1 and 10!" << endl;
    	}
    
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    Try to put an "invalid" number in your array (say, 11) and see if this function detects that.
     
  10. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #10
    I temporarily set the array to allow numbers larger than 10 and it did detect it, thanks :)
     
  11. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    Even if it's not the first element in the array..?

    The reason I'm asking is because your print() function (at least the last version shown in this thread) only returns the first element of the array (converted to a double).
     

Share This Page