User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

calculations with random #'s

  #1  
Sep 22nd, 2007
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...
 #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;
} 
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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,697
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 307
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: calculations with random #'s

  #2  
Sep 22nd, 2007
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;
}
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Troubleshooting Re: calculations with random #'s

  #3  
Sep 27th, 2007
Below is what I have:
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15.  
  16. double standardDev(int, int, int);
  17.  
  18. srand ( static_cast<unsigned int> ( time ( 0 ) ) );
  19. int i;
  20. int Low = 147;
  21. int High = 206;
  22. int numbers = 0;//the actual numbers
  23.  
  24. double avg = 0.0;//average of group of numbers
  25.  
  26.  
  27.  
  28.  
  29. int count = 0;
  30. int sum = 0;
  31. int sumsquared = 0;
  32.  
  33.  
  34. for (i=147; i > 146 && i < 207; i++)
  35. {
  36. count += 1;
  37. numbers = rand() % (High - Low + 1)+ Low;
  38. cout << numbers << endl;
  39.  
  40. sum += numbers;
  41. sumsquared += (numbers * numbers);
  42. }
  43. for (i=0; i>146 && i<206; i++)
  44. {
  45. if (i%5==0)
  46. cout<<endl;
  47. cout<<setw(10)<<numbers;
  48. }
  49.  
  50. if (count > 0)
  51. {
  52. avg = ((double) sum / (double) count);
  53. cout << "\n\nCount is: " << count << endl;
  54. printf("Average is: %.2f\n", avg);
  55. printf("Deviation is: %.3f\n\n", standardDev(sum, sumsquared, count));
  56. }
  57. else
  58. {
  59. cout << "No calculation!" << endl;
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. // Function for calculating standard deviation
  66. double standardDev(int sum, int sumsquared, int count) {
  67. double deviation = (double) (sumsquared - ((sum * sum) / count));
  68. deviation = deviation / (double) (count - 1);
  69. double stndDev = sqrt(deviation);
  70. return stndDev;
  71.  
  72.  
  73.  
  74. return 0;
  75. }
  76.  

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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: calculations with random #'s

  #4  
Sep 27th, 2007
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:
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
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: calculations with random #'s

  #5  
Sep 27th, 2007
Ok I 've made the necessary adjustments to the program....but it's only generating 60 numbers.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: calculations with random #'s

  #6  
Sep 27th, 2007
post new code.
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
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: calculations with random #'s

  #7  
Sep 27th, 2007
Aslo.....is my location of the colums algorithim correct? I also need to print the numbers in order from high to low.
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: calculations with random #'s

  #8  
Sep 27th, 2007
My new code is as below:

  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15.  
  16. double standardDev(int, int, int);
  17.  
  18. srand ( static_cast<unsigned int> ( time ( 0 ) ) );
  19. int i,j;
  20. int Low = 147;
  21. int High = 206;
  22. int numbers = 0;//the actual numbers
  23.  
  24. double avg = 0.0;//average of group of numbers
  25.  
  26.  
  27.  
  28.  
  29. int count = 0;
  30. int sum = 0;
  31. int sumsquared = 0;
  32.  
  33.  
  34. for (i=147; i > 146 && i < 207; i++)
  35. {
  36. count += 1;
  37. numbers = rand() % (High - Low + 1)+ Low;
  38. cout << numbers << endl;
  39.  
  40. sum += numbers;
  41. sumsquared += (numbers * numbers);
  42. }
  43. for (i=147; i < 207; i++)
  44. {
  45. if (i%5==0)
  46. cout<<endl;
  47.  
  48. }
  49. {
  50. for (i=0;i>count-1;i++)
  51. for (j=i+1;j>count; j++)
  52. if (count>numbers)
  53. swap (count,numbers);
  54. }
  55.  
  56. if (count > 0)
  57. {
  58. avg = ((double) sum / (double) count);
  59. cout << "\n\nCount is: " << count << endl;
  60. printf("Average is: %.2f\n", avg);
  61. printf("Deviation is: %.3f\n\n", standardDev(sum, sumsquared, count));
  62. }
  63.  
  64. return 0;
  65. }
  66.  
  67. // Function for calculating standard deviation
  68. double standardDev(int sum, int sumsquared, int count) {
  69. double deviation = (double) (sumsquared - ((sum * sum) / count));
  70. deviation = deviation / (double) (count - 1);
  71. double stndDev = sqrt(deviation);
  72. return stndDev;
  73.  
  74.  
  75.  
  76. return 0;
  77. }
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
Last edited by Ancient Dragon : Sep 27th, 2007 at 9:33 am. Reason: add line numbers
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: calculations with random #'s

  #9  
Sep 27th, 2007
>> 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 <.
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
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,442
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Troubleshooting Re: calculations with random #'s

  #10  
Sep 27th, 2007
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC