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...

Recommended Answers

All 10 Replies

Below is what I have:

#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.

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++)

Ok I 've made the necessary adjustments to the program....but it's only generating 60 numbers.

post new code.

Aslo.....is my location of the colums algorithim correct? I also need to print the numbers in order from high to low.

My new code is as below:

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

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

>> 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.

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

I made a bit of adjustments...regardless of what I put...it still shows a count of 60..though it only prints 10 numbers. Actually the count of numbers was suppose to be between 93 and 139 #'s whose range is between 147 and 206.
Thx for any assistance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.