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.

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:

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:

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.

'swap' isn't a good name cause STL (standard template library) has a swap function as well. You didn't get problems till now cause you didn't include <algorithm> where the swap function was defined. Actually both function do the same but it will not compile if you have two global functions using the same name. The easiest way out is to rename your swap function to swap_ints or selection_swap.

>>>> void selection_sort(std::vector<int>& a, int length)

The length argument is redundant when using std::vector cause you can get the 'length' by calling array.size(). I would suggest to change the functions selection_sort and minimum_from accordingly:

Introduction
This article is the first in a series of articles about the C/C++ Visual Studio Express debugger. It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints. Lastly, Part 3 focuses on th…

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into.
One of these problems is selecting and deleti…

The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.