# Find lowest number in an array

Page 1 of 1

## 4 Replies - 8547 Views - Last Post: 25 January 2010 - 11:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151893&amp;s=2ca680d1dea56bda6e18fa1bca81b4ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tblakk

Reputation: 0
• Posts: 4
• Joined: 13-November 09

# Find lowest number in an array

Posted 25 January 2010 - 09:27 AM

Hi everyone, I'm a beginner in C++ and need a little assistance. In the code I have below, I am trying to find the lowest value in array. I got the program to display the highest value and thought that if I copied the code and reverse the < operator in the for loop that it would display the lowest value. By reversing the < operator does work in finding the lowest value however, it doesn't work when both loops are in the program. The program displays the last loops value.

Thanks.

```#include <iostream>
#include <cstdlib> // For atoi

using namespace std;

int main()
{
const int  SIZE = 100;		// Array size
char getInput[SIZE];		 // Char array to hold the user's input
int total = 0;			  // Accumulator
int num;
int count;
int maxIndex,maxValue;
int startScan;

// Prompt the user to enter a series of numbers
cout << "Enter a series of numbers and I will add them for you: ";
cin.getline(getInput,SIZE);

num = atoi(getInput); // Convert user's string input and assign it to num.

// Add the numbers in the string
for(count = 0; count < strlen(getInput); count++)
{
total += getInput[count] - '0';
}

// Search the getInput array for the highest value
for(startScan = 0; startScan < strlen(getInput); startScan++)
{

maxIndex = startScan;
maxValue = getInput[startScan];

for(int index = startScan + 1; index < strlen(getInput); index++)
{

if (getInput[index]< maxValue)
{
maxValue = getInput[index];
maxIndex = index;
}

}

getInput[maxIndex] = getInput[startScan];
getInput[startScan] = maxValue;
}

// Search the getInput array for the lowest value
for(startScan = 0; startScan < strlen(getInput); startScan++)
{

maxIndex = startScan;
maxValue = getInput[startScan];

for(int index = startScan + 1; index < strlen(getInput); index++)
{

if (getInput[index] > maxValue)
{
maxValue = getInput[index];
maxIndex = index;
}

}

getInput[maxIndex] = getInput[startScan];
getInput[startScan] = maxValue;
}

// Display the sum of the digits
cout << total << endl;

// Display the highest value
cout << getInput[maxIndex] << endl;

// Display the lowest value

cout << getInput[maxIndex] << endl;

system("pause");
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Find lowest number in an array

### #2 taylorc8

• B&

Reputation: 150
• Posts: 1,572
• Joined: 21-July 09

## Re: Find lowest number in an array

Posted 25 January 2010 - 09:50 AM

Try making the algorithm to do it.

get teh first element

loop through comparing value <

zero all variables before hand

### #3 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: Find lowest number in an array

Posted 25 January 2010 - 09:51 AM

Look at these lines: Your output statements are both exactly the same, so obviously their output are both going to be exactly the same.
cout << getInput[maxIndex] << endl;

cout << getInput[maxIndex] << endl;
try storing your lowest index in a different variable.

This post has been edited by Bench: 25 January 2010 - 09:52 AM

### #4 tblakk

Reputation: 0
• Posts: 4
• Joined: 13-November 09

## Re: Find lowest number in an array

Posted 25 January 2010 - 10:13 AM

Bench, on 25 Jan, 2010 - 08:51 AM, said:

Look at these lines: Your output statements are both exactly the same, so obviously their output are both going to be exactly the same.
cout << getInput[maxIndex] << endl;

cout << getInput[maxIndex] << endl;
try storing your lowest index in a different variable.

Thanks for the response. I created variables to store the lowest index and lowest value but the program is displaying the results of the last loop for both cout statements.
Is there something I am not doing?

```#include <iostream>
#include <cstdlib> // For atoi

using namespace std;

int main()
{
const int  SIZE = 100;		// Array size
char getInput[SIZE];		 // Char array to hold the user's input
int total = 0;			  // Accumulator
int num;
int count;
int maxIndex,maxValue;
int minIndex,minValue;
int startScan;

// Prompt the user to enter a series of numbers
cout << "Enter a series of numbers and I will add them for you: ";
cin.getline(getInput,SIZE);

num = atoi(getInput); // Convert user's string input and assign it to num.

// Add the numbers in the string
for(count = 0; count < strlen(getInput); count++)
{
total += getInput[count] - '0';
}

// Search the getInput array for the highest value
for(startScan = 0; startScan < strlen(getInput); startScan++)
{

maxIndex = startScan;
maxValue = getInput[startScan];

for(int index = startScan + 1; index < strlen(getInput); index++)
{

if (getInput[index]< maxValue)
{
maxValue = getInput[index];
maxIndex = index;
}

}

getInput[maxIndex] = getInput[startScan];
getInput[startScan] = maxValue;
}

// Search the getInput array for the lowest value
for(startScan = 0; startScan < strlen(getInput); startScan++)
{

minIndex = startScan;
minValue = getInput[startScan];

for(int index = startScan + 1; index < strlen(getInput); index++)
{

if (getInput[index] > minValue)
{
minValue = getInput[index];
minIndex = index;
}

}

getInput[minIndex] = getInput[startScan];
getInput[startScan] = minValue;
}

// Display the sum of the digits
cout << total << endl;

// Display the highest value
cout << getInput[maxIndex] << endl;

// Display the lowest value

cout << getInput[minIndex] << endl;

system("pause");
return 0;
}

```

### #5 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Find lowest number in an array

Posted 25 January 2010 - 11:18 AM

Here's what happens when I run the most recent code you posted.

It doesn't even get to the highest and lowest.

Why are you storing ints in a char array? I'd use:
```int getInput[SIZE];
```

Also I don't see where you initialize your variables. Whenever I had to find the lowest in an array, I made my checker variable (whatever each element gets compared with, to see which is lower) the largest number I could. For your highest, you can initialize it to zero as long as negative numbers are not allowed.

Edit: and when I input one number with no spaces: