Solved

Urgent!!! Can't find the problem...

Posted on 2003-10-29
7
195 Views
Last Modified: 2010-04-15
I wrote a program to find the bigger and the smaller number in a list but there's something wrong and don't know what it is becuse the program just gives me the biggest number while in the smaller number always appear 0.  Thanks for your help!!!
Here it is:

#include <stdio.h>

void main()
{
 int arr[10], n, dat, max=0, min=0;
 for(n=0;n<=9;n++)
 {
  printf("Number: ");
  scanf("%d",&dat);
  if(dat>max)max=dat;
 if(dat<min)min=dat;
 }
 printf("Bigger %d Smaller %d",max,min);
}
0
Comment
Question by:gothic130
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 150 total points
ID: 9645336
for(n=0;n<=9;n++)
 {
  printf("Number: ");
  scanf("%d",&dat);

  if (n == 0) min = dat;

  if(dat>max)max=dat;

  if(dat<min)min=dat;
 }
 printf("Bigger %d Smaller %d",max,min);
0
 

Author Comment

by:gothic130
ID: 9645408
>>if (n == 0) min = dat;

 Please could you explain me what this line does?
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9645745
You should initialize min to INT_MAX instead of 0
You should initialize max to INT_MIN instead of 0

They defined in <limits.h>

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Expert Comment

by:bobbit31
ID: 9645842
> Please could you explain me what this line does?

basically, your min starts out at 0... if all the numbers in the array are positive, min will always be 0.

what the line above does is it sets min to the first element in the array... if the first element is the min then it stays the min, otherwise it'll keep going through and update min accordingly (note that if (n==0) will only happen once)
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 9645850

Using the code in the original question, a small change (similar to bobbit31's) should do just fine:

#include <stdio.h>

void main()
{
 int arr[10], n, dat, min, max;

 min = arr[0];                /*  Default min to the first value  */
 max = arr[0];               /*  Default max to the first value  */
 for(n=1;n<=9;n++)      /*  Search the rest of the list  */
 {
  printf("Number: ");
  scanf("%d",&dat);
  if(dat>max)max=dat;
 if(dat<min)min=dat;
 }
 printf("Bigger %d Smaller %d",max,min);
}


Kent
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9645854
gothic130 your algrythm will work if you just change the initialization as above.
It would also allow you to enter negative numbers.
0
 

Author Comment

by:gothic130
ID: 9645950
Thank you all:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question