Thread: Basic C Homework Question - likely easy for you but sending me for a loop

  1. #1
    cnoob()
    Join Date
    Mar 2015
    Posts
    28

    Question Basic C Homework Question - likely easy for you but sending me for a loop

    Reading through the board I've found people prefer verbatim what the professor is looking for. I'm not looking for someone to answer this for me just point me towards the right direction as I'm at a loss as to where to start with the code.

    "your program will draw the following figure on the screen using a combination of spaces and asterisks" (_ = space)

    _*****
    __*****
    ___*****
    ____*****
    _____*****
    _____*****
    _____*****
    _____*****
    ____*****
    ___*****
    __*****
    _*****
    _*****
    _*****
    _*****
    __*****
    ___*****
    ____*****

    2. you will use the definition of function main shown below (do not change this function)
    Code:
    int main (void) {
       int i = 0;
       while (i < 41) {
          print_line (sin(i/(3.1416*2))*20+21,5);
          i = i + i;
       }
       return 0;
    }
    ------
    my question is so far I know I need to create another function called "print_line" but I'm simply lost as to how to proceed.. I've searched through the forums and around the net for something similar to get ideas however I've been unable to find something. I'm not looking for anyone to do my work for me I need to / want to complete this myself I just don't know how to start. Thanks for any help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably made a typo error, i.e., this:
    Code:
    i = i + i;
    probably should be:
    Code:
    i = i + 1;
    As an experiment, I suggest that you write print_line to print its first argument, then example the print out for a pattern to understand. See if you can relate this pattern to the figure that is supposed to be printed. The second argument to print_line is probably the number of asterisks to print, so that should be simple.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    solid, yea I notice I did mess that up when typing to the page however in the program it is correct. I'll start down that path and see if I can make some sense of it. do you think I'll need to create a print_line function end it and then create another like mymain in order to call the print_line and main functions into play or should I be able to do all the additional coding within print_line?
    thank you for the help and advice

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should be able to do everything in the "print_line()" function - i.e. the only functions your program should contain are the given "main()" and "print_line()".

  5. #5
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    Just trying to lay the code down and start seeing the mistakes along the way and the first thing I'm getting in the compiler is "
    Code:
    $ gcc -ansi -std=c99 -pedantic -ggdb -lm program5.c
    program5.c: In function ‘main’:
    program5.c:24:8: error: too many arguments to function ‘print_line’
            print_line (sin(i/(3.1416*2))*20+21,5);
            ^
    program5.c:7:5: note: declared here
     int print_line(int i) {
         ^
    "
    Code:
    # include <stdio.h>
    # include <math.h>
    
    
    
    
    int print_line(int k) {
       k = 0;
       while (k < 41) {
          printf(" ");
          k = k + 1;
       }
       printf("****\n");
    
    
       return 0;
    }
    
    
    
    
    
    
    
    
    int main (void) {
        int i = 0;
        while (i < 41) {
           print_line (sin(i/(3.1416*2))*20+21,5);
           i = i + 1;
        }
        return 0;
    }
    sooooo... I'm confused in what way it's telling me I'm dumb

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    print_line is supposed to have two parameters, but you defined it with only one parameter. As I observed, I believe the second parameter is for the number of asterisks to print per line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    thank you for the quick reply! that makes sense thus the error "to many arguments too function." it wants two I gave it one. ok the confusing part about that is the professors "suggested strategy" seems to imply only defining the function to accept one integer argument and the number of asterisks is set at 4 (though his image shows 5 on each line). Basic C Homework Question - likely easy for you but sending me for a loop-capture-jpg

    I added in the second argument to the print_line function however now I'm just getting 41 rows all evenly spaced with 41 spaces before it... not the glorious curved lines that pi is set to produce. I assume now I need to use the main function to alter the spaces instead of having them be static

    Also on this subject I only see 1 parameter in print_line? the int i... what is the second parameter it's looking for or why is it insisting on two parameters? I'd like to understand the issue not just fix it. Thank you again for the time
    Last edited by SandyMan; 03-28-2015 at 01:48 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SandyMan
    ok the confusing part about that is the professors "suggested strategy" seems to imply only defining the function to accept one integer
    Perhaps your professor wanted you to experiment with a simpler version of print_line first, i.e., one that assumes 4 asterisks per line. Otherwise, that "suggested strategy" is inconsistent with how print_line is called in the sample main function. (Also, I note that it says "first function argument": "first" would be redundant if there were only one parameter.)

    Quote Originally Posted by SandyMan
    I assume now I need to use the main function to alter the spaces instead of having them be static
    You quoted your assignment instructions earlier: "you will use the definition of function main shown below (do not change this function)". Therefore, no, changing the main function is the wrong way to go. Rather, do not change k in print_line. Use k, not the magic number 41, to control the loop. 41 has nothing to do with print_line: your professor could change it in the main function to 42, 100, or 24601 without telling you and your protests will be summarily ignored.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    I misspoke, by use the function to alter the spaces I meant point to the function and have function main set the spaces for me, not edit function main. However thank you for the clarity and caution. Originally I set 41 to frame the code structure but now it needs to become dynamic, I'll work on my functions and work with K as you suggested, your help is appreciated.

  10. #10
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    Code:
    # include <stdio.h>
    # include <math.h>
    
    
    
    
    int print_line(int spaces, int five) {
       while (spaces > 0) {
          printf(" ");
          spaces = spaces - 1;
       }
       printf("****\n");
       return 0;
    }
    
    
    int main (void) {
        int i = 0;
        while (i < 41) {
           print_line (sin(i/(3.1416*2))*20+21,5);
           i = i + 1;
        }
        return 0;
    }
    should do it. What is the point of the "5" in the second argument of the print_line in the main function? It looks like that would give some sort of constant but I don't see it being implemented anywhere? Maybe a constant minimum of 5 spaces? Seems unnecessary.

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by SandyMan View Post
    Code:
    …
        k = 0;
        while (k < 41) {
            printf(" ");
            k = k + 1;
        }
    …
    Your while-loop iterate allways to 41, but this should be variable (the value from the function argument).

    Hint: you can see what the function receive as argument with:
    Code:
    int print_line (int l, int m) {
        printf("-> l=%d / m=%d\n", l, m);
        return 0;
    }
    Last edited by WoodSTokk; 03-28-2015 at 03:18 PM.
    Other have classes, we are class

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SandyMan
    What is the point of the "5" in the second argument of the print_line in the main function? It looks like that would give some sort of constant but I don't see it being implemented anywhere? Maybe a constant minimum of 5 spaces? Seems unnecessary.
    I have told you twice that I think the second argument is the number of asterisks to print per line. The only way to be sure is to ask your professor.

    Quote Originally Posted by WoodSTokk
    Your while-loop iterate allways to 41, but this should be variable (the value from the function argument).
    I made this observation in post #8.

    Quote Originally Posted by WoodSTokk
    Hint: you can see what the function receive as argument with:
    I made this suggestion in post #2. Of course, a debugger could work just as well.
    Last edited by laserlight; 03-28-2015 at 03:24 PM.

  13. #13
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    Quote Originally Posted by laserlight View Post
    I have told you twice that I think the second argument is the number of asterisks to print per line. The only way to be sure is to ask your professor.
    I'm very new to C and didn't fully understand the roles each argument had in the end result. Now that I've pieced it apart and understand it better your observation makes sense. I e-mailed the professor asking him for clarification on the # of asterisks he would like between 5 or 4 (5 in his image, 5 in the argument, 4 in the instructions) which should clear some things up. For now I will write the program to use that second argument as the # of asterisks. Thanks!

  14. #14
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    Quote Originally Posted by WoodSTokk View Post
    Your while-loop iterate allways to 41, but this should be variable (the value from the function argument).

    Hint: you can see what the function receive as argument with:
    Code:
    int print_line (int l, int m) {
        printf("-> l=%d / m=%d\n", l, m);
        return 0;
    }

    Thank you for the help yes that was my original code that was worse than my current not very good code. I have since changed it to
    Code:
    while (spaces > 0)
    thanks for the code to use to see what the function receives as an argument, I'll play with that to help learn. Take care!!

  15. #15
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I see a second problem in your code (don't no if you have fixed it already).
    Quote Originally Posted by SandyMan View Post
    Code:
    int print_line(int k) {
        k = 0;
        while (k < 41) {
    …
    (This is the old code with only one argument, but it shows the problem)
    You take the argument 'k' and then you set 'k' to a new value.
    If you need a variable for iteration in the while-loop, take a new one.
    Code:
    int print_line(int k) {
           int i = 0;
           while (i < k) {
    …
    To secure your self to alter the arguments, you can take it as constant like:
    Code:
     void print_line (const int l, const int m) {
    …
    With the 'const' prefix, you receive an error while compiling if you alter one of the constant variable.
    Last edited by WoodSTokk; 03-28-2015 at 03:46 PM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy C question on problem with loop/output
    By prafiate in forum C Programming
    Replies: 12
    Last Post: 06-10-2012, 04:01 PM
  2. easy loop question
    By ashlee in forum C Programming
    Replies: 4
    Last Post: 10-16-2010, 03:14 PM
  3. Easy While Loop Question.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2005, 09:58 AM
  4. Have a really basic easy question..
    By weezer562 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2005, 08:25 PM
  5. I have a 2 easy question about some homework?
    By correlcj in forum C Programming
    Replies: 3
    Last Post: 07-18-2002, 07:28 PM