How to put work into functions..

I have my code set up to work exactly as I want it too. Some compilers give me an error on the way I got my array to be a value defined by how many values the user wants to enter, but for this purpose that is ok.

However, my task is so seperate each part into its own function. This is where I am stumbling. I have tried putting my sort code into a "void" function above main but it gives me "undeclared first use of this function".

I need a function where the user enters the values (which I have worked out) a sort function (which is giving me trouble) and another seperate function to output the sorted array. (which I think I can figure out if I can get one of the others in a function).

My issue is I am having trouble understanding how to set up code "in a function". Where they are placed and how to name the parameters to eventually pass that function later.

Some insight and tips on this function work would be awesome!

I'm not sure what we can tell you that any existing tutorial or textbook can't. Doesn't your textbook give you examples of how to write functions and pass parameters to them?

Take a look at the tutorial on this site:

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
My apologies. I will try to be more clear. Our textbook does show examples and explains but I am having trouble understanding these. I forgot to post my code in the previous post so I will add that on as well.

When I look at the textbook and the examples online , the functions above name all seem to have different variables and when I try this, the variables are always undeclared first use of this function....i am obviously doing something wrong.

Do I just make up variables in the parameters of my function and then pass in the actual variables when called??

I have got all the errors removed, besides one. It wants a value in my called sort function in the array [], but when i put a value or variable in I get the "name lookup of () changed"
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>

using namespace std;


int sort( int nums [], int size, int temp) {
 for(int i=0; i<size; i++)  {
    for(int j=0; j<size; j++) {
      if(nums[j]>nums[j+1]) {
                              
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp; 
}
} 
}
} 

int main()
{

int n = 0;
int temp;

cout << "How many numbers would you like to enter?\n";
cin >> n; 

int array[n];

cout<<"Enter your numbers: "<<endl;
for(int i=0; i<n; i++)
{
cin>>array[i]; 
} 
cout<<endl; 
cout<<"Orignally entered array by the user is: "<<endl;

for(int j=0; j<n; j++) {
     cout<<array[j];
     cout<<endl; 
} 
cout<<endl;

sort (array[], n, temp);

cout<<"Sorted Array is: "<<endl;
for(int i=0; i<n; i++)
{
cout<<array[i]<<endl; 
} 

return 0;
}
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std ;

/*int*/ void sort( int nums [], int size ) // ***
{
    for(int i=0; i<size; i++)
    {
        for(int j=0; j < (size-1) ; j++) // *** (size-1) ie. (j+1) < size
        {
            if( nums[j] > nums[j+1] )
            {
                const int temp = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;
            }
        }
    }
}

int main()
{
    const int MAX_SIZE = 1000 ; // ***
    int array[MAX_SIZE]; // *** size of an array must be an integral constant

    std::cout << "How many numbers would you like to enter?\n";
    int n = 0;
    std::cin >> n;
    if( n > MAX_SIZE ) n = MAX_SIZE ; // ***

    std::cout << "Enter your numbers: " << '\n';
    for(int i=0; i<n; i++)
    {
        std::cin >> array[i];
    }
    std::cout << '\n';
    std::cout << "Orignally entered array by the user is: " << '\n';

    for(int j=0; j<n; j++)
    {
        std::cout << array[j];
        std::cout << '\n';
    }
    std::cout << '\n';

    sort( array, n ); // *** note: omit the empty subscript []

    std::cout << "Sorted Array is: " << '\n';
    for(int i=0; i<n; i++)
    {
        std::cout << array[i] << '\n';
    }
}
JLBorges, I don't think your j < (size-1) (line 8) makes any sense, the OP's code looks more correct there.
When j becomes equal to size-1, what would be the value of j+1?
Topic archived. No new replies allowed.