1

I have been following Alex Allain's book to get a very good understanding of C++. I already knew some basics, but I got stuck as I always do on arrays and sorting algorithms. Anyway, one of the problems he presented was to check if an array is sorted or not. And if its not, sort it... Here is the code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void swap(int array[], int firstindex, int secondindex);
int findsmallel(int array[], int size, int index)
{
    int indexofsmall=index;
    for(int i=index+1; i<size; i++)
    {
        if(array[i]<array[indexofsmall])
            {
                indexofsmall=i;
            }
    }
    return indexofsmall;
}
int findhigh(int array[], int size, int index)
{
    int indexofhigh=index;
    for(int i=index+1; i<size; i++)
    {
        if(array[i]>array[indexofhigh])
            {
                indexofhigh=i;
            }
    }
    return indexofhigh;
}
void sortlow(int array[], int size)
{
    for (int i=0; i<size; i++)
    {
        int index=findsmallel(array, size, i);
        swap(array, index, i);
    }
}
void sorthigh(int array[], int size)
{
    for (int i=0; i<size; i++)
    {
        int index=findhigh(array, size, i);
        swap(array, index, i);
    }
}
void swap(int array[], int firstindex, int secondindex)
{
    int temp=array[firstindex];
    array[firstindex]=array[secondindex];
    array[secondindex]=temp;
}
void displayarray(int array[], int size)
{
    cout<<"{ ";
    for(int i=0; i<size;i++)
    {
        if(i!=0)
        {
        cout<<", ";
        }
    cout<<array[i];
    }
    cout<<" }";
}
int main()
{
    int inputedarray[5];
    cin>>inputedarray[];
    if(inputedarray[4] != sortlow || inputedarray[4] != sorthigh)
    {
        sortlow(inputedarray, 5);
        displayarray(inputedarray, 5);
    }
    else
        cout<<"Array is already sorted."<<endl;
    return 0;
}

Getting two errors about the comparison between a pointer and an integerm when checking the condition. Any help would be greatly appreciated! EDIT: The errors I am getting is: C:\Code Block Projects\Alex Allains Book\Chapter 1\main.cpp|84|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|

And any way to check and see if the arrays are sorted or not? Please? :(

6
  • And please take note, most of this code is literally copy-pasted from the book :(. My mind is still trying to comprehend it! But I must >:( Commented Mar 22, 2013 at 7:32
  • 1
    can you post exact errors Commented Mar 22, 2013 at 7:32
  • Problem is in line 3 of main which makes no sense. Recheck. Commented Mar 22, 2013 at 7:34
  • What is the purpose of if(inputedarray[4] != sortlow || inputedarray[4] != sorthigh)? Commented Mar 22, 2013 at 7:37
  • The statement cin>>inputedarray[5]; is undefined behavior. You're inputting into the sixth element in the five-element array. Commented Mar 22, 2013 at 7:37

3 Answers 3

3

I think the third line of its main is doing a comparison: checking if the last element of array is the smallest/biggest to determine if the array is sorted. Although it's not the correct way, let's just assume it's doing so. Change your code from

 if(inputedarray[4] != sortlow || inputedarray[4] != sorthigh)

to

 if(inputedarray[4] != findsmallel(inputedarray,5,0) ||
        inputedarray[4] != findhigh(inputedarray,5,0))

Then you should be able to compile your code.

To get your code to work. Modify main as following:

int main()
{
    int inputedarray[5];
    for( int i=0; i<5; i++)
    {
        cin>>inputedarray[i];
    }

    if(inputedarray[4] != findsmallel(inputedarray,5,0) ||
       inputedarray[4] != findhigh(inputedarray,5,0))
    {
        sortlow(inputedarray, 5);
        displayarray(inputedarray, 5);
    }
    else
        cout<<"Array is already sorted."<<endl;
    return 0;
}

Then you should input your integers like this

2 3 5 1 4

It should work fine unless you put the biggest/smallest integer as the last input digit.

Sign up to request clarification or add additional context in comments.

Comments

1

The "errors" you get is because you are comparing a value with a function pointer. You do not call those functions.

As noted by kma, you can't use these function calls in expressions, as they return void, i.e. do not return a value at all.

Even if the did return something, the first call could rearrange the array so what was previous at index four of the array will not be the same in the next comparison.

1 Comment

Calling would not make sense either, the functions in question is declared to return void.
0

sortlow and sorthigh in your main program are not declared int variables, but function names. C++ take this as pointers (adress) to the functions. But you compare those to integers from the arrays.

Edit: How to check if an array is sorted? Imagine you have an array of 5 cards (from a normal deck of cards) face down in front of you. You can only turn two cards at a time and compare them and put them back face down. How do you determine if the 5 cards are in order? Write down the steps you take. Once you figured out that algorithm you can start thinking about how to express it in C++ code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.