2 Replies - 10480 Views - Last Post: 08 February 2009 - 06:44 PM Rate Topic: -----

#1 jerryroy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-November 07

print the largest and smallest number in an array

Posted 08 February 2009 - 02:38 PM

hey i am acceptint 10 values from the user an adding it to the array what i have a problem trying to figure out how to print back the largest and smallest number this is what i have so far:
#include <iostream>

using namespace std;

int main()
{
	int num,numbers[10],sum_even,average_even,sum_odd,avg_odd,count,odd_count(0),even_count(0);
	int max(0),min,max_index(0),min_index(0);
	sum_even = sum_odd = min = 0;


	for(count=0;count<10;count++)
	{
		cout <<"\nPlease Enter Number: ";
		cin >>numbers[count];


	if(num < numbers[count])
	{
		num = numbers[count];
		max = num;
		max_index = count;
	}
	if(max > min)


	if(numbers[count]%2 ==0)
	{
		sum_even = sum_even + numbers[count];
		even_count++;
	}
	else
	{
		sum_odd = sum_odd + numbers[count];
		odd_count++;
	}
	}
	avg_odd = sum_odd/odd_count;
	average_even = sum_even/even_count;

	cout <<"\nSum of even numbers = " << sum_even <<" Average = "<< average_even;
	cout <<"\nSum of Odd Numbers = " << sum_odd <<" Average =" << avg_odd;
	cout <<"\nThe Higest Number is: " << max << "The index is: " << max_index;
	cout <<"\nThe Smallest Number is: " <<min << " The index is: " << min_index;
} 



what i have so far smallest number is zero not sure what else to do? thanks for all the help

Is This A Good Question/Topic? 0
  • +

Replies To: print the largest and smallest number in an array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: print the largest and smallest number in an array

Posted 08 February 2009 - 02:43 PM

Easiest option is to sort the list and then output index[0] and index[size-1]

So:

int temp;
for(int i = 0; i < 10; i++)
{
	for(int j = 1; j < 10; j++)
	{
		   if(arr[i] > arr[j])
		   {
				 temp = arr[i];
				 arr[i] = arr[j];
				 arr[j] = temp;
		   }
	 }
}

cout << "smallest is: " << arr[0];
cout << "largest is: " << arr[9];


Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: print the largest and smallest number in an array

Posted 08 February 2009 - 06:44 PM

View PostKYA, on 8 Feb, 2009 - 01:43 PM, said:

Easiest option is to sort the list and then output index[0] and index[size-1]

So:

int temp;
for(int i = 0; i < 10; i++)
{
	for(int j = 1; j < 10; j++)
	{
		   if(arr[i] > arr[j])
		   {
				 temp = arr[i];
				 arr[i] = arr[j];
				 arr[j] = temp;
		   }
	 }
}

cout << "smallest is: " << arr[0];
cout << "largest is: " << arr[9];



The bubble sort can be made a little faster by using 10-i as the new upper limit each pass through the inner loop ... since we know that each pass bubbles the next largest ... to the next top position ...

for(int i = 0; i < 10; i++)
{
	for(int j = 1; j < 10-i; j++)
	{
		   if(arr[i] > arr[j])
		   {
				 temp = arr[i];
				 arr[i] = arr[j];
				 arr[j] = temp;
		   }
	 }
}


But doing a sort here ... if ALL you want is the lowest and the highest value ... is perhaps a little 'overkill' ?

You can just pass through the data ONCE ... This is enough.

BEFORE ... you pass through the data ... either on input ... or after all input and then by traversing the array (or vector) of data ...

set the FIRST value to both lowest and highest ...

Then ...
test each NEXT value and update 'lowest' and highest' if appropriate.

After ONE pass ... you are done.

You don't really need to use arrays at all ... You can do all your processing after you input each new item of data to find ...

lowest
highest
average
sumOfEvenNumbers
sumOfOddNumbers

....

Shalom,
David
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1