•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,606 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,648 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 785 | Replies: 10 | Solved
![]() |
Good day guys:
The assignment is :
Write a program that will generate an unknown (between 93 & 139) count of random numbers whose values fall between 147 and 206. Calculate the average to 2 decimal places and the standard deviation (3 decimal places). On the monitor, display only the count of numbers, the average and the standard deviation. On the printer, display the same information after you have listed all the numbers in 5 columns in order from high to low and then skipped two spaces.
So far, this is what I've got. Thank you and have agood day. Any assistance is sincerely appreciated...
1. It's saying that 'numbers' is undeclared. I dont know why..isn't it declared ok?
2. How do I calcualte the sum, since it's going to change every time i run the program?
3. The way that I have the 'sqrt' function to calcualte the standard deviation...is it ok?
Thanks much. Please also take a look at my previous post...
The assignment is :
Write a program that will generate an unknown (between 93 & 139) count of random numbers whose values fall between 147 and 206. Calculate the average to 2 decimal places and the standard deviation (3 decimal places). On the monitor, display only the count of numbers, the average and the standard deviation. On the printer, display the same information after you have listed all the numbers in 5 columns in order from high to low and then skipped two spaces.
So far, this is what I've got. Thank you and have agood day. Any assistance is sincerely appreciated...
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
srand ( static_cast<unsigned int> ( time ( 0 ) ) );
int i;
int Low = 147;
int High = 206;
double numbers;//the actual numnbers
double avg;//average of group of numbers
double sdeva;// calcualtion parts of standard deviation
double sdevb;//actual square root for standard deviation
float count;//amout of numbers
int sum;//sum of the numbers
for (i=0; i<93 && i>139; i++)
{
numbers = rand()%(High-Low+1)+Low;
cout<<numbers<<endl;
}
{
if (i%5 ==0)
cout<<endl;
cout<<setw(10)<<numbers;
}
avg = numbers/count;
cout<<avg<<endl;
sdeva = ((sum * sum)/count) - ((sum * sum)/count);
sdevb = sqrt(sdeva);
cout<<sdevb;
return 0;
} 2. How do I calcualte the sum, since it's going to change every time i run the program?
3. The way that I have the 'sqrt' function to calcualte the standard deviation...is it ok?
Thanks much. Please also take a look at my previous post...
Random numbers within a range:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
{ <--Why is that there
if (i%5 ==0)
cout<<endl;
cout<<setw(10)<<numbers;
}
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
{ <--Why is that there
if (i%5 ==0)
cout<<endl;
cout<<setw(10)<<numbers;
}
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
Below is what I have:
A few things:
1. For some reason...it's not printing the numbers in the 5 columns that I want to.
2. I want it randomly print between 93 and 139 mnumbers...the values are ok..between 147 & 206...but not the quantity (between 93 & 139)..thx for assistance.
cplusplus Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> #include <ctime> using namespace std; int main() { double standardDev(int, int, int); srand ( static_cast<unsigned int> ( time ( 0 ) ) ); int i; int Low = 147; int High = 206; int numbers = 0;//the actual numbers double avg = 0.0;//average of group of numbers int count = 0; int sum = 0; int sumsquared = 0; for (i=147; i > 146 && i < 207; i++) { count += 1; numbers = rand() % (High - Low + 1)+ Low; cout << numbers << endl; sum += numbers; sumsquared += (numbers * numbers); } for (i=0; i>146 && i<206; i++) { if (i%5==0) cout<<endl; cout<<setw(10)<<numbers; } if (count > 0) { avg = ((double) sum / (double) count); cout << "\n\nCount is: " << count << endl; printf("Average is: %.2f\n", avg); printf("Deviation is: %.3f\n\n", standardDev(sum, sumsquared, count)); } else { cout << "No calculation!" << endl; } return 0; } // Function for calculating standard deviation double standardDev(int sum, int sumsquared, int count) { double deviation = (double) (sumsquared - ((sum * sum) / count)); deviation = deviation / (double) (count - 1); double stndDev = sqrt(deviation); return stndDev; return 0; }
A few things:
1. For some reason...it's not printing the numbers in the 5 columns that I want to.
2. I want it randomly print between 93 and 139 mnumbers...the values are ok..between 147 & 206...but not the quantity (between 93 & 139)..thx for assistance.
Last edited by Ancient Dragon : Sep 27th, 2007 at 8:48 am. Reason: add line numbers to code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation:
Rep Power: 36
Solved Threads: 867
look at line 43 -- the condition is wrong because i is initialized to 0, so it will never be greater than 146. And why do you want that loop to print the same value for numbers so many times? numbers never changes value within that loop.
[edit]line 34 is screwy too -- since i is initialized to 147 there is no need to test that it is greater than 146. Just simplify to this:
[edit]line 34 is screwy too -- since i is initialized to 147 there is no need to test that it is greater than 146. Just simplify to this:
for (i=147; i < 207; i++)
Last edited by Ancient Dragon : Sep 27th, 2007 at 8:54 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation:
Rep Power: 36
Solved Threads: 867
My new code is as below:
1. It's not generating more than 60 numbers
2. It's not being printed in 5 columns.
3. Is my bubble sort algorithm ok (it's suppose to print the numbers from high to low)?
Thx
cplusplus Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> #include <ctime> using namespace std; int main() { double standardDev(int, int, int); srand ( static_cast<unsigned int> ( time ( 0 ) ) ); int i,j; int Low = 147; int High = 206; int numbers = 0;//the actual numbers double avg = 0.0;//average of group of numbers int count = 0; int sum = 0; int sumsquared = 0; for (i=147; i > 146 && i < 207; i++) { count += 1; numbers = rand() % (High - Low + 1)+ Low; cout << numbers << endl; sum += numbers; sumsquared += (numbers * numbers); } for (i=147; i < 207; i++) { if (i%5==0) cout<<endl; } { for (i=0;i>count-1;i++) for (j=i+1;j>count; j++) if (count>numbers) swap (count,numbers); } if (count > 0) { avg = ((double) sum / (double) count); cout << "\n\nCount is: " << count << endl; printf("Average is: %.2f\n", avg); printf("Deviation is: %.3f\n\n", standardDev(sum, sumsquared, count)); } return 0; } // Function for calculating standard deviation double standardDev(int sum, int sumsquared, int count) { double deviation = (double) (sumsquared - ((sum * sum) / count)); deviation = deviation / (double) (count - 1); double stndDev = sqrt(deviation); return stndDev; return 0; }
2. It's not being printed in 5 columns.
3. Is my bubble sort algorithm ok (it's suppose to print the numbers from high to low)?
Thx
Last edited by Ancient Dragon : Sep 27th, 2007 at 9:33 am. Reason: add line numbers
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation:
Rep Power: 36
Solved Threads: 867
>> It's not generating more than 60 numbers
That's because 207-146-1 = 60 (see line 34)
What's the purpose of the loop at lines 43-48 ? All it does is print several line feeds and you can do that without a loop.
>>It's not being printed in 5 columns.
probably because of the endl at the end of line 38. Looks like you need to move lines 45 and 46 up to line 39, then delete lines 43-48.
>> Is my bubble sort algorithm ok
No. What is this supposed to be sorting? both count and numbers are just simple integers. Only arrays can be sorted. If you want the numbers to be displayed in sorted order then you will have to make numbers and array of at least 60 integers.
Line 50, you are using the wrong comparison operator -- replace > with <.
Same problem at line 51.
You should also be more liberal with braces. Lines 42-49 should be coded like this:
Note that you do NOT want the braces at lines 49 and 54 of the code you posted.
That's because 207-146-1 = 60 (see line 34)
What's the purpose of the loop at lines 43-48 ? All it does is print several line feeds and you can do that without a loop.
>>It's not being printed in 5 columns.
probably because of the endl at the end of line 38. Looks like you need to move lines 45 and 46 up to line 39, then delete lines 43-48.
>> Is my bubble sort algorithm ok
No. What is this supposed to be sorting? both count and numbers are just simple integers. Only arrays can be sorted. If you want the numbers to be displayed in sorted order then you will have to make numbers and array of at least 60 integers.
Line 50, you are using the wrong comparison operator -- replace > with <.
for (i=0;i < count-1;i++)
Same problem at line 51.
You should also be more liberal with braces. Lines 42-49 should be coded like this:
for (i=0;i<count-1;i++)
{
for (j=i+1;j<count; j++)
{
if (count>numbers)
swap (count,numbers);
}
}Note that you do NOT want the braces at lines 49 and 54 of the code you posted.
Last edited by Ancient Dragon : Sep 27th, 2007 at 9:48 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Oh goodness...now it prints only 12 numbers...but still says that the 'count' is 60. My revised code:
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
double standardDev(int, int, int);
srand ( static_cast<unsigned int> ( time ( 0 ) ) );
int i,j;
int Low = 147;
int High = 206;
int numbers = 0;//the actual numbers
double avg = 0.0;//average of group of numbers
int count = 0;
int sum = 0;
int sumsquared = 0;
for (i=147; i < 207; i++)
{
count += 1;
numbers = rand() % (High - Low + 1)+ Low;
if (i%5==0)
cout << numbers << endl;
sum += numbers;
sumsquared += (numbers * numbers);
}
for (i=0;i>count-1;i++)
for (j=i+1;j>count; j++)
if (count>numbers)
swap (count,numbers);
if (count > 0)
{
avg = ((double) sum / (double) count);
cout << "\n\nCount is: " << count << endl;
printf("Average is: %.2f\n", avg);
printf("Deviation is: %.3f\n\n", standardDev(sum, sumsquared, count));
}
return 0;
}
// Function for calculating standard deviation
double standardDev(int sum, int sumsquared, int count) {
double deviation = (double) (sumsquared - ((sum * sum) / count));
deviation = deviation / (double) (count - 1);
double stndDev = sqrt(deviation);
return stndDev;
return 0;
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Random number generator's (C++)
- Getting a non-random number? (C++)
- creating random numbers (C)
- Calculator program returning random values (Assembly)
- random number generator and calculations (Java)
- Random Restarts (Windows NT / 2000 / XP / 2003)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Urgent solution needed
- Next Thread: An introduction and a few questions..



Linear Mode