Create a function to sort array elements in ascending format

Hi,
Re subject wrote a code without a function and it compiles fine. Trying to implement a
function but it cannot compile this time.
Appreciate if one can point out my mistake as now I am all confused

Thank you!

// My code below ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int arrayAscend(int size, int arr[]){
int temp, i, j;
for(i = 0; i <size; i++){
      for(j = 0; j < size -1; j++) {
        if(arr[j] > arr[i]){
                  temp = arr[i]; //swap them
				  arr[i] = arr[j];
                  arr[j] = temp;	
                }
       }
}
}
int main ()
{
int Size = 5;
int Income[] = {40000, 20000, 100000, 35000, 75000,28000};

arrayAscend(Size, Income);
cout <<"Ascending order: \n";
for (int i =0; i < Size; i++){
cout << income[Size] <<"\n";
}
	return 0;
}
Last edited on
A few issues.

First

int arrayAscend(int size, int arr[]) // Your function is of type int. Meaning it has to return an integer.

Change it from int to void.

void arrayAscend(int size, int arr[]) // void means it does not return anything.

Second

int Income[] // Arrays must be fixed size during the declaration. So change that to Income[5];

Read about vectors - http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

Third

Your array name is Income with capital I. Yet when you try and print it out you use lowercased I.

int Income[] // Uppercase cout << income[Size] <<"\n" // lowercase;
Last edited on
Thank you for pointing those out... Have made the modifications and now able to compile.
I appreciate your help!

Cheers
Topic archived. No new replies allowed.