i really need help!!!
i am a noob, just getting started in c++
and i need help sorting my array.
please don't suggest me std::sort because i don't really know how to use those! sorry i am such a noob,
but i prefer it the simplest way possible please help

{
    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!!!

Recommended Answers

All 5 Replies

hi,
in your code you wrote:

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

as 'i' is index,its value should be 0 to 8 if size of array is 10, so it should be witten like:
for(i=0;i<9;i++)
i find this mistake only.i dont think there is any mistake in swap function.

You should never do input/output in a function, unless it is an input/output function. Otherwise, your sort function is fine. Here is my sort function:

void Sort(int Array[], int NumUsed)
{
    int temp, i, j;
    
    for (i = 0; i < NumUsed - 1; i++)
    {
        int Smallest = i;
        
        for (j = i + 1; j < NumUsed; j++)
        {
            if (Array[j] > Array[i])
            {
                Smallest = j;    
            }
        }
        
        temp = Array[i];
        Array[i] = Array[j];
        Array[j] = temp;
    }    
}

thanks, but how to do bubble sort this?

also, i am keep getting 0 for my highest number, why?

For bubble sort all you have to do is blindly swap :

int A[5] = {5,3,1,7};

//bubble sort
for(int i = 0; i < 5; i++){
   for(int j = i+1; j < 5; j++)
          if(A[i] > A[j] ) std::swap(A[i],A[j]);
}
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.