Hello, I am working on a program that takes an integer array filled from a random number generator. These values are suppose to be from -5000 to 5000, 2500 of them. Then I want to find the standard deviation of the numbers in the array. I am required to use and integer array, calculate the average, returns the average which is a floating point value. Then I calculate the standard dev. I am having a problem with casting certain values. I am suppose to use integers and floats and I can't get it to work. I pretty sure I am suppose to use static cast but it wasn't working so I took it out. Any help would be greatly appreciated, and imput would be great too. I really want to get this working, thank you.


heres my code:


Driver

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Driver.cpp

#include <iostream>
#include <iomanip>

#include "Functions.h"
using namespace std;

int main()
{
	// Declared Variables
	int Numbers[2500];
	int Total = 2500;
	float StandardDev, Mean;

	// Program description, program name, & author info.
	cout <<  "\n -----------------------------------------------------  \n";
	cout <<  "\n	C++ STATISTICAL ANALYSIS via STANDARD DEVIATION     \n"; 
	cout <<  "\n -----------------------------------------------------  \n";
	cout << setw(15) <<  "\n	AUTHOR:	 "	   << setw(15) << "	Page Lynn Potter ";
	cout << setw(17) <<  "\n	CLASS:	 "     << setw(5)  << "	CIS 2275  ";
	cout << setw(10) <<  "\n	ASSIGNMENT: "  << setw(20) << "	QUIZ #1 \n";
	cout <<  "\n -----------------------------------------------------  \n";

	cout << "\n This program computes two statistical values for an array "
		 << "\n of 2500 values rainging in value from -5000 to +5000. ";
	cout << "\n The sum and average of the values will be calculated, "
		 << "\n and diplayed for viewing as well as the standard deviation. \n";

	FillArray(Numbers, Total);

	float Average = AveArray(Numbers, Total);
	
	cout << setw(25) << "\n RESULTS \n";
	cout <<  "\n -----------------------------------------------------  \n";
	
	cout.precision(4);
	cout.setf(ios::fixed);

	cout << "\n AVERAGE =  " << setw(10) << Mean << "\n";

	float StandardDeviation = StdDeviation(Numbers, Total, Mean);

	cout << "\n STANDARD DEVIATION:  " << setw(10) << StandardDev << "\n";

	return (0);
}

Functions

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.cpp

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void FillArray(int Numbers[], int Total)
{
	srand(123);
	
	for (int i = 0; i < Total; i++)
		{ 
			// Gives me 0 - 10000
			Numbers[i] = ((rand()%10000 + 1) - 5000);
		}

}

float AveArray(int Numbers[], int Total)
{
	// Declared Variables
	int sum = 0;
	
	// Just to make sure there isn't any junk values.
	float Mean = 0,0;
	
	for (int i = 0; i < Total; ++i)
		{
			sum += Numbers[i];
		}
		
	
	Mean = sum/Total;

	return Mean;
}

float StandardDev(int Numbers[], int Total, float Mean)
{
	// Declared Variables
	float StandardDev = 0.0;
	int Sum2 = 0;

	for (int i = 0; i < Total; ++i)
		{
			Sum2 += pow((Numbers[i] - Mean), 2);
		}
   
	StandardDev = sqrt(Sum2 / (Total - 1));

	return StandardDev;
}

Header

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.h

#include <iostream>

using namespace std;

/* The FillArray() is passed the integer array and total number
   (size) of the array. It then uses rand() to fill the array
   with values from -5000 to +5000. */
void FillArray(int Numbers[], int Total);

/* The AveArray() calculates the average (mean) of the array. 
   It is passed the array, total number (size) and returns, a 
   floating point value. */
float AveArray(int Numbers[], int Total);

/* The StandardDec() is passed the array, array size, and the
   average (mean) value. It then calculates and returns the 
   standard deviation. */
float StandardDev(int Numbers[], int Total, float Mean);

Recommended Answers

All 19 Replies

When you are debugging, use fewer numbers and make them non-random. Solve a problem at a time, then go back to the real assignment. You say you are having a problem casting numbers. What line number and what is the problem? Have a short array like {1,2,3,4,5}, where you can calculate the sum, the mean, and the standard deviation easily and compare. Put some debugging code in there to figure out exactly where the problem starts (i.e. reading in the array, calculating the sum, calculating the mean, calculating the standard deviation), then go from there. Be more specific about what the problem is. Does it not compile? Compile, but give a run-time error? Run to completion, but give bad results?

commented: All excellent points +19

It doesn't even compile, I keep getting truncations.

One thing in your code struck me almost immediately. In functions.cpp you haven't included functions.h, which is most likely the root cause of your problem!

Cheers for now,
Jas.

[edit] P.s. I think you also need to include stdlib.h for srand, but I could be wrong!

In your AveArray method:

float AveArray(int Numbers[], int Total)
{
	// Declared Variables
	int sum = 0;      //INT 
	
	// Just to make sure there isn't any junk values.
	float Mean = 0,0;
	
	for (int i = 0; i < Total; ++i)
		{
			sum += Numbers[i];  //INT
		}
		
	
	Mean = sum/Total;   // = INT/INT   **
                      
	return Mean;
}

** the result of integer by integer division is another integer (so truncated or 0 if numerator <denominator) which is then assigned to the float value.

Instead, cast one or both integers to a float: Mean = (float)sum/Total; or Mean = (float)sum/(float)Total; You're going to run into a similar problem with sum2 in the next one where you're trying to cram down the result of pow into your int. Making sum2 a float will make that situation better and it will ensure that you don't have the same truncation problems in that method too.

I have one error now..... What does this mean?
: error C2064: term does not evaluate to a function taking 3 arguments


Heres my edited code......
Driver

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Driver.cpp

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

#include "Functions.h"

int main()
{
	// Declared Variables
	int Numbers[2500];
	int Total = 2500;
	float Mean;

	// Program description, program name, & author info.
	cout <<  "\n -----------------------------------------------------  \n";
	cout <<  "\n	C++ STATISTICAL ANALYSIS via STANDARD DEVIATION     \n"; 
	cout <<  "\n -----------------------------------------------------  \n";
	cout << setw(15) <<  "\n	AUTHOR:	 "	   << setw(15) << "	Page Lynn Potter ";
	cout << setw(17) <<  "\n	CLASS:	 "     << setw(5)  << "	CIS 2275  ";
	cout << setw(10) <<  "\n	ASSIGNMENT: "  << setw(20) << "	QUIZ #1 \n";
	cout <<  "\n -----------------------------------------------------  \n";

	cout << "\n This program computes two statistical values for an array "
		 << "\n of 2500 values rainging in value from -5000 to +5000. ";
	cout << "\n The sum and average of the values will be calculated, "
		 << "\n and diplayed for viewing as well as the standard deviation. \n";

	FillArray(Numbers, Total);

	float Average = AveArray(Numbers, Total);
	
	cout << setw(25) << "\n RESULTS \n";
	cout <<  "\n -----------------------------------------------------  \n";
	
	cout.precision(4);
	cout.setf(ios::fixed);

	cout << "\n AVERAGE =  " << setw(10) << Mean << "\n";

    float StandardDev = StandardDev(Numbers, Total, Mean);

	cout << "\n STANDARD DEVIATION:  " << setw(10) << StandardDev << "\n";

	return (0);
}

Function

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.cpp

#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>

using namespace std;

void FillArray(int Numbers[], int Total)
{
	srand(123);
	
	for (int i = 0; i < Total; i++)
		{ 
			// Gives me 0 - 10000
			Numbers[i] = ((rand()%10000 + 1) - 5000);
		}

}

float AveArray(int Numbers[], int Total)
{
	// Declared Variables
	int sum = 0;
	
	// Just to make sure there isn't any junk values.
	float Mean = 0.0;
	
	for (int i = 0; i < Total; ++i)
		{
			sum += Numbers[i];
		}
		
	Mean = (float)sum/(float)Total; 

	return Mean;
}

float StandardDev(int Numbers[], float Total, float Mean)
{
	// Declared Variables
	float StandardDev = 0.0;
	float Sum2 = 0.0;

	for (int i = 0; i < Total; ++i)
		{
			Sum2 += pow((Numbers[i] - Mean), 2);
		}
   
	StandardDev = sqrt(Sum2 / (Total - 1));

	return StandardDev;
}

Header

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.h

#include <iostream>

using namespace std;

/* The FillArray() is passed the integer array and total number
   (size) of the array. It then uses rand() to fill the array
   with values from -5000 to +5000. */
void FillArray(int Numbers[], int Total);

/* The AveArray() calculates the average (mean) of the array. 
   It is passed the array, total number (size) and returns, a 
   floating point value. */
float AveArray(int Numbers[], int Total);

/* The StandardDec() is passed the array, array size, and the
   average (mean) value. It then calculates and returns the 
   standard deviation. */
float StandardDev(int Numbers[], float Total, float Mean);

I have one error now..... What does this mean?
: error C2064: term does not evaluate to a function taking 3 arguments

Something in your code is specified as a function with 3 arguments, but isn't a function with 3 arguments.

Something in your code is specified as a function with 3 arguments, but isn't a function with 3 arguments.

I know that, but I don't see why the error is there, and what to do to fix it..... Thank you

Sorry I am kinda new to this C++ stuff

I am now getting an error with my calculations. It complies with no errors but the calculations are wrong.

Let me know what you think....

Driver

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Driver.cpp

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

#include "Functions.h"

int main()
{
	// Declared Variables
	int Numbers[2500];
	int Total = 2500;
	float Mean = 0.0;

	
	char ENTER;

	// Program description, program name, & author info.
	cout <<  "\n -----------------------------------------------------  \n";
	cout <<  "\n	C++ STATISTICAL ANALYSIS via STANDARD DEVIATION     \n"; 
	cout <<  "\n -----------------------------------------------------  \n";
	cout << setw(15) <<  "\n	AUTHOR:	 "	   << setw(15) << "	Page Lynn Potter ";
	cout << setw(17) <<  "\n	CLASS:	 "     << setw(5)  << "	CIS 2275  ";
	cout << setw(10) <<  "\n	ASSIGNMENT: "  << setw(5)  << "	QUIZ #1 \n";
	cout <<  "\n -----------------------------------------------------  \n";

	cout << "\n This program computes two statistical values for an array "
		 << "\n of 2500 values rainging in value from -5000 to +5000. ";
	cout << "\n The sum and average of the values will be calculated, "
		 << "\n and diplayed for viewing as well as the standard deviation. \n";
	cout << "\n -----------------------------------------------------  \n";
	cout << "\n	PLEASE HIT ENTER TO CONTINUE... \n";
	cin.get(ENTER);

	FillArray(Numbers, Total);

	float MeanValue = AveArray(Numbers, Total);
	
	cout << "\n -----------------------------------------------------  \n";
	cout << "\n	----RESULTS----										   \n";

	
	cout.precision(4);
	cout.setf(ios::fixed);

	cout << "\n AVERAGE =  " << Mean << "\n";


    float StandardDeviation = StandardDev(Numbers, Total, Mean);

	cout << "\n STANDARD DEVIATION =  " << StandardDeviation << "\n\n";

	return (0);
}

Functions

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.cpp

#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>

using namespace std;

void FillArray(int Numbers[], int Total)
{
	srand(123);
	
	for (int i = 0; i < Total; i++)
		{ 
			// Gives me 0 - 10000
			Numbers[i] = ((rand()%10000 + 1) - 5000);
		}

}

float AveArray(int Numbers[], int Total)
{
	// Declared Variables
	float sum = 0.0;
	
	// Just to make sure there isn't any junk values.
	float Mean = 0.0;
	
	for (int i = 0; i < Total; ++i)
		{
			sum = sum + Numbers[i];
		}
		
	Mean = (float)sum/(float)Total; 

	return Mean;
}

float StandardDev(int Numbers[], float Total, float Mean)
{
	// Declared Variables
	float StandardDeviation = 0.0;
	float Sum2 = 0.0;

	for (int i = 0; i < Total; ++i)
		{
			Sum2 += ((Numbers[i] - Mean) * (Numbers[i] - Mean));
		}
   
	StandardDeviation = sqrt(Sum2 / (Total - 1));

	return StandardDeviation;
}

Header

//  AUTHOR:		Page Lynn Potter
//   CLASS:		CIS 2275 C++ II
// PROGRAM:		Quiz #1  |  C++ ...Standard Deviation
//  E-MAIL:		ppotter03@inbox.com
//    FILE:		Functions.h

#include <iostream>

using namespace std;

/* The FillArray() is passed the integer array and total number
   (size) of the array. It then uses rand() to fill the array
   with values from -5000 to +5000. */
void FillArray(int Numbers[], int Total);

/* The AveArray() calculates the average (mean) of the array. 
   It is passed the array, total number (size) and returns, a 
   floating point value. */
float AveArray(int Numbers[], int Total);

/* The StandardDec() is passed the array, array size, and the
   average (mean) value. It then calculates and returns the 
   standard deviation. */
float StandardDev(int Numbers[], float Total, float Mean);

I have always been rusty at math, so any suggestions are very welcome. I really appreciate all the help, thank you guys once again.....

Did you try testing your methods with a 4-5 element array like VernonDozier had indicated? Try that first. I know it may seem like a pain to break your code down (just copy and paste relevant portions over) but it will save you a lot of time in the long run.

Then, failing that, are the numbers huge and/or way out of proportion? In that case something may not have been initialized properly.

Did you try testing your methods with a 4-5 element array like VernonDozier had indicated? Try that first. I know it may seem like a pain to break your code down (just copy and paste relevant portions over) but it will save you a lot of time in the long run.

Then, failing that, are the numbers huge and/or way out of proportion? In that case something may not have been initialized properly.

Ok I will try that again...... What if that doesn't work? I was wondering if there is an error (Logical) with my std.Dev formula? Does that formula look proper?

int numbers[] = {10,20,30,50,70};
	float mean = AveArray(numbers,5);
	std::cout <<mean<<std::endl;
	float stddev = StandardDev(numbers,5,mean);
	std::cout <<stddev<<std::endl;
36
24.0832

Checks out on Windows Calc

And why do you refuse to give us any important information -- like what was the answer you wanted, what was the answer you got, where in the 200 lines of code you think the error is?

Get with it -- we can't read you screen. You need to give us the information that explains the problem, not just "there's a problem somewhere - what is it?"

And why do you refuse to give us any important information -- like what was the answer you wanted, what was the answer you got, where in the 200 lines of code you think the error is?

Get with it -- we can't read you screen. You need to give us the information that explains the problem, not just "there's a problem somewhere - what is it?"

WaltP-
That is not very nice, that is the easiest way to discourage a learner. If you don't like helping than don't even bother to read the darn thing. I just specified that my math has an error and I apologize for inconveniencing your wonderful day.

int numbers[] = {10,20,30,50,70};
	float mean = AveArray(numbers,5);
	std::cout <<mean<<std::endl;
	float stddev = StandardDev(numbers,5,mean);
	std::cout <<stddev<<std::endl;
36
24.0832

Checks out on Windows Calc

Thank you, I will check it out right now....

Hello here!

Change your random-generating code to this:

// And use srand(time(NULL));
srand(time(NULL));
Numbers[i] = (rand()%10000) - 5000;

so if the Numbers is 10000 it'll be 10000-5000 = 5000, and if 0 then it'll be 0-5000 = -5000...

Regards,,,
Kimo

Hello here!

Change your random-generating code to this:

// And use srand(time(NULL));
srand(time(NULL));
Numbers[i] = (rand()%10000) - 5000;

so if the Numbers is 10000 it'll be 10000-5000 = 5000, and if 0 then it'll be 0-5000 = -5000...

Regards,,,
Kimo

Oh, that makes sense .... Thanks

There is a very basic problem that you have in main(). Now if you go through it carefully, you'll notice that there are float Mean and float MeanValue trying to serve the same purpose.

int numbers[] = {10,20,30,50,70};
	float mean = AveArray(numbers,5);
	std::cout <<mean<<std::endl;
	float stddev = StandardDev(numbers,5,mean);
	std::cout <<stddev<<std::endl;
36
24.0832

Checks out on Windows Calc

Yes the mean is right here, but i can't calculate the deviation myself or on the windows calculator,,,
When i calculated it, i got 22.*something* :icon_biggrin:

anyway congratulations to ppotter :)
i think it seems to be working now...

Regards,,,
Kimo

Yes the mean is right here, but i can't calculate the deviation myself or on the windows calculator,,,
When i calculated it, i got 22.*something* :icon_biggrin:

anyway congratulations to ppotter :)
i think it seems to be working now...

Regards,,,
Kimo

I got the value to show.... Thanks guys I appreciate it.

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.