function calling with random generators

closed account (zT4NhbRD)
I'm not asking anyone at all to write the code for me or anything like that, just some pointers on where to start and what to think about when writing the functions. I apologize ahead of time if this kind of question isn't really suitable.

Last edited on
closed account (E3h7X9L8)
use srand function to get random numbers in a range :
rangevalue = (rand() % (nHigh - nLow + 1)) + nLow; (this will generate a number withing the range nLow and nHigh whenever you call it and assign it to variable rangevalue) dont forget to put srand(time(0)); at beggining of main function so the numbers can be random and include the library <ctime>
closed account (zT4NhbRD)
Thanks for the help but I've got that part down already. The trouble I'm having with that particularly is getting getScore to give a randomized score whose range is set in getStats only a certain of times. Any hints on how I can get that started?
closed account (E3h7X9L8)
1
2
3
4
5
6
7
8
9
10
int limits[2]; // declare an array for min and max of the range
getStats(limits); // use function getStats with array as param.

void getStats( int *limits); // dont forget you must use pointers *
{
cin >> limits[1]; // min
cin >> limits[2]; // max
}

//now you use array limits for int getScore (dont forget about pointer *) 
closed account (zT4NhbRD)
Oops sorry, should've mentioned not allowed to use arrays!
closed account (E3h7X9L8)
use 2 variables instead

1
2
3
4
5
6
7
8
int min,max;
getStats(min, max);

void getStats(int *min, int *max)
{
cin >> min;
cin >> max;
}
Last edited on
closed account (zT4NhbRD)
Unfortunately that won't work either.

Here's what I have so far. So far the main problem is the meanScore will not get calculated. It gets outputted as 0 every time.

Sorry for the horrible looking code. I'm just doing whatever I can to get this to work while fulfilling all the requirements set:

Last edited on
It's really difficult to tell what's going on in that code. You can you lay out a step by step what you're trying to accomplish? Do you get the range from a user? Or manually input them? Then what? just generate some scores within that range? Explain the problem better. Here are a couple observations though

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime> // needed to for time


using namespace std;

//Declare secondary functions before main

void printGradeRange(double meanScore);
void getStats(double &meanScore);
int getScore();


int main() {

	srand(int(time(0))); // Seed for rand

	//This needs to be initialized to some value 
	//before passing it to printGradeRange
	//As it is, this will not compile
	double totalMean;

	printGradeRange(totalMean);
	
	system("pause");
	return 0;

}
/*Functions*/
//Random Score Generator
int getScore()
{
	return (rand());
}
//Calculated Mean and Standard Deviation
void getStats(double &meanScore) 
{
	cout.precision(2);
	int getScore();
	//double N = 10.0; // Why are you initializing to 10 here? and then.....
	double totalScore = 0.0, randomScore1 = 0.0, randomScore2 = 0.0;
	//for (N = 10; N>0; N--) Initializing to 10 here?
	//do this
	for (int n = 10; n > 0; n--){// and why did it need to be a double instead of int?
		if (n > 4){
			randomScore1 = (getScore() % 101);
			cout << randomScore1 << endl;
		}
		else {
			randomScore2 = (getScore() % 30 + 40);
			cout << randomScore2 << endl;
			totalScore += randomScore2;
		}
		totalScore += randomScore1;
		meanScore = (totalScore / n);
	}
}
//Print: Scores Generated, Mean, Std, and Range
void printGradeRange(double meanScore)
{
	cout.precision(2);
	//Don't put return-type before function call
	//if You want to pass by reference, then the function prototype needs
	//a reference paramater, then you just plug in the variable to ref. Don't put
	//& before arguement in the call. And again, no data type in front of arguments. 
	getStats(meanScore);
	double givenMean;
	getStats(givenMean);
	cout << fixed << meanScore << endl;
}
closed account (zT4NhbRD)
Sorry for the confusion!

The range is supposed to be previously set in getStats. Everytime getStats calls getScore, getScore will randomly generate a number within the range set in getStats. Then within getStats the mean of all the scores generated is calculated.

Then in printGradeRange, it is supposed to print all the scores and the mean.

Got it now, though not completely. Will have to do for now. Thanks to all who replied. :)
Last edited on
Topic archived. No new replies allowed.