Calculating the Range

This is a discussion on Calculating the Range within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> static void range ( int low, int high, FILE *out ) { while ( low ...

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

    Question Calculating the Range

    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;
    }
    Isn't there a much easier way to calculate the range of numbers?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    for(int i=low;i<=high;i++) fprintf( out, "%d ",i);
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Isn't there a much easier way to calculate the range of numbers?
    It depends on what you mean by calculate the range. If you only want to print from low to high then that code (which looks like my style ) is as simple as you can get while maintaining some semblance of generality and safety. Of course, you could simplify the concepts to bare bones:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int low, high;
      
      printf ( "Enter two numbers numbers (ex. low high): " );
      scanf ( "%d %d", &low, &high );
      
      while ( low <= high )
        printf ( "%d ", low++ );
      
      return 0;
    }
    But this is terribly unsafe and the output will likely be different on different systems.

    -Prelude
    My best code is written with the delete key.

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

    Question

    Yeah, that is your code, Prelude. Exactly what is your style? You seemed to recognize it pretty quick.

    Oh, and where is everyone getting those "I was born" avatars?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Exactly what is your style? You seemed to recognize it pretty quick.
    I use an unusually large amount of whitespace between tokens and logical blocks for readability. No one else I've seen does this to the same extent with the K&R bracing style.

    >Oh, and where is everyone getting those "I was born" avatars?
    I don't know about everyone, but I had it floating around on my hard drive. I can't recall where I found it originally.

    -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, 03: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, 04:24 PM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21