How to find the smallest number in array?

I figured out how to find the largest number in array but I can't seem to figure out how to create a function to find the smallest number. Starting at Line 160, I can't figure out how to create a function that calculates smallest value!!

Thank YOU!

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>  //for cin, cout
#include <conio.h>	 //for _getch()

using namespace std;

const int MAX_SIZE = 20;	      // maximum size of array


//function prototypes
void ReadList( int[], int&);    
void PrintList(const int[], int);
void PrintReverse(const int[], int);
void PrintGreaterTen(const int[]);
void PrintLargest(const int[]);
void PrintSmallest(const int[], int);


int main()
{
    int list[MAX_SIZE];	     // array of nonnegative integers
    int numberOfIntegers;       // number of nonnegative integers in array

	//Call the functions	
    ReadList(list, numberOfIntegers);
    PrintList(list, numberOfIntegers);
	PrintReverse(list, numberOfIntegers);
	PrintGreaterTen(list);
	PrintLargest(list);
	PrintSmallest(list, numberOfIntegers);
	   
    _getch();  //hold the output screen

}




//***************************************************************************
// Definition of function ReadList. 
// This function reads nonnegative integers from the keyboard into an array.
// The parameter list is an array to hold nonnegative integers read.
// The parameter length is a reference parameter to an int.  It holds the 
// number of nonnegative integers read.
//***************************************************************************


void ReadList( int list[], int& length)  

{
	int number;		
	int index = 0;
	cout << "Enter nonnegative integers each separated by a blank space,\n"
	     << "and mark the end of the list with a negative number: " << endl;

       cin >> number;    //read the first integer entered

		//check that the number is nonnegative and
       // the size of the array is not exceeded
	while ((number >=0) && (index < MAX_SIZE))
  	{
	     list[index] = number;   //store the integer in the array
	     index++;        // increment the index
	     cin >> number;    // read the next integer          
       }

	length = index;  // length is the number of nonnegative integers
                        //    in the list
}



//***************************************************************************
// Definition of function PrintList. 
// The function prints the nonnegative integers in the array list and the
// number of integers in the array.
// The parameter list holds the nonnegative integers 
// The parameter length holds the number of nonnegative integers.
//***************************************************************************

void PrintList(const int list[], int length)

{
   int index;
   cout << endl;
   cout << "The list contains " << length
        << " nonnegative integer(s) as follows:"
        << endl;
   for (index = 0; index < length; index++)
   {
	   cout << list[index]<<" "<< endl;

   }
}
//***************************************************************************
// Definition of function PrintReverse. 
// The function prints the nonnegative integers in the array list in reverse
// order and the number of integers in the array.
// The parameter list holds the nonnegative integers 
// The parameter length holds the number of nonnegative integers.
//***************************************************************************
void PrintReverse(const int list[], int length)
{
	int index;
	cout << endl;
	cout << "Now here is the list of the " << length << " nonnegative integer(s) in reverse order:"
		 << endl;
	for (index = length-1; index >= 0; index--)
	{
		cout << list[index]<<"" << endl;
	}
}
//***************************************************************************
// Definition of function PrintGreaterTen. 
// The function prints the nonnegative integers in the array list that
// are greater than ten and the number of integers in the array that are
// greater than ten. The parameter list holds the nonnegative integers  
// The parameter length holds the number of nonnegative integers.
//***************************************************************************
void PrintGreaterTen(const int list[])
{
	int numberofTen = 0;
	cout << endl;
	cout << "Non-Negative integer(s) greater than 10 are: " << endl;
	for(int index=0; index < 10; index++)
	{
		if (list[index] > 10)
		{
			cout << list[index] << "" << endl;
			numberofTen++;
		}
	}
	cout << endl;
	cout << "*There are " << numberofTen << " non-negative integer(s) that are greater than 10*" << endl;
}
//***************************************************************************
// Definition of function PrintLargest. 
// The function prints the largest nonnegative integer in the array list and
// returns the value. The parameter list holds the nonnegative integers.
//***************************************************************************
void PrintLargest(const int list[])
{
	int index = 0;
	int largest;
	largest = list[index];
	
	for (index = 1; index < 20; index++)
	{
		if(list[index] > largest)
			largest = list[index];
	}
	cout << endl;
	cout << "Largest value stored in the array is: " << largest << endl;
}
//***************************************************************************
// Definition of function PrintSmallest. 
// The function prints the smallest nonnegative integer in the array list and
// returns the value. The parameter list holds the nonnegative integers.
// The parameter length holds the number of nonnegative integers.
//***************************************************************************
void PrintSmallest(const int list[], int length)  //Can;t Figure out
{
       //This is what I have attempted to make, but I get a stack overflow :S
	int index = 0;
	int smallest;
	smallest = list[index];
	
	for (index = length-1; index >= 0; index--)
	{
		if (list[index] > smallest)
			smallest = list[index];
	}
	cout << endl;
	cout << "Smallest value stored in the array is: " << smallest << endl;
}
Last edited on
Not sure about your stack, but on line 169 replace > with <
Now I get a random number for smallest value
Last edited on
closed account (S6k9GNh0)
What's the exact error... and why must you use conio... D:
closed account (S6k9GNh0)
Actually, you need to check your for loops... you can't iterate through an array without knowing its size.
Last edited on
It isn't obvious why you would be getting stack overflow.

The error with PrintSmallest has already been pointed out as the comparison on line 169.

PrintLargest is wrong. It always processes 20 elements regardless of how many are entered by the user.

PrintGreaterTen is wrong. It always processes (only) 10 elements, regardless of how many are entered by the user.
Last edited on
Topic archived. No new replies allowed.