i have no clue how to sort using the array. it is not 2d.

this is what i have

for(int i=1; i <=num; i++)
{
	do{
	cout <<"enter score " << i << ": ";
	cin >> scores[i];
	if((scores[i] <= 0)||(scores[i] >=100))
		cout << "invalid score\n";
	}while((scores[i] <= 0)||(scores[i] >=100));
//this is where i input the scores.

void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i <scores[9]; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}

void Swap(int scores[], int i)
{
	int temp;
	temp = scores[i];
	scores[i]=scores[i+1];
	scores[i+1] = temp;
}
//this is the part i am having trouble with

the swap my teacher told me does not work.... it may be just me, but i can't get it to work,
please reply fast
thank you!!!

Hi,

I think your problem is actually in the

void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i <scores[9]; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}

for(i= 0; i <scores[9]; i++)

rather I think your want the length of the array, I.e to walk trough the length all the values in the array making the comparisons and swaping. So for this we should rather write

for(i= 0; i <sizeof(scores) / sizeof(int); i++)

Hi,

I think your problem is actually in the

void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i <scores[9]; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}

for(i= 0; i <scores[9]; i++)

rather I think your want the length of the array, I.e to walk trough the length all the values in the array making the comparisons and swaping. So for this we should rather write

for(i= 0; i <sizeof(scores) / sizeof(int); i++)

thank you, it kinda worked, because when i put in my inputs, it will read in 0, and not the highest number.... what is going on?

thank you, it kinda worked, because when i put in my inputs, it will read in 0, and not the highest number.... what is going on?

Hi,

Please may explain it again, I am not sure I follow.

Hi,

Please may explain it again, I am not sure I follow.

void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i < num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}

this is what i have done differently, the num being how many values i had, when i put in 5 for num, and values, 20, 39, 49, 70, and 10,
program prints 0, 10, 20, 39, 49.
why does it do this?

void SortNumbers()
{
cout <<"the sorted list is ";
for(i= 0; i < num; i++)
{
if(scores>scores[i+1])
Swap(scores, i);
cout << scores << " ";
}
cout << endl;
}
this is what i have done differently, the num being how many values i had, when i put in 5 for num, and values, 20, 39, 49, 70, and 10,
program prints 0, 10, 20, 39, 49.
why does it do this?

Hi,

many thanks for the response

If we go back to where you enter the numbers.

for(int i=1; i <=num; i++)
{
	do{
	cout <<"enter score " << i << ": ";
	cin >> scores[i];
	if((scores[i] <= 0)||(scores[i] >=100))
		cout << "invalid score\n";
	}while((scores[i] <= 0)||(scores[i] >=100));
//this is where i input the scores.

Particularly your for(int i=1; i <=num; i++)

void SortNumbers()
{
cout <<"the sorted list is ";
for(i= 0; i < num; i++)
{
if(scores[i]>scores[i+1])
Swap(scores, i);
cout << scores[i] << " ";
}
cout << endl;
}

for(i= 0; i < num; i++)

Notice the two for statements. When you input you are starting at array position 1 and then when printing you are starting at array position 0. Position 0 has nothing so it prints 0.
So change the first for loop int to = 0 as well.

here is what i have done, could you explain little bit clearer, i kind of understand, but being a complete noob, what your saying makes little sense to me.

//*************************** Open Lab Assignment #6***************************
// FILE: ola7.cc
// AUTHOR: Written by Stephen Kim
// INSTRUCTOR: Dr. Chrisila Pettey 
// COURSE:  CSCI 1170 
// DUE DATE : December 1, 2009 
// DESCRIPTION: program that sorts, finds minimum or maximum, or calculates 
// the average of given test scores repeatedly until the user selects quit.
// FORMULA :average = (first + second + third)/3.0;
//*****************************************************************************

#include <iostream>
using namespace std;

void PrePrintMenu();
void PrintMenu();
int GetSelection();
void SortNumbers();
int FindMin();
int FindMax();
float CalculateAverage();
void Swap(int scores[10], int i);

char selection;
int choice;
int num;
int scores[10];
int i;

int main()
{
			/*{
				cout << "invalid selection\n";
				cout << "Please enter a number between 1 and 5: ";
			}*/
	do{
		PrePrintMenu();
		if((selection== 'P')||(selection =='p'))
			PrintMenu();
		else if((selection== 'Q')||(selection =='q'))
		{	cout << "Good bye\n";
			return 0;
		}
		else
			cout <<"Invalid Selection\n";
		}while((selection !='P')||(selection !='p')||(selection !='Q')||(selection !='Q'));
	return 0;
}

void PrePrintMenu()
{
	cout << "Type one of the following\n";
	cout << "P to process test scores\n";
	cout << "Q to quit\n";
	cin >> selection;
}

void PrintMenu()
{

	cout << "Enter the number of tests to process: ";
	cin >> num;
	if((num>0)&&(num<11))
	{		
		
		for(int i=0; i < num; i++)
		{
			do{
			cout <<"enter score " << i << ": ";
			cin >> scores[i];
			if((scores[i] <= 0)||(scores[i] >=100))
				cout << "invalid score\n";
			}while((scores[i] <= 0)||(scores[i] >=100));
		}
		do{
		cout << "What would you like to do?\n";
		cout << "1. Print the scores in ascending order\n";
		cout << "2. Find the minimum\n";
		cout << "3. Find the maximum\n";
		cout << "4. Find the average\n";
		cout << "5. Quit\n";
		cin >> choice;	
		
			if(choice ==1)
				SortNumbers();
			else if(choice ==2)
				FindMin();
			else if(choice ==3)
				FindMax();
			else if(choice ==4)
				cout << "CalculateAverage();";
			else if(choice ==5)
			{	
				cout << "Good bye\n";
				return;
			}
			else 
			{
				cout <<"invalid selection\n";
				cout << endl;
			}
		}while(choice != 5);
		
	
	}

}

void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i <= num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}

		cout << endl;
}

void Swap(int scores[], int i)
{
	int temp;
	temp = scores[i];
	scores[i]=scores[i+1];
	scores[i+1] = temp;

}

int FindMin()
{
	cout <<"The minimum is ";
		for(i= 0; i < num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
		}
	cout << scores[0];
	cout << endl;
}

int FindMax()
{
	cout <<"The maximum is ";
		for(i= 0; i < num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
		}
	cout << scores[i];
	cout << endl;
}

thanks for your help by the way

yes i do get what you are saying, but there is still a problem, when i want to sort, it only sorts two numbers at a time, which means the first and the third won't be sorted, i heard about bubble sort, but don't know how to do it on here.
also, when i plug in 3 numbers such as, 3, 17, 41, i would get 3, 17, 0... complete opposite of what i had before. here is what i did.

void PrintMenu()
{
	cout << "Enter the number of tests to process: ";
	cin >> num;
	if((num>0)&&(num<11))
	{		
		for(int i=0; i < num; i++)
		{
			do{
			cout <<"enter score " << i+1 << ": ";
			cin >> scores[i];
			if((scores[i] <= 0)||(scores[i] >=100))
				cout << "invalid score\n";
			}while((scores[i] <= 0)||(scores[i] >=100));
}
}
void Swap(int scores[], int i)
{
	int temp;
	temp = scores[i];
	scores[i]=scores[i+1];
	scores[i+1] = temp;

}


void SortNumbers()
{
	cout <<"the sorted list is "; 
	for(i= 0; i < num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}

i just don't know what i am doing wrong, please help, also could you tell me how to get the average of the array?

Hi,
So for this we should rather write

for(i= 0; i <sizeof(scores) / sizeof(int); i++)

That does not work once this is not done within the array's scope.

@OP :

just do this :

for i = 0; i < arraySize ++i{
   for j = i+1; j < arraySize; j++
         if array[i] > array[j] 
              swap the two values;
}

This is how you can use the swap function to sort data in your array. Hope this helps.

#include <iostream>



using namespace std;


void swap(int a[],int arraySize)
	{
		int temp=0;
		int counter = 0;
		int secondCounter = 0;
		for(int i=0;i<arraySize;i++)
			{	
			if(a[i] > a[i+1])
			  {
			  	
				temp = a[i];
				a[i] = a[i+1];
				a[i+1] = temp;			
										    
			   }		
				counter++;
				
				if(counter == arraySize )
				{
				 counter = 0;
				 i = -1;
				secondCounter++;
				 if(secondCounter == arraySize)
				   {

					i = arraySize-1;
				   }
				}				
			}

		  
		
	}


int main()
	{
		int a[15] = {2,3,1,5,4,10,19,100,299,0,29,98,900,1000,2000};
		swap(a,sizeof(a)/sizeof(int));
		
		for(int k = 0; k<sizeof(a)/sizeof(int);k++)
		   {
			cout<<a[k]<<endl;

		   }	
		
		return 0;
	}

OutPut:
0
1
2
3
4
5
10
19
29
98
100
299
900
1000
2000

Hi,

Your algorithm is incorrect: Let us assume we want 5 test numbers entered in the order 10, 9, 8 7, 6

So tha array would be like this:

[10] [9] [8] [7] [6] [0] [0] [0] [0] [0] Remember you declared and array of: int scores[10]

void SortNumbers()
{	
cout <<"the sorted list is "; 
	for(i= 0; i <= num; i++)
		{
			if(scores[i]>scores[i+1])
				Swap(scores, i);
			cout << scores[i] << " ";
		}
		cout << endl;
}
void Swap(int scores[], int i)
{
	int temp;
	temp = scores[i];
	scores[i]=scores[i+1];
	scores[i+1] = temp;
}

[10] [9] [8] [7] [6] [0] [0] [0] [0] [0]

Your for loop begins and compares 10 and 9 finding 10 is greater than 9 and thus going int the swap function and swaps giving us this:


[9] [10] [8] [7] [6] [0] [0] [0] [0] [0]

It then exits and returns to the sortNumbers() function and prints
scores[0] which is 9 then i++. It then takes 10 which is now in scores[1] and compares to scores[2] which is 8 and indeed 10 is greater than 8 so back into the swap function to swap and back to sortNumber and print scores[1] which is 8. The array looks like this:

[9] [8] [10] [7] [6] [0] [0] [0] [0] [0]

Let us extrapolate: after 6 the comparison continues because 10 is greater than 0


[9] [8] [7] [6] [0] [0] [10] [0] [0] [0]

So after for(i= 0; i <= num; i++) remember num in this example = 5 so 10 finds itself at position 6 of the array as you have declared an array[10] of 10 usable slots.

So you need to change the algorithm.

Lets forget about a template swap for now.
Then A better swap than the above is this :

void swap(int &left, int &right){
int temp = left;
left = right;
right = temp;
}

Think about why that is.

You obviously did not consider changing the size of the array before posting your reply. of course its going to print out a bunch of zeroes if the size of the array is 15 but the elements are only 5.

i have no clue how to sort using the array. it is not 2d.
FORGET YOUR CODE I M GIVING YOU A SIMPLE
FOR THIS YOU SORT AN ARRAY EASILY

#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int add(int ,int);
int main(int argc, char *argv[])
{
    int array[3],temp,c=1;
    for(int i=0;i<=3;i++)
    {cin>>array[i];}
    for(int j=0;j<=3;j++)
    {
            for(int i=c;i<=3;i++)
            {if(array[j]>array[i])
            {temp=array[j];
            array[j]=array[i];
            array[i]=temp;
            }
            }
            c++;
            
            
            
            
            }
    
    for(int i=0;i<=3;i++)
    cout<<array[i]<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

YOU DON'T UNDERSTAND RPLY ME THANK YOU

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.