Hi everyone. First time here in bytes, but, I have a problem here dealing with adding a generated set of random numbers. Here is the code. And I recommend that you guys run it to see what I'm talking about. -
#include <iostream>
-
#include <iomanip>
-
#include <cstdlib>
-
#include <cmath>
-
-
using namespace std;
-
//declarations
-
int seed, n, lower, upper, num;
-
double sum1, sum2, avg;
-
bool repeat = true;
-
-
//function prototypes
-
void getParameters(int& seed, int& n, int& lower, int& upper);
-
int getNum(int lower, int upper, int n, int seed);
-
void update(int num, double& sum1, double& sum2);
-
//void output(double avg, double std);
-
-
int main (){
-
char input;
-
do {
-
getParameters(seed,n,lower,upper);
-
getNum(lower, upper, n, seed);
-
update(num,sum1,sum2);
-
//output(avg, std);
-
cout<<endl;
-
cout<<"Enter Y or y to repeat, N or n to exit. ";cin>>input;
-
}while(input == 'Y' || input == 'y');
-
if (input == 'N' || input == 'n') repeat = false;
-
system ("pause");
-
return 0;
-
}
-
-
/***************************************************************
-
The function gets the seed, size, upper, and lower bounds used
-
to generate the random numbers.
-
***************************************************************/
-
void getParameters(int& seed, int& n, int& lower, int& upper){
-
cout<<"Seed for random number generator: ";cin>>seed;
-
cout<<"The size of the sequence: ";cin>>n;
-
cout<<"Lower and upper bound for the random number:";cin>>lower>>upper;
-
}
-
-
/***************************************************************
-
This function generates the random numbers based on the user input.
-
***************************************************************/
-
int getNum(int lower, int upper, int n, int seed){
-
int i, a;
-
cout<<"The numbers generated are: "<<endl;
-
srand(seed);
-
for (i = 1; i <= n; i++){
-
a=(rand()%(upper-lower)+lower);
-
cout<<setw(4)<<a;
-
}
-
cout<<endl;
-
return a;
-
}
-
-
/***************************************************************
-
This function should add up all the random numbers (sum1), and
-
square it (sum2).
-
***************************************************************/
-
void update(int num, double& sum1, double& sum2){
-
-
}
-
/***************************************************************
-
This function will output the average and standard deviation
-
using the values from sum1 and sum2.
-
***************************************************************/
-
//void output(double avg, double std){
-
// }
-
Okay so here's the deal. If you run it, it's pretty straightforward. Program asks the user for the seed, upper and lower bounds, and size of the sequence to generate the random numbers. Thing is, as seen in my getNum function, I assigned the "randomizing" equation to a variable a. Now in the update function, I need to get the sum of those values (generated in getNum) to move on to the next phase of my coding (average and standard dev). My question is, is it possible to find the sum in getNum inside the update function? Am I even doing it the right way? Because it generated a set of values, because then these values are assigned to one variable, which I know is totally impossible, because one variable should only store one value. So I'm pretty much stuck. Also, most of the codes that I've seen while finding a solution is assigning the rand() to different variables, and then adding the values in those variables. Still don't get it? Okay, the codes that I've seen are, let's say:
a=rand(); equals some random value
b=rand(); equals some random value
c=rand(); equals some random value
sum = a+b+c;
Mine is:
(In getNum function)
a=rand(); equals multiple random values (because there's a for loop involved)
(In update function)
sum=?? (how to add these values??) <-- and finding the sum is in another function
Is my procedure kinda right? Or do I have to do the summation inside the getNum function and just "transfer" it to the update function? As an added catch, no arrays should be used. I know it's something simple, but for some reason I can't find the right operator or function or something for it. Anyways, here it is. Comments and suggestions are welcome. Thanks very much for helping. (Better get myself working again!!)
3 2753
Use a separate variable to keep a running sum.
yes, yes, yes, and thank you so much Rabbit. I was actually thinking on using the operator "+=" on this problem, but I was having doubts. I dug up some info about the running sum that you recommended and it used the operator "+=", so that pretty much solved my problem there. I'll post the complete working code, for future references to other users. That's it and thanks again.
So here is the final code. Feel free to test this and salvage any codes that you wish to use in your codes. -
-
#include <iostream>
-
#include <iomanip>
-
#include <cstdlib>
-
#include <cmath>
-
-
using namespace std;
-
int seed, n, lower, upper, num=0;
-
double sum1, sum2, avg, total=0, stn;
-
bool repeat = true;
-
//function prototypes
-
void getParameters(int& seed, int& n, int& lower, int& upper);
-
int getNum(int lower, int upper, int n, int seed);
-
void update(int num, double& sum1, double& sum2);
-
void output(double avg, double stn);
-
-
int main (){
-
-
char input;
-
do {
-
getParameters(seed,n,lower,upper);
-
getNum(lower, upper, n, seed);
-
update(num,sum1,sum2);
-
output(avg, stn);
-
cout<<endl;
-
cout<<"Enter Y or y to repeat, N or n to exit. ";cin>>input;
-
}while(input == 'Y' || input == 'y');
-
if (input == 'N' || input == 'n') exit (1);
-
system ("pause");
-
return 0;
-
}
-
-
/***************************************************************
-
The function gets the seed, size, upper, and lower bounds used
-
to generate the random numbers.
-
***************************************************************/
-
void getParameters(int& seed, int& n, int& lower, int& upper){
-
system("cls");
-
cout<<"Seed for random number generator: ";cin>>seed;
-
cout<<"The size of the sequence: ";cin>>n;
-
cout<<"Lower and upper bound for the random number:";cin>>lower>>upper;
-
}
-
-
/***************************************************************
-
This function generates the random numbers based on the user input.
-
***************************************************************/
-
int getNum(int lower, int upper, int n, int seed){
-
int i, a=0;
-
cout<<"The numbers generated are: "<<endl;
-
srand(seed);
-
for (i = 1; i <= n; i++){
-
a=(rand()%(upper-lower)+lower);
-
total+=a;
-
num += (a*a);
-
cout<<setw(4)<<a;
-
}
-
cout<<endl<<endl;
-
}
-
-
/***************************************************************
-
This function adds up all the random numbers (sum1), and adds the
-
square of the individual numbers(sum2).
-
***************************************************************/
-
void update(int num, double& sum1, double& sum2){
-
sum1=total;
-
sum2=num;
-
}
-
-
/***************************************************************
-
This function will output the average and standard deviation
-
using the values from sum1 and sum2.
-
***************************************************************/
-
void output(double avg, double stn){
-
avg = sum1/n;
-
stn = sqrt((sum2-(2*avg*sum1)+(n*avg*avg))/n);
-
cout.setf(ios::fixed | ios::showpoint);
-
cout.precision(2);
-
cout<<"Average: "<<avg<<" "<<"Standard Deviation: "<<stn;
-
cout<<endl;
-
total = 0;
-
num = 0;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Stavros Christoforou |
last post by:
Hello everyone,
I was wondering if someone could help me with an issue I have in C++. I
want to select random points within the volume of a sphere. I know how
to get random numbers using srand()...
|
by: Intaek LIM |
last post by:
generally, we use srand(time(0)) to generate random numbers.
i know why we use time(0), but i can not explain how it operates.
first, see example source below.
...
|
by: abhi |
last post by:
How do you get random numbers in vb .net?
I want random six digit numbers.
Cheers,
Abhi
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: SpecialEd |
last post by:
I'm having trouble adding random numbers that I've generated using a for loop. So far my block of code looks:
cout<<"Please enter the number of dice you want to use:";
cin>>numberOFdice;...
|
by: Deathwing |
last post by:
Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your...
|
by: Naya |
last post by:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total??...
|
by: Peter Oliphant |
last post by:
I would like to be able to create a random number generator that produces
evenly distributed random numbers up to given number.
For example, I would like to pick a random number less than 100000,...
|
by: bilgekhan |
last post by:
What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.
What about this method:
srand(time(0));
int r1 = rand();
srand(rand());...
|
by: PureCoffee |
last post by:
So here is some code but I can't get past the generation. I would like to add whatever randomely generated numbers are shown in each "private sub".
Obviously I am a NOOB to VB but I rather enjoy...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |