Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iBookG4user

macrumors 604
Original poster
Jun 27, 2006
6,595
2
Seattle, WA
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;
}
 
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
 
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?
 
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

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;
}
 

Attachments

  • Screen shot 2009-09-20 at 5.42.31 PM.png
    Screen shot 2009-09-20 at 5.42.31 PM.png
    178.1 KB · Views: 2,047
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
 
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

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;
}
 
Now, do you know what he meant by writing the code to test the functions? What does he mean by that?

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.
 
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;
	}
 
I temporarily set the array to allow numbers larger than 10 and it did detect it, thanks :)

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.