help with output from array

This is a discussion on help with output from array within the C Programming forums, part of the General Programming Boards category; I have everything right in this program except for the lack of one line of output. The first line of ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    help with output from array

    I have everything right in this program except for the lack of one line of output. The first line of the output should have the range 1-9, but I can't figure out how to do that and have the rest print out as well. Here's the code and the output.
    Code:
    void showOutput(int range[], int input)
    {
       int primeCount = 0;
       
       // print the headers for the table
       printf("\n%11s","Range of\n");
       printf("%9s%25s%19s\n", "Primes","Prime Numbers","Quantity");
       printf("%11s%25s%17s\n", "------------","-------------------","----------");
       
       
       for(int i = 1; i < input; i++)
       {
           // separate the values in blocks of 10
           if(i % 10 == 0)
           {
              
    	  // show the current range
    	  printf("%4d%2s%5d%6c",(i)," -", (i+9), ' ');
    	  
    	  // find the primes within the range and print them
    	  for(int j = i; j <= (i+9); j++)
    	      if(range[j]==1)
    	      {
    		  printf("%5d",j);
    		  primeCount++;
    	      }
    	      
    	  // to make sure that the quantity column's value are lined up,
    	  // blank spaces are added based on how many primes were printed
    	  // for that set of 10 integers
    	  for(int k = primeCount; k < 4; k++)
    	          printf("%5c",' ');
              
    	  // print the number of primes in the set of 10
    	  // and reset the counter for the next group of 10
    	  printf("%12d", primeCount);
    	  primeCount = 0;
    	  
    	  // since all of the above data was on one line,
    	  // print a new line for the next set
    	  printf("\n");
           }
       }
    }
    int main( )
    {
       int input = 0, currentSpace = 0;
       
       printf("Enter the value of N: ");
       scanf("%d",&input);
       
       // define a large range of integers to test for primes
       int range[500];
       
       // set all elements to 1
       for(int i = 0; i <= input; i++)
           range[i] = 1;
       
       range[0] = range[1] = 0;
       
       for(int i=2;i <=input; i++)
       {
           if(range[i] == 1)
           {
              // if the element contains a 1
    	  // remove all multiples of the number
    	  // and make sure the multiples
    	  // don't go above the user's input
    	  currentSpace = i+i;
              while(currentSpace < input)
              {
                   // set the value to zero
    	       range[currentSpace] = 0;
    	       
    	       // set the variable to remove the next multiple
    	       currentSpace += i;
    	  }
           }
       }
       
       // call the function to print the formatted output
       showOutput(range,input);
           
           
       return (EXIT_SUCCESS);
    }
    Code:
    bash-2.05$ ./primes
    Enter the value of N: 200
    
      Range of
       Primes            Prime Numbers           Quantity
    ------------      -------------------       ----------
      10 -   19         11   13   17   19           4
      20 -   29         23   29                     2
      30 -   39         31   37                     2
      40 -   49         41   43   47                3
      50 -   59         53   59                     2
      60 -   69         61   67                     2
      70 -   79         71   73   79                3
      80 -   89         83   89                     2
      90 -   99         97                          1
     100 -  109        101  103  107  109           4
     110 -  119        113                          1
     120 -  129        127                          1
     130 -  139        131  137  139                3
     140 -  149        149                          1
     150 -  159        151  157                     2
     160 -  169        163  167                     2
     170 -  179        173  179                     2
     180 -  189        181                          1
     190 -  199        191  193  197  199           4

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I would suggest having your lists go like so:

    Code:
    1  - 10
    11 - 20
    21 - 30
    Then change your check statement to:
    Code:
    if((i-1)%10)
    {
        ...
    }
    The other way would be to simply create a 1-9 row before you start your for loop.
    Don't quote me on that... ...seriously

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Be careful of your uses of C++ for loop declarations. This would never compile on a C compiler.
    Last edited by SlyMaelstrom; 10-27-2005 at 03:33 AM.
    Sent from my iPadŽ

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You're saying something that compiles C99 is not a C compiler? How odd.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Whoops, my sentence is backwards. My brain compiler didn't pick up the logical error.
    Sent from my iPadŽ

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    But once you include the headers, which I assume he's just omitted for brevity, it is valid C99, and hence valid C. He is using C99 comments/declarations.

    Of course, it won't compile in a C89 compiler, which are still extremely common.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Ok you win.

    *sits in the corner*

    ...

    ...

    ... *weeps*
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 04:56 AM
  2. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM

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