Thread: A beginner question.

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    2

    A beginner question.

    Hello all,

    So I am teaching myself C to gain a better understanding of programming, I figured this was the best place to start. Using online courses I've been watching videos and completing lab assignments based on what I've learned. Unfortunately, there is no sort of answer key if I am to get stuck. If you could take the few minutes to help me out here it would be greatly appreciated. To any intermediate programmer, I imagine this question should be cake. The question is as follows:

    "Create a third c project. Using a for loop (or for loops)create the following pattern in your output. Note that youshould never output more than one asterisk within a printf()statement in your solution:
    1: *
    2: **
    3: ***
    4: ****
    5: *****
    6: ******
    7: *******
    8: ********
    9: *********
    10: **********
    11: ***********
    12: ************
    13: *************
    14: **************
    15: ***************

    Thank you to all who help me out!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    It sounds like you should have at least been introduced to "for()" loops at this point.

    Post your best attempt, and let us know specifically where you're stuck.

    >> Using a for loop (or for loops)...

    That's actually a pretty good clue.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Think like this. Each line has star (*) and space ( ). You need to find proper equations to print them in a loop.

    For example:

    First line: 1 star 14 spaces
    Second line: 2 stars 13 spaces
    .
    .
    .
    Fifteenth line: 15 stars 0 spaces

    It's easy to see the equations between them. Try it out and post your code if you need further help.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    2
    Code:
    for (int x=0; x<16; x=x+1)
        {
            printf("%d: *", x);
            printf("\n");
        }
    This is what I have been using for the initial number. The lesson i watched included only integers in the for loops. Am I able to use a char object? Do i need to? The repeating star is the only thing i can't figure out.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    Quote Originally Posted by meatloaf View Post
    Code:
    for (int x=0; x<16; x=x+1)
        {
            printf("%d: *", x);
            printf("\n");
        }
    This is what I have been using for the initial number. The lesson i watched included only integers in the for loops. Am I able to use a char object? Do i need to? The repeating star is the only thing i can't figure out.
    Try my following suggestions:
    Code:
       // In this case, start x at 1 not 0
       // x++ or ++x instead of x=x+1, more efficient
       
       for (int x=0; x<16; x=x+1) 
        {
           printf("%d: ", x);
           
           // Add a second for() loop here to print the multiple '*'
    
           printf("\n");
        }
    I would align the ':' to make it look cleaner, but not part of the initial assignment you posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. Beginner's question
    By Innos in forum C++ Programming
    Replies: 66
    Last Post: 10-08-2013, 07:57 AM
  3. beginner question
    By ShaiAdar in forum C Programming
    Replies: 7
    Last Post: 03-24-2009, 04:55 AM
  4. Another beginner question
    By endo in forum C# Programming
    Replies: 6
    Last Post: 03-31-2005, 11:57 AM
  5. Beginner question on a 'While'
    By kev_inflames in forum C++ Programming
    Replies: 6
    Last Post: 09-22-2004, 08:13 PM

Tags for this Thread