easy question for some.. however

This is a discussion on easy question for some.. however within the C Programming forums, part of the General Programming Boards category; im suck.. I have an idea but i need a boot in the bum to get me going in the ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    easy question for some.. however

    im suck.. I have an idea but i need a boot in the bum to get me going in the right direction.
    here is the question any thoughts or ideas let me know.. im struggling to get my head round a particular bit.

    Question4.
    -----------
    write a C program to produce the following output.

    ****
    ***
    **
    *

    your program should contain 2 for loops and only one '*'.
    -------------

    i am thinking of a nested loop.. i think i would want my first one to be a line controll
    and the second to print the stars.
    I am going to have two or three variables as far as i can see.. but im unsure how to do this.. I dont want the answer i would like suggestions or tips to help me understand the problem.. my whole issue with programing is the problem solving part.. i need some practice. :S

    thanks for any help offered.
    Last edited by webznz; 10-23-2007 at 08:50 PM. Reason: spelling

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You've already got the idea, what more do you want?

    Have 3 variables, a counter for the outer loop, and a counter for the inner loop, and a 'stars' variable for the inner loop (how many stars to print, which decreases by 1 on each line -- each iteration of the outer loop).

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    okay, i been trying to figure it out but not sure how to duplicate the star variable or initialize it to 4 stars then minus one after each iteration of the for loop.... suggestions?

    Code:
    #include <stdio.h>
    
    char stars = '*';
    int a,b;
    
    int main(){
    	for (a=0;a<5;a++){
    		for (b=0;b<4;b++){
    		/*dont know what to put here*/
    		}
    		printf ("%s\n",stars);
    	}
    }

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The inner loop shouldn't go to 4. BTW &#37;s is for strings, where stars is a char.

    This is what I mean:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 0,
            b = 0,
            lines = 4;  /* initial # of lines */
        
        for(a = lines; a > 0; a--)          /* lines */
        {
            for(b = 0; b < a; b++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        
        return 0;
    }
    There are a few ways to do this.
    Last edited by zacs7; 10-23-2007 at 11:46 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    oh thats it! nice work.. i completely overlooked putchar

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by webznz View Post
    oh thats it! nice work.. i completely overlooked putchar
    Don't need to use putchar, you can use printf("*") or printf("%c", stars) for example.

    ["stars" is a misnomer, since the variable holds only ONE star!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 08:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 11:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 01:19 PM

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