5 Replies - 10368 Views - Last Post: 28 June 2008 - 10:40 AM Rate Topic: -----

#1 Visualgap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 08

How to display highest and lowest numbers in an array?

Post icon  Posted 27 June 2008 - 12:19 PM

I'm trying to write this:
"Write a program that allows the user to enter seven double values representing store sales for each day of one week. After all seven values are entered, echo them to the screen, and display highest and lowest sales values."

I got this so far (it only displays the seven values):
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
	const int SZ = 7;
	double sales[SZ];
	int x; 
	
   
		for(x = 0; x < SZ; ++x)
	{
		cout<<"Enter sales for day "<<(x + 1)<<" >> ";
		cin>>sales[x];
		
	}
	cout<<"The entered sales are: ";
	for(x = 0; x< SZ; ++x)
	 cout<<sales[x]<<"  ";
 

getch();
}



Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to display highest and lowest numbers in an array?

#2 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: How to display highest and lowest numbers in an array?

Posted 27 June 2008 - 12:31 PM

Do it like this:

Assume the biggest number is the first element in the array, store it in a variable that and then check it against the next element in the array, if it is bigger than the previous then assign it to the variable you created earlier, continue it like this until the array is finished, then output the variable to the screen. Same is for lowest number first assume that the lowest number is the first element in the array and assign its value to a variable which stores the lowest number and continue on till the end of array.

An alternate way is to sort them via a sorting algorithm. There are a lot try the one which is easy for you.

Sorting Algorithm - Wikipedia

Example
int getLowest()
{
     int lowest = array[1];
      // loop through array
     for ( int i = 0; i < arraySize; i++ )
     {
        
        if ( array[ i ] < lowest )                             
           lowest = array[ i ]; // new lowest         
     } // end for
      return lowest; // return lowest
}


This post has been edited by manzoor: 27 June 2008 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 Visualgap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 08

Re: How to display highest and lowest numbers in an array?

Posted 27 June 2008 - 01:16 PM

:D

This post has been edited by Visualgap: 27 June 2008 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Visualgap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 08

Re: How to display highest and lowest numbers in an array?

Posted 27 June 2008 - 01:28 PM

I managed to display the highest number but not the lowest (instead I get a weird number) :crazy: :
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
	const int SZ = 7;
	double sales[SZ];
	int x; 
	double lowest = sales[0];
	double highest = sales[0];
   for(x = 0; x < SZ; ++x)
	{
		cout<<"Enter sales for day "<<(x + 1)<<" >> ";
		cin>>sales[x];
		
	}
	cout<<"The entered sales are: ";
	for(x = 0; x< SZ; ++x)
	 cout<<sales[x]<<"  ";
	 
	 for ( int x = 1; x < SZ; x++ )  
		 if ( highest < sales[x])							   
		   highest = sales[x];
		{
		cout<<"\nThe highest sale is "<<highest<<endl;			   
		}
	 for ( int x = 1; x > SZ; x++ )
		
		
		if( lowest > sales[x])									 
		lowest = sales[x]; 
	   {
	 cout<<"\nThe lowest sale is "<<lowest<<endl;
		}  
			 
	  getch();
	 }


This post has been edited by Visualgap: 27 June 2008 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: How to display highest and lowest numbers in an array?

Posted 28 June 2008 - 06:55 AM

Ok the problem lied with the initialization of lowest and highest variables

You are initializing it with sales[0] before sales has a value.

So initialize it after getting data in it

I have done it for ya.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    const int SZ = 7;
    double sales[SZ];
    int x;

   for(x = 0; x < SZ; x++)
    {
        cout<<"Enter sales for day "<<(x + 1)<<" >> ";
        cin>>sales[x];

    }
    double lowest = sales[0];
    double highest = sales[0];

    cout<<"The entered sales are: ";
    for(x = 0; x< SZ; x++) {
     cout<<sales[x]<<"  ";
    }

     for ( int x = 0; x < SZ; x++ ) {
         if ( sales[x] > highest ) {
           highest = sales[x];
         }


     }
     for ( int x = 0; x < SZ; x++ ) {


        if( sales[x] < lowest ) {
        lowest = sales[x];
        }

     }
     cout<<"\nThe lowest sale is "<<lowest<<endl;
     cout<<"\nThe highest sale is "<<highest<<endl;


      getch();
     }



This post has been edited by manzoor: 28 June 2008 - 06:56 AM

Was This Post Helpful? 2
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,806
  • Joined: 16-October 07

Re: How to display highest and lowest numbers in an array?

Posted 28 June 2008 - 10:40 AM

You issue seems to be initializing, but you're pretty close.

Note, it's only the "After all seven values are entered, echo them to the screen" requirement that calls for an array. The "display highest and lowest sales values" can be done without one.

Here's a working example with comments to clarify.
#include <iostream>

using namespace std;

#define SZ 7

int main() {
	// the "const int" will not work in some compilers
	// the #define will always work
	// const int SZ = 7;
	
	double sales[SZ];
	
	// don't assign values to this yet
	double lowest, highest;
	
	for(int i = 0; i < SZ; i++) {
		// I like having a bucket value, 
		// I think it makes things easier to read
		double entered;
		
		cout<<"Enter sales for day "<< (i + 1) <<" >> ";
		cin >> entered;
		
		sales[i] = entered;
		
		if (i==0) {
			// intialize your high and low here, after you have a value to chew on
			lowest = highest = entered;
		} else {
			// do compares here, no other loops required for checking
			if ( entered < lowest ) { lowest=entered; }
			if ( entered > highest ) { highest=entered; }
		}
	}
	
	// that's it, output time
	cout<<"The entered sales are: ";
	for(int i = 0; i < SZ; i++) {
		cout << sales[i] <<"  ";
	}
	cout << endl;
	cout<<"The highest sale is " << highest << endl;
	cout<<"The lowest sale is " << lowest << endl;
	
	// always play nice, return a 0 if it's good
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1