Q.Write a function to accept upto 25 numbers and display highest and lowest number along with all the numbers.

Solution I tried

#include<stdio.h>
#include<conio.h>
void main()
{
int array[25],low,high;
int i;
clrscr();
do
{
  printf("Enter a number(0 to terminate)\n");
  scanf("%d",&array[i]);
}while(array[i]!=0);
i=0;
low=high=array[0];
while(array[i]!=0)
{
 if (low>array[i])
  low=array[i];
 else if(high<array[i])
  high=array[i];
 i++;
}
for(i=0;array[i]!=0;i++)
 printf("Numbers are %d",array[i]);
printf("Highest is %d,lowest is %d",high,low);
}

Problem is that I am not getting the desired output. For every input I tried, I got the output as highest is 0, lowest is 0.HELP!!!:sad: :!:

Recommended Answers

All 4 Replies

Some points:

1. Use code tags to post your indented code so that it is more readable.

2. The loop you use for input can easily cause buffer overflows if zero is not entered by the user. If you know the array size in advance, replace all the cumbersome while loops with the for loops and repost your updated code.

3. You did a mistake in placing the logic for finding out the highest in the else block. Place it in a seperate if block.

4. Initializing your variables will save you a lot of head banging in the future. Always initialize your variables before using them in such situations.

I made the following changes

{
#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],low,high;
int i=0;
clrscr();
for(i=0;i<5;i++)
        {
 printf("Enter a number\n");
 scanf("%d",&array[i]);
        }
low=high=array[0];
while(array[i]!=0)
{
 if (low>array[i])

  low=array[i];

 if(high<array[i])

  high=array[i];
        i++;
}
for(i=0;array[i]!=0;i++)
 printf("Number is %d\n",array[i]);
printf("Highest is %d\n",high);
printf("Lowest is %d",low);
}
}

But now its assigning the first value input as both highest and lowest.

I still see a while loop in your code. On top of that the while loop uses the value of i which is 5 when the while block is entered (since you never reset it after the for loop).

Stop checking for the occurance of zero in the loop. Just make it like the loop you used for accepting user input.

Ya git it!! Thnx a lot!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.