Program Error - Sorts array data

Cant fingure out error cause

Page 1 of 1

4 Replies - 978 Views - Last Post: 08 December 2007 - 07:48 PM Rate Topic: -----

#1 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Program Error - Sorts array data

Post icon  Posted 08 December 2007 - 03:20 PM

I am building a application that takes grades that are entered and stores them in a array. It then will find the fallowing:
- Display all grades entered
- Display Low to High
- Display High to low
- Display Highest and Lowest value
- Display Average

I am getting the fallowing errors and I think it might be how the array is being passed to my functions but not sure.
Errors:
code.obj : error LNK2019: unresolved external symbol "void __cdecl average(double)" (?average@@YAXN@Z) referenced in function _main
code.obj : error LNK2019: unresolved external symbol "void __cdecl high_and_low(double)" (?high_and_low@@YAXN@Z) referenced in function _main
code.obj : error LNK2019: unresolved external symbol "void __cdecl high_to_low(double)" (?high_to_low@@YAXN@Z) referenced in function _main
code.obj : error LNK2019: unresolved external symbol "void __cdecl low_to_high(double)" (?low_to_high@@YAXN@Z) referenced in function _main
code.obj : error LNK2019: unresolved external symbol "void __cdecl print_scores(double)" (?print_scores@@YAXN@Z) referenced in function _main

Here is the code I am currently building.
//
//
//

#include <iostream>

using namespace std;

void print_scores(double);
void low_to_high(double);
void high_to_low(double);
void high_and_low(double);
void average(double);

void main()
{
	double scores[50];
	int count=0;
	double score=0;

	cout << "Enter 0 when all grades have been entered" <<endl;

	for (count=0;count<=50;count++)
		{
			cout << "Please enter the grades ";
			cin >> score;
			if (score == 0)
			{
				return;
			}
				if (score < 0)
				{
					while (score < 0)
					{
						cout << "Error - Invalid Score. Please only enter positive scores ";
						cin >> score;
							if (score == 0)
							{
								return;
							}
					}
				}
			scores[count] = score;
		}

	for (count=0;count<=50;count++)
	{
		print_scores(scores[count]);
	}

	for (count=0;count<=50;count++)
	{
		low_to_high(scores[count]);
	}

	for (count=0;count<=50;count++)
	{
		high_to_low(scores[count]);
	}

	for (count=0;count<=50;count++)
	{
		high_and_low(scores[count]);
	}

	for (count=0;count<=50;count++)
	{
		average(scores[count]);
	}

	

}

void print_scores(double scores[])
{
	
	int count=0;

	for (count=0;count<=50;count++)
		{
			cout << scores[count] << " ";
		}
}

void low_to_high(double scores[])
{
	bool swap;  
	double temp;  
	int count=0;

	do
	{	
		swap = false;

	   for (int count=0; count <= 50;count++)

	   {	
		   if (scores[count] > scores[count+1])

			{   
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "Scores from Low to High" <<endl;
	for(count=0;count<=50;count++)
	{
		cout << scores[count];
	}

}

void high_to_low(double scores[])
{
	bool swap;  
	double temp;	
	int count=0;

	do
	{	
		swap = false;

	   for (int count=0; count <= 50;count++)

	   {	
		   if (scores[count] < scores[count+1])

			{   
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "Scores from High to Low" <<endl;
	for(count=0;count<=50;count++)
	{
		cout << scores[count];
	}

}

void high_and_low(double scores[])
{

	double high=0,low=0;
	int count=0; 
	high = scores[0];
	low = scores[0];

	for (count=1;count<=50;count++)
		{
			if(scores[count] > high)
			{
				high = scores[count];
			}
		}

	for (count=1;count<=50;count++)
	{
		if(scores[count] < low)
		{
			low = scores[count];
		}
	}

	cout << "The high score was " << high <<endl;
	cout << "The low score was " << low <<endl;
}

void average(double scores[])
{

	double average=0,total=0;
	int count=0;

	for (count=0;count<=50;count++)
	{
		total += scores[count];
	}
	
	average = total / 50;

	cout << "The average " << average <<endl;

}



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Program Error - Sorts array data

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Program Error - Sorts array data

Posted 08 December 2007 - 03:29 PM

Your functions are prototyped to accept plain doubles as arguments, but they are defined as accepting double arrays. And when you call them, you are passing doubles rather than double arrays.

Pick which you actually want and go from there. From the looks of things, you need to use the array versions as they are defined; you'll need to change the prototypes and the various calls from e.g.:
	for (count=0;count<=50;count++)
	{
		average(scores[count]);
	}

to just
 average(scores);

Was This Post Helpful? 0
  • +
  • -

#3 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: Program Error - Sorts array data

Posted 08 December 2007 - 03:59 PM

View Postjjhaag, on 8 Dec, 2007 - 03:29 PM, said:

Your functions are prototyped to accept plain doubles as arguments, but they are defined as accepting double arrays. And when you call them, you are passing doubles rather than double arrays.

Pick which you actually want and go from there. From the looks of things, you need to use the array versions as they are defined; you'll need to change the prototypes and the various calls from e.g.:
	for (count=0;count<=50;count++)
	{
		average(scores[count]);
	}

to just
 average(scores);


Yeah I realized that just after I posted it and checked everything over a 3rd time. I wanted to update that I corrected the issue but you all reply to quick :P . Thanks

BUT got another issue with my application

So I made the corrections for the last issue and also add a const int variable called size that holds that declares the array size. Did this so it would be more easy for me to modify my code if I ever needed to change it.

So now my issue is I am getting a weird error once I run my program.
Error
Run-Time Check Failure #2 - Stack around the variable 'scores' was corrupted.
I cant figure out were the problem is.

//
//
//

#include <iostream>

using namespace std;

void print_scores(double [],const double);
void low_to_high(double [],const double);
void high_to_low(double [],const double);
void high_and_low(double [],const double);
void average(double [],const double);

void main()
{
	const int size=5;
	double scores[size];
	int count=0;
	double score=0;

	cout << "Enter 0 when all grades have been entered" <<endl;

	for (count=0;count<=size;count++)
		{
			cout << "Please enter the grades ";
			cin >> score;
			if (score == 0)
			{
				break;
			}
				if (score < 0)
				{
					while (score < 0)
					{
						cout << "Error - Invalid Score. Please only enter positive scores ";
						cin >> score;
							if (score == 0)
							{
								break;
							}
					}
				}
			scores[count] = score;
		}

		print_scores(scores,size);
		low_to_high(scores,size);
		high_to_low(scores,size);
		high_and_low(scores,size);
		average(scores,size);


	

}

void print_scores(double scores[],double size)
{
	
	int count=0;

	for (count=0;count<=size;count++)
		{
			cout << scores[count] << " ";
		}
}

void low_to_high(double scores[],double size)
{
	bool swap;  
	double temp;  
	int count=0;

	do
	{	
		swap = false;

	   for (int count=0; count <= size;count++)

	   {	
		   if (scores[count] > scores[count+1])

			{   
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "\nScores from Low to High" <<endl;
	for(count=0;count<=size;count++)
	{
		cout << scores[count] << " ";
	}

}

void high_to_low(double scores[],double size)
{
	bool swap;  
	double temp;	
	int count=0;

	do
	{	
		swap = false;

	   for (int count=0; count <= size;count++)

	   {	
		   if (scores[count] < scores[count+1])

			{   
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "\nScores from High to Low" <<endl;
	for(count=0;count<=size;count++)
	{
		cout << scores[count] << " ";
	}

}

void high_and_low(double scores[],double size)
{
	double high=0,low=0;
	int count=0; 
	high = scores[0];
	low = scores[0];

	for (count=1;count<=size;count++)
		{
			if(scores[count] > high)
			{
				high = scores[count];
			}
		}

	for (count=1;count<=size;count++)
	{
		if(scores[count] < low)
		{
			low = scores[count];
		}
	}
	cout << "\nThe high score was " << high <<endl;
	cout << "The low score was " << low <<endl;
}

void average(double scores[],double size)
{
	double average=0,total=0;
	int count=0;

	for (count=0;count<=size;count++)
	{
		total = total + scores[count];
	}
	
	average = total / size;

	cout << "The average " << average <<endl;

}



Also when you enter data for some reason the low to high function is displaying the wrong information. It gives the numbers with the number 6 missing and a very weird number -9.25596e+061 in front of it all. So its -9.25596e+061 1 2 3 4 5

void low_to_high(double scores[],double size)
{
	bool swap;  
	double temp;  
	int count=0;

	do
	{	
		swap = false;

	   for (int count=0; count <= size;count++)

	   {	
		   if (scores[count] > scores[count+1])

			{   
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "\nScores from Low to High" <<endl;
	for(count=0;count<=size;count++)
	{
		cout << scores[count] << " ";
	}

}


Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Program Error - Sorts array data

Posted 08 December 2007 - 04:29 PM

It looks like you're accessing elements of your array that don't exist. In your various functions, you are looping through the elements using the following:
for (count=0;count<=size;count++) {...

However, you shouldn't be using <= here, just <. The array indices are zero based, so last element of your array scores is found at an index of size-1. So you're actually acessing a garbage value beyond the end of the last element of the array, and when you try to modify it, you're apparently corrupting the stack. Similarly, when you swap values in the sorting functions, you're actually swapping in a garbage value.

Switch the upper limit of your looping control statements to a strictly less-than statement count<size and I believe that it should work.
Was This Post Helpful? 0
  • +
  • -

#5 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: Program Error - Sorts array data

Posted 08 December 2007 - 07:48 PM

Ok so after doing some reading and thinking about my code I have changed directions. I decided to uses vectors to hold my data because it allows me to do what I need. I wanted to have a user enter in scores, but I did not know how many I want the user to enter. So I went with a vector so I can just increase the vector size as I go very easy.

That said I am having a new ever once my program compiles. I am getting Vector subscript out of range.

Can someone help me find the problem/solution

//
//
//

#include <iostream>
#include <vector>

using namespace std;

void print_scores(vector<double>);
void low_to_high(vector<double>);
void high_to_low(vector<double>);
void high_and_low(vector<double>);
void average(vector<double>);

void main()
{
	vector<double> scores;

	int count=0;
	double grade=0;

	cout << "Enter -1 when all grades have been entered" <<endl;

	for (int count=0;count < scores.size();count++)
		{
			cout << "Please enter the grades ";
			cin >> grade;
			if (grade == 0)
			{
				break;
			}
				if (grade < 0)
				{
					while (grade < 0)
					{
						cout << "Error - Invalid Score. Please only enter positive scores ";
						cin >> grade;
							if (grade == 0)
							{
								break;
							}
					}
				}
			scores.push_back(grade);
		}

		print_scores(scores);
		low_to_high(scores);
		high_to_low(scores);
		high_and_low(scores);
		average(scores);


	

}

void print_scores(vector<double> scores)
{
	int count=0;

	for (int count=1;count<scores.size();count++)
		{
			cout << scores[count] << " ";
		}
}

void low_to_high(vector<double> scores)
{
	bool swap;  
	double temp; 
	int count=0;

	do
	{	
		swap = false;

	   for (int count=1; count < scores.size();count++)

	   {	
		   if (scores[count] > scores[count+1])

			{  
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "\nScores from Low to High" <<endl;
	for(int count=1;count<scores.size();count++)
	{
		cout << scores[count] << " ";
	}

}

void high_to_low(vector<double> scores)
{
	bool swap;  
	double temp;   
	int count=0;

	do
	{	
		swap = false;

	   for (int count=1; count < scores.size();count++)

	   {	
		   if (scores[count] < scores[count+1])

			{  
				temp = scores[count];
				scores[count] = scores[count+1];
				scores[count+1] = temp;
				swap = true;

			}

	   }

	} while (swap==true);

	cout << "\nScores from High to Low" <<endl;
	for(count=1;count<scores.size();count++)
	{
		cout << scores[count] << " ";
	}

}

void high_and_low(vector<double> scores)
{
	double high=0,low=0;
	int count=0;
	high = scores[1];
	low = scores[1];

	for (int count=1;count<scores.size();count++)
		{
			if(scores[count] > high)
			{
				high = scores[count];
			}
		}

	for (int count=1;count<scores.size();count++)
	{
		if(scores[count] < low)
		{
			low = scores[count];
		}
	}
	cout << "\nThe high score was " << high <<endl;
	cout << "The low score was " << low <<endl;
}

void average(vector<double> scores)
{
	double average=0,total=0;
	int count=0;

	for (int count=1;count<scores.size();count++)
	{
		total = total + scores[count];
	}
	
	average = total / scores.size();

	cout << "The average " << average <<endl;

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1