4 Replies - 7016 Views - Last Post: 25 January 2010 - 11:18 AM Rate Topic: -----

#1 tblakk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 tblakk  Icon User is offline

  • New D.I.C Head

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

Re: Find lowest number in an array

Posted 25 January 2010 - 10:13 AM

View PostBench, 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;
}



Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • 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.
Attached Image

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:
Attached Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1