Working on arrays?

Hello, I need help trying to find the smallest and largest number in a randomized array of 25 numbers. I know that what I have put into my main function is wrong, but can't seem to find the problem. Please help. Any suggestions would be wonderful. Thanks.

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

using namespace std;

void fillArray(int ar[], int i);
void printArray(const int ar[], int i);

int main()
{
	int array[25];

	fillArray(array, 25)

	cout << "The smallest number in the array is " << printArray(array, 25) << endl;
	cout << "The largest number in the array is " << printArray(array, 25) << endl;

	return 0;
}
void fillArray(int ar[], int i)
{
	int min = 0, max = 0;

	for(i = 0; i < 25; i++)
	{
		if(ar[i] > ar[max])
			max = i;
		else if(ar[i] < ar[min])
			min = i;
	}

}
void printArray(const int ar[], int i)
{
		int min = 0, max = 0;

	for(int i = 0; i < 25; i++)
	{
		if(ar[i] > ar[max])
			max = i;
		else if(ar[i] < ar[min])
			min = i;
	}

	cout << ar[max];
	cout << ar[min];
}
line 13 you are missing a ;
cout << "The smallest number in the array is " << printArray(array, 25) << endl; This is invalid because your printArray function dosn't return a value.

Line 26 if(ar[i] > ar[max]) you are checking to see if ar[i] is less than ar[max] but you haven't put anything in the array yet.

You should use more descriptive names for your parameters

use the srand() and rand() functions to create randome numbers
Last edited on
Okay, I've tweaked my code, but I keep producing compiling errors? Not sure what I'm doing wrong.

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 <cmath>

using namespace std;

void fillArray(int ar[], int size);
void printArray(const int ar[], int size);
void smallLarge(int &min, int &max);

int main()
{
	int arr[25];
	
	fillArray(arr,25);
	printArray(arr,25);

	return 0;
}
void fillArray(int ar[], int size)
{

	for(int i = 0; i <= 100; i++)
	{
		int size = rand() % 101;
		
		if(size >= 1 && size <= 100)
			ar[i] = size;
	}


}
void printArray(const int ar[], int size)
{
	for(int i = 0; i < size; i++)
	cout << ar[i] << endl;
	 
	int big;
	int small;

	smallLarge(small, big);

	cout << "The smallest number in the array is " << small << endl;
	cout << "The largest number in the array is " << big << endl;

}
void smallLarge(int &min, int &max)
{	
	min = 0; 
	max = 0;

	for(int i = 0; i < 100; i++)
	{
		if(max > min)
			max = i;
		
		else if(min < max)
			min = i;
	}			
		return;
}
Topic archived. No new replies allowed.