really new to C -- comparing numbers 
Author Message
 really new to C -- comparing numbers

I am really new to C... I am trying to have a user input 5 numbers then print
the largest integer and the smallest integer.  I understand how to do it with 2
numbers.  Any help would be useful.. Thank You
--



Sat, 13 Sep 2003 13:59:10 GMT  
 really new to C -- comparing numbers

Quote:

> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with 2
> numbers.

Really, this has nothing to do with C and everything with basic
algorithmics... use a loop, keep separate "smallest" and "largest"
variables, initialised to suitable values.

Richard
--



Mon, 15 Sep 2003 05:38:23 GMT  
 really new to C -- comparing numbers

Quote:

> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with 2
> numbers.  Any help would be useful.. Thank You

how about something like this?

#include <stdio.h>

int main(int argc, char *argv[]){
        int big = 0;
        int i;
        for(i = 0; i < argc; i++){
                if(atoi(argv[i]) > big)
                        big = atoi(argv[i]);
        }
        printf("the biggest number was %d\n", big);

Quote:
}

--



Mon, 15 Sep 2003 05:38:34 GMT  
 really new to C -- comparing numbers


Quote:
>I am really new to C... I am trying to have a user input 5 numbers then print
>the largest integer and the smallest integer.  I understand how to do it with 2
>numbers.  Any help would be useful.. Thank You

1) learn about arrays
2) learn about loops (for-loops will do for this program)
3) write a for loop to initialise an array of 5 ints.
Then either:

4a) learn about qsort - a bit heavy at this stage

or

4b)Think about how you could iterate (work on each element) over your
array keeping track of the smallest value so far. Then do the same for
the largest value.

--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
--



Mon, 15 Sep 2003 05:49:17 GMT  
 really new to C -- comparing numbers

Quote:

> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with 2
> numbers.  Any help would be useful.. Thank You

Not really C-specific, but...

Compare the first two numbers and save the smaller one.  Then, compare
that result with the next number and save the smaller one.  And so on,
until you have compred them all.  The result is the smallest value.

--

+---------+----------------------------------+-----------------------------+

|    J.   |                                  |  herein are not necessarily |
|  Brody  | http://www.bestweb.net/~kenbrody |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
GCS (ver 3.12) d- s+++: a C++$(+++) ULAVHSC^++++$ P+>+++ L+(++) E-(---)

    DI+(++++) D---() G e* h---- r+++ y?
--



Mon, 15 Sep 2003 09:51:58 GMT  
 really new to C -- comparing numbers

Quote:

> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with 2
> numbers.  Any help would be useful.. Thank You
> --


What you need to do is declare two integers:

int smallest;
int largest;

Load the first number the user input into both of them,
and then compare each of the rest with these two ints, replacing the
value as necessary.  See the following pseudocode:

for (each number the user has input)
{
  if (the number < smallest)
    smallest = the number;
  if (the number > largest)
    largest = the number;

Quote:
}

Give it a try, and post it up when you've coded it, so we can offer
further help.

Micah

--
A man's pride shall bring him low: but honor shall uphold
the humble in spirit.
                                        Proverbs 29:23
--



Mon, 15 Sep 2003 09:53:24 GMT  
 really new to C -- comparing numbers
1) Use scanf or a similar function to put the user input into 5 long ints.
2)  Declare a long called largestInteger (or something similar)
3)  Move the first integer to largestInteger
4)  Compare the second integer to largestInteger.  If second is bigger than
largestInteger, move second to largestInteger.
5)  Repeat step 4 for third, fourth and fifth integers.
6)  Output largestInteger.

You also might want to read up on various sorting algorithms and functions.
--



Mon, 15 Sep 2003 10:05:13 GMT  
 really new to C -- comparing numbers

Clearly a homework question, so you get no code.  I hope you still find
this useful.

Quote:
> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with
> 2
> numbers.  Any help would be useful.. Thank You

If those are the requirements, then you don't need more than 2 numbers.
Create the two output variables (int largest, smallest;) and do a 5
iteration loop to get input, comparing and changing largest and
smallest if the input is larger or smaller.  No need to bother with an
array and sorting or any more complex a solution.
--



Mon, 15 Sep 2003 10:18:28 GMT  
 really new to C -- comparing numbers

Quote:

> I am really new to C... I am trying to have a user input 5 numbers then print
> the largest integer and the smallest integer.  I understand how to do it with 2
> numbers.  Any help would be useful.. Thank You

    Marie,

    If you compare two numbers, put the larger one in biggest and the smaller one
in smallest.  Then compare each subsequent number with biggest and smallest,
replace biggest or smallest when necessary.

--Jeff Turner
--



Mon, 15 Sep 2003 10:18:13 GMT  
 really new to C -- comparing numbers


    >>
    >> I am really new to C... I am trying to have a user input 5
    >> numbers then print the largest integer and the smallest
    >> integer.  I understand how to do it with 2 numbers.  Any help
    >> would be useful.. Thank You

    PB> how about something like this?

    PB> #include <stdio.h>

    PB> int main(int argc, char *argv[]){
    PB>         int big = 0;
    PB>         int i;
    PB>         for(i = 0; i < argc; i++){
    PB>                 if(atoi(argv[i]) > big)
    PB>                         big = atoi(argv[i]);
    PB>         }
    PB>         printf("the biggest number was %d\n", big);
    PB> }
    PB> --

What happens if all of the numbers are negative?  Try this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
   int big = atoi(argv[0]);
   int i;
   for (i=1; i < argc; ++i) {
      if (atoi(argv[i]) > big) {
         big = atoi(argv[i]);
      }
   }
   printf("the biggest number was %d\n", big);

Quote:
}

Cheers,
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey
--



Mon, 15 Sep 2003 10:21:54 GMT  
 really new to C -- comparing numbers

Quote:
> I am really new to C... I am trying to have a user input 5 numbers then
print
> the largest integer and the smallest integer.  I understand how to do it
with 2
> numbers.  Any help would be useful.. Thank You

Let xi represent the element of the set at position i.
Let L(i) represent the largest of x0..xi
Let S(i) represent the smallest of x0..xi

It can be shown that:
L(i) is the larger of xi and L(i-1)
S(i) is the smaller of xi and S(i-1)

Furthermore:
L(1) is the larger of x0 and x1
S(1) is the smaller of x0 and x1

This lends itself nicely to a recursive solution, although an iterative
solution is possible, if less elegant.
--



Mon, 15 Sep 2003 10:22:01 GMT  
 really new to C -- comparing numbers

Quote:


> > I am really new to C... I am trying to have a user input 5 numbers then
> > print the largest integer and the smallest integer.  I understand how to
> > do it with 2 numbers.  Any help would be useful.. Thank You

> how about something like this?

> #include <stdio.h>

> int main(int argc, char *argv[]){
>         int big = 0;
>         int i;
>         for(i = 0; i < argc; i++){
>                 if(atoi(argv[i]) > big)
>                         big = atoi(argv[i]);
>         }
>         printf("the biggest number was %d\n", big);
> }

How about:

#include <stdio.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    int big = INT_MIN;
    int i;

    for(i = 0; i < argc; i++){
        int temp    = atoi(argv[i]);
            if(temp > big)
                big = temp;
    }

    printf("the biggest number was %d\n", big);

    return 0;

Quote:
}

--
http://www.whereismyhead.com/clark/
Clark S. Cox, III

--



Wed, 17 Sep 2003 07:52:59 GMT  
 really new to C -- comparing numbers

Quote:

> #include <stdio.h>
> #include <stdlib.h>

> int main(int argc, char **argv) {
>    int big = atoi(argv[0]);
>    int i;
>    for (i=1; i < argc; ++i) {
>       if (atoi(argv[i]) > big) {
>          big = atoi(argv[i]);
>       }
>    }
>    printf("the biggest number was %d\n", big);
> }

What happens if the name of the program is "1000000"?  (Think about
argv[0].)

What happens (and what should happen) if no numbers are given?

Assuming a C90 implementation, what status does the program return to
the environment?

The original problem statement called for user input; does examining
command-line arguments qualify?

(If the original problem statement didn't look suspiciously like a
homework assignment, I'd provide answers to these questions.)

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
MAKE MONEY FAST!!  DON'T FEED IT!!
[The line above, part of the .signature, nearly caused this post to get
 bounced. -mod]
--



Wed, 17 Sep 2003 07:54:01 GMT  
 really new to C -- comparing numbers

Quote:

>    int big = atoi(argv[0]);

The user will be surprised... :-)
--



Wed, 17 Sep 2003 07:56:19 GMT  
 really new to C -- comparing numbers
Actually, not homework.. I'm trying to learn this on my own with a book so it
is kind of challenging.. but thank you for the help
--



Sat, 20 Sep 2003 05:37:55 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Help--need really fast search to compare two HUGE files of text records

2. word - automatic numbering/bold/underline/italics

3. Form1.cs on new applications

4. Newbie: separate big .cs file into small .cs files

5. I'm really, really, really new at this.

6. Help: Problem with comparing floating point numbers (example source code and output is given)

7. How do I store a really big number?

8. Really Easy Random Number Question

9. Comparing 7 inlined RNG codes /Re: Timing differences for a random number generator

10. Powers and square roots for really large numbers

11. - Comparing input numbers

12. Help: Problem with comparing floating point numbers (example source code and output is given)

 

 
Powered by phpBB® Forum Software