Passing an array as argument to function!

Hi, I am attempting to write a program that will ask the user to input 10 integers, and then send them to the function that will put them into descending order and replace the original numbers in the array. Then return them to the main to output them to the screen. I am getting confused on how to call the function with the array and how to write the function prototype? Here is my code, i commented out the function because it won't run with it in.

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
54
55
56
57
58
59
60
  #include<iostream>
#include<iomanip>
#include<string>
#include<cmath>

using namespace std;

int i;
int j;
const int max = 10;


int main()
{
	const int max = 10;

	int Jethro[max];

	for(i = 0; i < max; i++)
	{
	cout<<"Please input ten integers: "<<endl<<endl;
	
	while(!(cin>>Jethro[i]))
		{ cin.clear();
      cin.ignore(10000,'\n');
	
	cout<<"Please input only integers"<<endl;
	}
	 
	}
	cout<<endl<<endl;


	for(i = 0; i<max; i++)
	{
		cout<<Jethro[i]<<endl;
	}

	system("pause");
	return 0;
}
/*
int hi_2_lo(int num[max])
{
	int y = num[0];
	int low;
	for(i = 0; i<10; i++)
	{
		
			if(y<num[i]);
		low = y;
		y = num[i];
		num[i] = y;


	}
	return 0;
}

*/
passing arrays is actually quite easy:
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
54
55
56
57
58
59
60
61
#include<iomanip>
#include<string>
#include<cmath>

using namespace std;

int i;
int j;
const int max = 10;

void hi_2_lo( int num[] );

int main()
{
	const int max = 10;

	int Jethro[max];

	for( i = 0; i < max; i++ )
	{
		cout<<"Please input ten integers: "<<endl<<endl;

		while( !(cin>>Jethro[i]) )
		{
			cin.clear();
			cin.ignore( 10000, '\n' );

			cout<<"Please input only integers"<<endl;
		}

	}
	cout<<endl<<endl;

	hi_2_lo( Jethro );

	for( i = 0; i<max; i++ )
	{
		cout<<Jethro[i]<<endl;
	}

	system( "pause" );
	return 0;
}


void hi_2_lo( int num[] )
{
	int y = num[0];
	int low;
	for( i = 0; i<10; i++ )
	{

		if( y<num[i] );
		low = y;
		y = num[i];
		num[i] = y;


	}
	return;
}


Keep in mind, arrays are always passed by reference, not by value. so any modifications to the array within the hi_2_lo() function will be directly changing the array created in main().

The prototype is on line 11. it is exactly the same as the function definition, followed by a ;

I threw in a call at line 34 just to demonstrate how to pass it. This is by no means a finished program.
Last edited on
Oh, ok, that isn't bad at all! I am getting two errors when i run it now, though. One is one line 34 of your code and the other is in line 54 of your code. On 34, it is saying that the identifier can't be found (referring to the 'hi_2_lo') and line 54 says that there is an empty controlled statement found (referring to the ';' ). What do those mean?
hmm... did you remember to put in the prototype? that would explain the problem with line 34.

line 54 is a warning. It's just telling you that the if() statement on line 53 doesn't do anything since you put a ; at the end of it :)

also, make sure the return type for hi_2_lo() is void in both the prototype and definition.

also, also, lines 55 and 56 make circular reference to each other... thus 56 is useless.

also, also, also, your hi_2_lo sorting algorithm is pretty flawed. Here's a sort that's easy to implement complete with an example in c++: http://en.wikipedia.org/wiki/Selection_sort

keep in mind that you want to reverse the check since you're sorting in descending order (so rather than looking for the lowest element to swap, look for the highest)
Last edited on
Yes, i had the prototype and had void for both return types. Yea, that ';' was a silly mistake. Here is my code thus far:

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
54
55
56
57
58
59
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>

using namespace std;

int i;
int j;
const int max = 10;
void hi_2_low (int num[]);

int main()
{
	const int max = 10;

	int Jethro[max];

	for(i = 0; i < max; i++)
	{
	cout<<"Please input ten integers: "<<endl<<endl;
	
	while(!(cin>>Jethro[i]))
		{ cin.clear();
      cin.ignore(10000,'\n');
	
	cout<<"Please input only integers"<<endl;
	}
	 
	}
	cout<<endl<<endl;

	hi_2_lo(Jethro);

	for(i = 0; i<max; i++)
	{
		cout<<Jethro[i]<<endl;
	}

	system("pause");
	return 0;
}

void hi_2_lo(int num[])
{
	int y = num[0];
	int low;
	for(i = 0; i<10; i++)
	{
		
			if(y<num[i])
		low = y;
		y = num[i];
		


	}
	return ;
}
Would something like this work?

1
2
3
4
5
6
7
8
9

 for (i=1; i<10; i++) 
        for (j=9; j>=0; j--) {
            if(num[j-1] < num[j]) {
                low=num[j-1];
                num[j-1] = num[j];
                num[j] = low;
                }
            }
line 11: no 'w' in hi_2_lo

undefined identifier, identifier not found, error messages like these are very often spelling/capitalization errors. check closely when they occur.
Last edited on
Topic archived. No new replies allowed.