Thread: Range

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy Range

    How do you calculate the range of numbers using C?

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I want to write a program that displays the lowest number to highest number from a set of numbers that was entered.

  3. #3
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    Code:
    get the numbers
    min = first number
    while there are more numbers to be checked
    {
       if the next number is lower than min
          set min to the next number
    }
    max = first number
    while there are more numbers to be checked
    {
       if the next number is higher than max
          set max to the next number
    }
    that wasn't so bad, now was it?
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do you calculate the range of numbers using C?
    Simple enough, just take the range and then print the lowest to the highest.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static void range ( int low, int high, FILE *out )
    {
      while ( low <= high ) {
        fprintf ( out, "%d ", low++ );
        fflush ( out );
      }
    }
    
    int main ( void )
    {
      int low, high;
    
      printf ( "Enter a range of numbers (ex. 3-12): " );
      fflush ( stdout );
    
      if ( scanf ( "%d %*c %d", &low, &high ) != 2 ) {
        fprintf ( stderr, "Invalid input\n" );
        exit ( EXIT_FAILURE );
      }
    
      range ( low, high, stdout );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 03:24 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM