Thread: Help writing a program

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    Help writing a program

    I need help writing a program. The program calls for a user to be asked a width and height to make a table of asterisks. The height can ONLY be 3 or 5, and the width can be between 4-40. The table must be out of asterisks. Here is the program I have so far, except i need to replace the word "width" with the desired width of asterisks...How can I do this?

    Code:
    "pe3.c" 23 lines, 228 characters
         6  #include <stdio.h>
         7
         8  int main(void)
         9
        10  {
        11
        12  int height, width;
        13
        14  printf("Enter width:");
        15  scanf("&#37;d", &width);
        16  printf("Enter height:");
        17  scanf("%d", &height);
        18
        19  if (height == 3)
        20  printf("%d\n", width);
        21  printf("*%d*\n", width);
        22  printf("*%d*\n", width);
        23
        24  return 0;
        25
        26  }


    This is what the program is supposed to make:

    Code:
    Enter width: 20
    Enter height: 3
    ********************
    *                  *
    *                  *

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll need a loop. Do you know how to code a "for" loop?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    The instructor said I would need a loop. Can you give me an example of what I need to do?At the rate I am going I will be here all night. What I am doing is wrong I think.

    Code:
         6  #include <stdio.h>
         7
         8  int main(void)
         9
        10  {
        11
        12  int height, width;
        13
        14  printf("Enter width:");
        15  scanf("&#37;d", &width);
        16  printf("Enter height:");
        17  scanf("%d", &height);
        18
        19  if (width == 4)
        20  printf("****/n");
        21  else if (width == 5)
        22  printf("*****/n");
        23  else if (width == 6)
        24  printf("******/n");
        25  else if (width == 7)
        26  printf("*******/n");
        27  else if (width == 8)
        28  printf("********/n");
        29  else if (width == 9)
        30  printf("*********/n");
        31  else if (width == 10)
        32  printf("**********/n");
        33  else if (width == 11)
        34  printf("***********/n");

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Loops would work something like this.

    Code:
    ...
    int i = 0;
    int j = 0;
    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++) {
            printf("*");
        }
        printf("\n");
    }
    ...
    Something along those lines should work.
    What is C++?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    I understand the for loop. My problem is trying to work with * rather than a number...Can I set an int such as width = * /

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Well the number will tell your loop how many times to loop. Then inside the loop you can print your "*". This will print the "*" every iteration.

    So when you get the users input, and you get the width, you lloop width amount of times and print "*".
    What is C++?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Ok but wouldn't this leave more of a "filled box shape"? I need the inside to be empty so it looks like a table

    The code you sent me should output this I believe


    Code:
    Enter width:10
    Enter height:3
    **********
    **********
    **********
    Needed

    Code:
    Enter width: 10
    Enter height: 3
    **********
    *        *
    *        *

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    putc(' '); will print a space character
    putc('*') - star cgharacter - you should mix them for the 2nd and futher lines
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's a basic "for" loop. I'll write it, then I'll explain it.

    Code:
    int i ; 
    
    for ( i = 0 ; i < width ; i++ ) 
    {
        do-one-thing... ; 
    }
    
    some-next-instruction ;
    The code in RED is required for every "for" loop. (There is a shortcut to leave off the curly braces, but I won't get into that).

    The "for" says to loop some number of times. The parens enclose the INITIALIZER, the CONDITION and the INCREMENT, in that order.

    To start the loop, you set up an initializer. It is typical in a loop to control it with an INT. Here, variable i is the int. We set it with an initial values of 0. This is the most common thing to do.

    The flow of execution is this:
    1) the initializer is performed (this is only done once)
    2) the condition is checked
    3) if the condition is TRUE, then the code between the braces is executed. If the condition if false, then the loop is done, and control passes to the next instruction after the last curly brace.
    4) If the code between the braces was executed because the condition was true, then the increment is performed. The process repeats again, starting with step 2 above.

    So, let's take a simple example. Let's say you wanted to do the same thing (like print a '*') 5 times. You would initialize i to 0, you would set a condition for i to be less than 5 (because counting from 0 to 4 is "5 times"), and you would set an increment to bump i by one each loop.

    Does that make sense?

    This is just the very basics, but should be enough to get you going in the right direction.

    Todd

    EDIT - if not, hopefully the other posters who posted will have helped - who posted while I was writing this!! Man, you guys are fast!
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Can I put whatever I want where the green text is? I need the width to be between 4 and 40.

    So i == 4 < width < 40 ?

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You should validate the width prior to entering the loop.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Ok thank you. Now I just have to work on where to put the printf(" "); at.

    Thanks

    Code:
         6  #include <stdio.h>
         7
         8  int main(void)
         9
        10  {
        11
        12  int height, width;
        13  int i = 0;
        14  int j = 0;
        15
        16  printf("Enter width:");
        17  scanf("&#37;d", &width);
        18  printf("Enter height:");
        19  scanf("%d", &height);
        20
        21  for (i = 0; i < height; i++) {
        22          for(j = 0; j < width; j++) {
        23                  printf("*");
        24          }
        25          printf("\n");
        26  }
        27
        28  printf("PROGRAM ENDS\n");
        29  return 0;
        30
        31  }
    Last edited by alex1067; 04-02-2008 at 11:55 PM.

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    When you print the *, compare j and width to see where you are.

    If i is 0, don't worry about the check, after that, if j is equal to 0 or j is equal to width you can print *, else you can print ' '.
    What is C++?

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Can anyone please show me where to put the printf(" ") in my code?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make a flowchart, translate your code into the flowchart, insert appropriate spaces into flowchart, translate into code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM