By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

getting the smallest number

P: 2
every time i input hundreds or thousands the answer goes wrong.






Results



Attached Images
File Type: jpg 1st.jpg (75.7 KB, 62 views)
File Type: jpg 2nd.jpg (98.5 KB, 47 views)
File Type: jpg 3rd.jpg (105.1 KB, 48 views)
File Type: jpg result 1.jpg (22.9 KB, 52 views)
File Type: jpg result 2.jpg (30.6 KB, 48 views)
Feb 24 '17 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Your approach is not correct.

Given a,b,c, and d and you need to find the lowest number you start with an assumed lowest number. In your case use a.

Expand|Select|Wrap|Line Numbers
  1. int max;
  2. max = a;
Then compare b to max. If b is smaller then set max to b. Otherwise leave max alone.

Repeat for c and d and max will contain the lowest number.

At this point, whichever variable is equal to max is the variable with the lowest number.
Feb 24 '17 #2

Frinavale
Expert Mod 5K+
P: 9,731
Or perhaps, since you are finding the lowest number, declare the variable that weaknessforcats is talking about as "min" instead of "max".

Basically, declare a variable called min and set it to the first number entered (a).

Compare min to the second number entered (b) and if b is smaller set min = b.

Do the same thing for c, d, and e.

After all the variables have been compared with min, you know that min contains the smallest number...and you can print it.
Feb 27 '17 #3

Expert 100+
P: 2,396
The logic problem outlined above is the main issue. Take the numbers from your first example (12, 23, 34, 45, 56) and enter them in a different order. You will see that your program only consistently gives the right answer when the minimum value happens to be a; so the problem isn't with large values.

However, Another issue is that your variables are declared as int. That type is only guaranteed to hold numbers as large as 32767 (although many compilers do support larger int values). Suggest you declare your variables as long -- and adjust the scanf and printf format strings accordingly.
Feb 28 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.