Link to home
Start Free TrialLog in
Avatar of mayan1
mayan1

asked on

Sorting an Array

Hello, I am currently playing around in Dev C++ and would like to know how to I would like to create a function that is called from my main function that allows the user to enter 5 integers.   Once the integers are entered it should print the array out to the console.  

After printing the array in the order the integers were entered I should call a function to sort the array and then for a second time print the array, which now should display the integers in ascending sorted order.

I want to use this to sort my array if possible:

   void selection_sort(int a[], int length)
        {
               for (int count = 0 ; count < length - 1 ; count++)
                       swap(a[count],a[minimum_from(a,count,length)]);
        }
       
        int minimum_from(int a[], int position, int length)
        {
               int min_index = position;
       
               for (int count = position + 1 ; count < length ; count
++)
                       if (a[count] < a[min_index])
                               min_index = count;
                       return min_index;
        }
       
        void swap(int& first, int& second)
        {
               int temp = first;
               first = second;
               second = temp;
        }


Avatar of mayan1
mayan1

ASKER

In addition could you please comment the code so that I know what it is doing?
>>>>  I am currently playing around in Dev C++

Actually, the requirements are typical for homework. Here in EE we can't help if there isn't at least an attempt to solve the problem by the asker himself. The posted sort functions cannot be honoured as they are not self-written but copied from somewhere.

Please post code written by yourself. You may start by using the following:

// arraysort.cpp
#include <iostream>
#include <string>
using namespace std;


int main()
{
    // put here code to prompt for 5 integers

    // put here code to sort the array entered

    // put here code to print the sorted array

    return 0;
}


Regards, Alex

Avatar of mayan1

ASKER

Sorry for the wrong information being attached please disregard that I was not trying to post anyone elses code.

Here is my code thus far I am lost:

#include <iostream>
#include <string>
using namespace std;


int main()
{
                cout << "Enter 5 INTEGERS ONLY \n";
                        for (int a=0; a<5; a++)
                        cin >> array[a];
                cout << endl;

    sort(a, a+5);
   
    for (int i=0; i<5; i++)
        cout << a[i] << " ";


 

    return 0;
}
>> array[a];
What is the type of array.
To able to use the std::sort function in C++ you need to use containers.

It should be something like.

vector<int> array;
array.resize(5);

Then sort can be called this way:

sort(array.begin(), array.end());

Rest of you code is fine.

int main()
{
               vector<int> array;
      array.resize(5);
      
                cout << "Enter 5 INTEGERS ONLY \n";
                        for (int a=0; a<5; a++)
                        cin >> array[a];
                cout << endl;

    //sort(a, a+5); This will raise compilation error.
   sort(array.begin(), array.end()); // This is correct.
   
    for (int i=0; i<5; i++)
        cout << a[i] << " ";


 

    return 0;
}
Hello,

Put all the code in the question in between "using namespace std" and "int main()". Keep in mind that a function cannot call another function unless it has already been defined. For example, your call to sort(a, a + 5) needs a definition for sort and it needs to be placed before main() and not after it. Once you've got it compiled, you can start off by calling each function and figure out what they do:

int array[5];  

// place cin cout stuff here

int iNum1 = 1, iNum2 = 2, iResult;

selection_sort( array, 5 );
swap( iNum1, iNum2 );
iResult = minimum_from( array, iNum1, iNum2 );

-Ray
Avatar of mayan1

ASKER

I am getting the following errors:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c SortArray.cpp -o SortArray.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"  

SortArray.cpp: In function `int main()':
SortArray.cpp:8: error: `vector' undeclared (first use this function)
SortArray.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
SortArray.cpp:8: error: expected primary-expression before "int"

SortArray.cpp:8: error: expected `;' before "int"
SortArray.cpp:9: error: `array' undeclared (first use this function)
SortArray.cpp:19: error: name lookup of `a' changed for new ISO `for' scoping
SortArray.cpp:12: error:   using obsolete binding at `a'
SortArray.cpp:19: error: invalid types `int[int]' for array subscript

make.exe: *** [SortArray.o] Error 1

Execution terminated

What did  I do wrong?

#include <iostream>
#include <string>
using namespace std;


int main()
{
     vector<int> array;
     array.resize(5);
     
                cout << "Enter 5 INTEGERS ONLY \n";
                        for (int a=0; a<5; a++)
                        cin >> array[a];
                cout << endl;
 
   sort(array.begin(), array.end()); // This is correct.
   
    for (int i=0; i<5; i++)
        cout << a[i] << " ";


 

    return 0;
}
>>>> SortArray.cpp:8: error: `vector' undeclared (first use this function)

You need to include std::vector template class

#include <vector>


Note if using std::vector you need to change the sort functions posted above by

   void selection_sort(std::vector<int>& a, int length)
        {
               for (int count = 0 ; count < length - 1 ; count++)
                       swap(a[count],a[minimum_from(a,count,length)]);
        }
       
        int minimum_from(std::vector<int>& a, int position, int length)
        {
               int min_index = position;
       
               for (int count = position + 1 ; count < length ; ++count)
                       if (a[count] < a[min_index])
                               min_index = count;
                       return min_index;
        }
       
        void swap(int& first, int& second)
        {
               int temp = first;
               first = second;
               second = temp;
        }

FYI: std::vector is a template class from STL (standard template library) which is part of C++ standard since 1996. It is supposed to fully replace old C arrays where you have to care for allocation and bounds checking yourself. So,

   int a[5] = { 0 };  

would turn to

   std::vector<int> a(5, 0);

Note, you can omit std:: prefix by using the statement  

   using namespace std;

However that never should be done in the header files but in the cpp files only.

Regards, Alex






Avatar of mayan1

ASKER

I am still having a hard time understanding.  Here is my code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int array[5];


int main()
{
     vector<int> array;
     array.resize(5);
     
                cout << "Enter 5 INTEGERS ONLY \n";
                        for (int a=0; a<5; a++)
                        cin >> array[a];
                cout << endl;
 
   sort(array.begin(), array.end());
   
    for (int i=0; i<5; i++)
        cout << a[i] << " ";


void selection_sort(std::vector<int>& a, int length)
        {
               for (int count = 0 ; count < length - 1 ; count++)
                       swap(a[count],a[minimum_from(a,count,length)]);
        }
       
        int minimum_from(std::vector<int>& a, int position, int length)
        {
               int min_index = position;
       
               for (int count = position + 1 ; count < length ; ++count)
                       if (a[count] < a[min_index])
                               min_index = count;
                       return min_index;
        }
       
        void swap(int& first, int& second)
        {
               int temp = first;
               first = second;
               second = temp;
        }


    return 0;
}
selection_sort( array, array.size() );

instead of

sort(array.begin(), array.end());
and put all the function definitions before int main() instead of inside it
ASKER CERTIFIED SOLUTION
Avatar of Raymun
Raymun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial