Finding the lowest number of a certain input

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-15-2006
theorbb
Finding the smallest integer
Hi,

I've just started programming a few weeks ago and i have a problem which involves me finding the lowest number. That is, if the input is 123 12 1342 1 the program should print out the lowest number, ie. 1 in this example.

solving the problem. I have been able to find the min of set

amount of numbers (like the minimum of 2/3/4 numbers) , but i have no idea how to go about solving

the problem where the size of the numbers inputted varies.

sorry in advance if some of it doesnt make sense....its a bit hard to explain.

Theorbb
• 04-15-2006
Salem
Keep a variable called 'currentSmallestNumber'
• 04-15-2006
ssharish2005
Code:

```/*assume that an array with some ten elements in different order. finding the smallest among them*/ small <- first element of array for i=1 till array size - 1     if small > ith element of array then         small <- ith element of array     end if loop print small```
ssharish2005
• 04-15-2006
theorbb
could you please expand?....i dont really know what you mean, sorry.
• 04-15-2006
ssharish2005
my previous post was all about the pesudo code. u can actually convert that into the code. i think u dint get any of those words. let me explain u

Code:

``` assume array of 6  elements [ 10 3 5 1 29 20]  assume first elemenst of the array is the smalest of all elements.  small <- 10;  compare the small element with other elements of the array.    for(i=1//because first is already small assumption;i<6;i++)     compare the small with all other elements     if(small > ith element)     store the ith element in the small overwritting the older value     next time the loop goes back it check with the updated small value```
hope this helps u

ssharish2005
• 04-15-2006
Kazmeister
Hi!

I'm new to this too but it seemed like a good practice exercise, so I toyed around with it a bit and this is what I came up with, just using the basics.

Code:

```#include <stdio.h> int n;//how many numbers int s=999999;//smallest number (This number must be bigger than the biggest number to be entered. int main() { int x;//inputted number int y;//for loop counter int z[n]; n=1;       for (y=0;y<n;y++) {                                   scanf("%d", &x);//Input x here with scanf if entering numbers one by one.                                         //Input 0 to break the loop                                                 if (x != 0) {  //If loop is not broken...                                                                                               z[n]=x;//Set number entered to z[n]...                                                             if  (z[n]<s) {//If new number is smaller than smallest...                                                                               s=z[n];//New number becomes smallest...                                                                               }                                                                                                                   n++;//n is incremented by one allowing the loop to continue.                                                                                                    }                                                                           }                               printf("%d is the smallest number.", s);//Once a zero is entered, smallest number is displayed.       getchar();       getchar();                                            }```
It's not perfect, but provided the user is entering the numbers it works... More or less.

Anyway it was fun bending my mind around all the variables.

And now it's bedtime.

:D

-James-
• 04-16-2006
Salem
Well no, not until you provide some code to show what you can actually achieve yourself.

I mean, just reading input and printing it out would be a start.
Then move onto comparing the current number with the previous number (say).

Kazmeister's answer shows the basic idea, but has too many bugs to be actually run.
• 04-16-2006
theorbb
Quote:

Originally Posted by Salem
Well no, not until you provide some code to show what you can actually achieve yourself.

well sorry for asking Mr.Poseidon - "God of the C".....because i can do SOOO much with the knowledge of keeping a variable called 'currentSmallestNumber'.

But, thanks ssharish2005 and Kazmeister, i think im getting the logic behind how to go about the problem.
• 04-16-2006
quzah
You're an idiot if you can't figure out from "Keep a variable called 'currentSmallestNumber'" that you need to oh, I don't know... STORE THE SMALLEST NUMBER.

You fail at basic problem solving. You also fail common sense.

Quzah.
• 04-16-2006
Salem
Perhaps you should post next time what you've actually managed to do yourself if you want some meaningful help.

Sure, anyone can post lots of waffle about "I'm a noob" and "I'm stuck", and sure someone will happen along a spoon-feed you the answer, and you'll go away happy and think you've learnt something.

But you won't have really....

Well that depends, are you actually going to post your answer for all to see, or are you just going to take the answer and hand it in - bugs and all?

Nah, you won't do that, you don't have the stones to post any of your code in a forum - hell, I doubt you've ever written any yourself.
• 04-16-2006
a.baki

Code:

```#inlude <stdio.h> int main () {       int counter;       int number[5] = {10, 21, 29,78,3};//your input can be different, then try to convert codes       int min;       min = number[0];//or initialize to a very big number       for ( counter = 0; counter < 5; counter++ ){               if ( number[i] < min ){                     min = number[i];             }       }       printf ("%d\n", min);             }```
• 04-16-2006
SlyMaelstrom
Close, but a few errors. Actually, one compile error, and one optimization tip. For the sake of the original poster, I won't point them out. If he can't identify the problem, then he should take up something else. The logic you have is correct, though.
• 04-16-2006
cdalten
Quote:

Originally Posted by quzah
You're an idiot if you can't figure out from "Keep a variable called 'currentSmallestNumber'" that you need to oh, I don't know... STORE THE SMALLEST NUMBER.

You fail at basic problem solving. You also fail common sense.

Quzah.

You seem bitter. Is there anything you wish to share with the group?
• 04-16-2006
cdalten
Quote:

Originally Posted by SlyMaelstrom
Close, but a few errors. Actually, one compile error, and one optimization tip. For the sake of the original poster, I won't point them out. If he can't identify the problem, then he should take up something else. The logic you have is correct, though.

I counted 5 errors. Even after I corrected them, the code doesn't work.
• 04-16-2006
cdalten
Okay, make that 6 errors. After I corrected them all, I got the correct output.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last