turning my code into a function and how to call on it

Here is my code and it works perfectly. Now i just want to know how to turn it into a function and how to call on it. This code arranges 5 numbers in an array in increasing and decreasing order. I want to turn into a function so that i can make a shorter code that calls on this code(which is now a function) to sort 5 numbers.

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
62
63
64
65
66
67
68
69
70
#include <stdio.h>
int main(void)
{
	int Array[5];
	int TempArray[3];
	int i;
	int j;
	int max1;
	int max2;
	int mid;
	int min1;
	int min2;

	for (i = 0; i < 5; ++i){
		printf("Please enter a number: ");
		scanf("%d", &Array[i]);
	}
	max1 = Array[0];
	min1 = Array[0];


	// To find the maximum and minimum numbers in the whole list.
	for (i=1; i<=4; ++i){
		if (max1<Array[i])
			max1 = Array[i];
		if (min1>Array[i])
			min1 = Array[i];
	}

	j=0;

	// To find the sub-list of numbers from the main list.
	for (i=0; i<=4; ++i){
		if (max1 != Array[i]){
			if (min1 != Array[i]){
				TempArray[j] = Array[i];
				++j;
			}
		}
	}
	
	max2 = TempArray[1];
	min2 = TempArray[1];

	// To find the maximum and minimum numbers in the sub-list.
	for (j=0; j<=2; ++j){
		if (max2<TempArray[j])
			max2 = TempArray[j];
		if (min2>TempArray[j])
			min2 = TempArray[j];
	}
	
	// To find the middle number in the list.
	for (j=0; j<=2; ++j){
		if (TempArray[j] != max2)
			if (TempArray[j] != min2)
				mid = TempArray[j];
	}
		
	

	printf("In Ascending Order: %d, %d, %d, %d, %d\n", min1, min2, mid, max2, max1);
	printf("In Descending Order: %d, %d, %d, %d, %d\n", max1, max2, mid, min2, min1);
	
				


	system("PAUSE");
}
			



The code works perfectly.
please do help!! Thank you!
Last edited on
Basically:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Function prototype/declaration
int findMax(int Array[], int size);

// Main
int main(void)
{
  const int SIZE = 4;
  int myArray[SIZE] = { 3 , 4 ,1, 2 };
  int max = findMax(myArray, SIZE);
  // print to screen
  return 0;
} 

int findMax(int Array[], int size)
{
  int max = 0;  // probably not so good, what if all numbers are negative
  int i;
  for (i=0; i < size; ++i)  // i = 0
  {
    if (max < Array[i])
      max = Array[i];
  }
  return max;
}


To be safe:
1
2
3
4
5
6
7
8
9
10
11
12
13
int findMax(int Array[], int size)
{
  if (size <= 0) return 0;  // okay, now just make sure size > 0 before the call 

  int max = Array[0];
  int i; 
  for (i=1; i < size; ++i)
  {
    if (max < Array[i])
      max = Array[i];
  }
  return max;
}


Wow that helped thanks but how do I return multiple values. I want it to return min, max and a couple others
You can return some sort of aggregate or structure containing the values you want to return, or pass references to variables as parameters that will have their values overwritten with whatever extra values you want returned.
1
2
3
4
5
void example (int& foo, float& bar)
{
    foo = 10; 
    bar = 20;
} //The variables referenced by foo and bar now contain the values 10 and 20 respectively. 
Last edited on
Topic archived. No new replies allowed.