Call Function

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
#include <iostream>
#include <conio>

int main()
{
	cout<<swap_values(int &v1, int &v2);
   getch();
   return 0;
}
void swap_values(int &v1, int &v2)
{
	int temp;
   temp = v1;
   v1=v2;
   v2=temp;
}

int index_of_smallest(const int a[], int begin_index, int end_index)
{
	int min = a[end_index], index_of_min = end_index;
   for(int  index=begin_index; index<end_index; index++)
   if(a[index]<min)
	{
   	min = a[index];
      index_of_min = index;
   }

   return index_of_min;
}

void sort(int a[], int last_index)
{
	int index_of_next_smallest;
   for (int index=last_index; index>=0; index--)
   {
   	index_of_next_smallest = index_of_smallest(a, 0, index);
      swap_values(a[index], a[index_of_next_smallest]);
   }
}


Hi, I got this code from C++ book, but then it's without line 1-9, so, I don't know how to write the code to call the function after line 9, can anyone help?
The question is something like implementation of a sorting algorithm that sorts a list of numbers in an array...TQ...
1. Declare the functions before their first use.
2. Call the functions by passing the values in.

Note line 6 won't work because swap_values() doesn't return a value for the cout. Also, don't use the & when calling the function. Also, you didn't define v1 or v2.
Sorry Stewbond, I'm still a bigginer study this part, but don't know how to correct them...Now I try this it's run, but give a weird output...

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
#include <iostream>
#include <conio>


void swap_values(int &v1, int &v2)
{
	int temp;
   temp = v1;
   v1=v2;
   v2=temp;
}

int index_of_smallest(const int a[], int begin_index, int end_index)
{
	int min = a[end_index], index_of_min = end_index;
   for(int  index=begin_index; index<end_index; index++)
   if(a[index]<min)
	{
   	min = a[index];
      index_of_min = index;
   }

   return index_of_min;
}

void sort(int a[], int last_index)
{
	int index_of_next_smallest;
   for (int index=last_index; index>=0; index--)
   {
   	index_of_next_smallest = index_of_smallest(a, 0, index);
      swap_values(a[index], a[index_of_next_smallest]);
   }
}

int main()
{
	int v1,v2;
	cout<<swap_values;
   getch();
   return 0;
}
swap_values is a pointer to the function. You're probably outputting the address which doesn't mean anything to you;

To call a function, use () like
swap_values();.

Now swap_values accepts 2 arguments, so you need to feed it with two arguments:
swap_values(v1,v2);

You never initialize v1 and v2, you are swapping two values, but you have no idea what those values are so you'll get garbage.

Finally, swap_values returns a void. This means it can't be used to set anything such as cout. Think about it, what would you expect to see?

Change your main to this:
1
2
3
4
5
6
7
8
9
10
int main()
{
	int v1,v2;
	cout << "Enter v1, v2: "; //Ask the user for an input
	cin >> v1 >> v2; //Set v1 and v2
	swap_values(v1,v2); //Swap v1 and v2
	cout << "v1 is: " << v1 << " v2 is: " << v2 << endl; // Output v1 and v2
   getch();
   return 0;
}
Thanks Stewbond...the program works fine...but a question here...what's the purpose of putting code from line 13-34 into the program? TQ...
Topic archived. No new replies allowed.