Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey
I 'm begginer in c++,
I write program from below link:
Having Trouble Finding Maximum, Minimum (A R R A Y)[^][^]

this works fine but I want to change the program as below:
for example when user enter 10 number to get min & max the last number should be 0.
I mean the last number should be 0 and if it's not an error shows up.
Posted
Updated 15-May-13 21:12pm
v2
Comments
Aarti Meswania 16-May-13 3:17am    
you mean you want to Cin>> if a[9] having value 0 input by user?

And what's the problem? Just check if a[length-1] == 0, namely
C++
if ( a[length-1] == 0)
{
  // OK
}
else
{
  // handle error
}
 
Share this answer
 
Comments
behish 16-May-13 3:49am    
thanks a lot,it works.
Hi,

that should be fairly easy. You just use a while loop to go through the arroy and when you find zero, the loop ends:
C++
#include <stdlib.h>
#include <stdlio.h>
#include <limits.h>

// function that finds and prints the MIN and MAX array value
void FindMinAndMax(int* array, int length)
{
   int Min = INT_MAX; 
   int Max = INT_MIN;

   // here we check for the error
   if (array[length-1] != 0)
   {
      printf("Error: Array must end with 0 !!!");
      return ;
   }

   int i = 0;
   while (array[i] != 0)
   {
       if (array[i] > Max)
           Max = array[i]; 

       if (array[i] < Min)
           Min = array[i];   

       i++;     
   }
   
   printf("Min = %d\n", Min);
   printf("Max = %d\n", Max);
}

Hope this helps.

Best regards.
J.K.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900