Thread: Help with grading a asterisk triangle corresponding to different grade ranges.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Help with grading a asterisk triangle corresponding to different grade ranges.

    Help needed!
    so far:
    insert
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
        int i, j, range;
        int begin = 0;
        for (i = 1; i <= 10; ++i)
        {
            range = begin + 9;
            printf("%d -- %d : ", begin, range);
            for (j = 1; j <= i; ++j)
                printf("*");
                printf("\n");
        }
        return EXIT_SUCCESS;
    }
    Output right now:

    0 -- 9 : *
    0 -- 9 : **
    0 -- 9 : ***
    0 -- 9 : ****
    0 -- 9 : *****
    0 -- 9 : ******
    0 -- 9 : *******
    0 -- 9 : ********
    0 -- 9 : *********
    0 -- 9 : **********




    Needed output:

    0 -- 9 : *
    10 -- 19 : **
    20 -- 29 : ***
    30 -- 39 : ****
    40 -- 49 : *****
    50 -- 59 : ******
    60 -- 69 : *******
    70 -- 79 : ********
    80 -- 89 : *********
    90 -- 99 : **********


    Any suggestions on how to get the range to change each time? Thanks, Liz

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You need to increment begin, right now it's always 0.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
        int i, j;
        int begin = 0;
        int range = 0;
        for (i = 1; i <= 10; ++i)
        {
            begin = range;
            range = begin + 10;
            printf("%d -- %d : ", begin, range);
            for (j = 1; j <= i; ++j)
                printf("*");
                printf("\n");
        }
        return EXIT_SUCCESS;
    }
    NOW OUTPUT:

    0 -- 10 : *
    10 -- 20 : **
    20 -- 30 : ***
    30 -- 40 : ****
    40 -- 50 : *****
    50 -- 60 : ******
    60 -- 70 : *******
    70 -- 80 : ********
    80 -- 90 : *********
    90 -- 100 : **********

    Alright, so I incremented "begin" but I need to the range to start from 0 -- 9; 10 -- 19... etc. I can't quite figure out how to get it to that range....

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Go back to range = begin + 9. Then you need to increment begin by 10 in each loop, not set it to the last value of range.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elizabethafairl View Post
    Alright, so I incremented "begin" but I need to the range to start from 0 -- 9; 10 -- 19... etc. I can't quite figure out how to get it to that range....
    [/COLOR]
    Rather than "range" you want to print "range-1" (or, as anduril462 says, begin+9).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you use a %2d format for printf(), the numbers (begin and begin+9) of 0 and 9, will line up properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-29-2011, 12:58 PM
  2. declaring ranges a-z A-Z 0-9 etc. ?
    By kezkez in forum C Programming
    Replies: 1
    Last Post: 07-01-2010, 05:48 PM
  3. Asserts and their ranges
    By Matamoros123 in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2006, 12:32 PM
  4. C Ranges
    By EvoTone in forum C Programming
    Replies: 20
    Last Post: 09-28-2006, 05:40 AM
  5. vector<double> ranges
    By correlcj in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2002, 12:49 PM