Thread: Defining functions in C and using function arguments

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Defining functions in C and using function arguments

    I need some help, I almost have this figured out for an assignment.

    I need to have a program that writes this:

    ______********
    ____******
    __****
    **
    __****
    ____******
    ______********

    The _ is blank space, its suppose to work with what ever integer value I put in, but all I ever get is 7 lines and it looks just like this.

    Can anyone see where I am wrong? I have been messing with this for hours, I am by no means a programmer! Any help would be awesome!!!


    Code:
    #include <stdio.h>
    #include "csc1325.h"
    
    
    int print_line (int x, int y)  {
       while (x >= 1)  {
          printf(" ");
          x = x - 1;
       }
       while (y >= 1)  {
          printf("*");
          y = y - 1;
       }
       printf("\n");
       return 0;
    }
    
    
    int main (int argc, char * argv[]) {
       int i;
       i = 3;
       while (i >= 0) {
          print_line(i*2, i*2+2);
          i = i - 1;
       }
       i =  1;
       while (i <= 3) {
          print_line(i*2, i*2+2);
          i = i + 1;
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    when you change "i=3" in main to something else, i.e. "i=5", the second while loop in main is still only going from 1 to 3. this second "3" should probably be set to the initial value of i (5 in this example), which means you need another variable to track the initial value. so you use two variables: "i" (which changes during the two loops) and some constant (which keeps its initial value, so the two loops can use it).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    You mean something like this?

    I apologize for the dumb question, this stuff is not for me, I am trying to survive this class so I can pass, then change majors.





    Code:
       i=1;
       int j;
       j = GetInt(argc, argv, 1);
       while (i <= j) {
          print_line(i*2, i*2+2);
          i = i + 1;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It would have been easier if you had just told us what the program was supposed to do, instead of hoping we could figure out what it is you were trying to draw there.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Question

    My bad, I thought I put it in earlier

    Suppose to look like this

    ______********
    ____******
    __****
    **
    __****
    ____******
    ______********

    2. Your program will define function main as shown below:
    Code:
    int main (int argc, char * argv[]) {
    int i;
    i = 3;
    while (i >= 0) {
    print_line(i*2, i*2+2);
    i = i - 1;
    }
    i = 1;
    while (i <= 3) {
    print_line(i*2, i*2+2);
    i = i + 1;
    }
    return 0;
    }

    The first argument is the spaces and the second is the asteriks, which I am sure you guys can tell, and I have gotten it to come out like the original figure, but its suppose to change as you change your input, no matter what input I put in its always the five lines. But I think I see where we are going, if I have it always set to 3.

    Or maybe I see...

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Is that code for the second while loop? Then yes it would work in some cases. However, you still have the problem of two possibly different values. That is, the first while loop prints the top half of the "picture", and happens i=3 times. The second while loop prints the bottom half of the picture, and happens j=? times (may be 3, in which case the picture will be symmetric and correct, otherwise it wont be). Basically, move the declaration of j up and have something like this (my changes are in red):
    Code:
    int main (int argc, char * argv[]) {
       int j;
       j = // get the value from the user, or simply assign it a value, or whatever way you want
       int i;
       i = j;
       while (i >= 0) {
          print_line(i*2, i*2+2);
          i = i - 1;
       }
       i =  1;
       while (i <= j) {
          print_line(i*2, i*2+2);
          i = i + 1;
       }
       return 0;
    }
    I havent tried it out, but I think it should work. Let us know if you have problems still.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Bluesilver2009
    all I ever get is 7 lines
    Quote Originally Posted by Bluesilver2009
    its always the five lines
    Its always 7. One thing with computers is that you have to be exact and can never leave out details. Leaving out some minor detail (or inconsistency) could change everything. Just FYI for your future posts.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't care what it's supposed to look like. I wanted it in words. You know, like a homework problem description (since I assume that's what it is). Such as:

    Write a function that takes two arguments, the number of spaces and the number of asterisks. Use that function in a loop to count down to X and back up again.

    Something like that. Which it looks like that's what you're doing. See, I'm not really sure what you're doing here, because, if one of these is greater than the other, and you're counting spaces down to 0 and back up again, there's a chance one of these goes negative. Anyway, that sounds like what you're doing, so why don't you just:
    Code:
    line(x,y)
        print x spaces
        print y asterisks
    
    main
        read a number, or maybe two
        count down to zero
            fun( some number, some other number )
        count up again
            fun( some number, some other number )
    I'm still not sure how you're deciding on what numbers to decrease or increase, but that (the above) looks like all you need to be doing.

    Edit: If you really are always (intentionally, only ever) spitting out seven lines every time, then you just need one loop, and you need to simply decrement three times and increment three times.


    Quzah.
    Last edited by quzah; 10-27-2009 at 03:45 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Typo, all I am getting is 7 lines, no matter what integer I input

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I know, I just said to be careful to what you are posting, and make sure it isnt contradictory and is correct with no details left out. Anyway, try out what I suggested above.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Ok this is it, I have it kind of working but I am not sure where the problem is?

    Prints out 7 lines, in the figure its suppose to no matter what integer I input.


    This project will require students to demonstrate the following skills:
    • Define functions in a C program.
    • Use function arguments.

    Description
    Write a program that meets the following requirements:

    1. Your program will draw the following figure on the screen using a combination of spaces and
    asterisks:
    _____********
    ___******
    __****
    **
    __****
    ___******
    _____********

    Code:
    int main (int argc, char * argv[]) {
    int i;
    i = 3;
    while (i >= 0) {
    print_line(i*2, i*2+2);
    i = i - 1;
    }
    i = 1;
    while (i <= 3) {
    print_line(i*2, i*2+2);
    i = i + 1;
    }
    return 0;
    }
    3. You must provide appropriate definitions for function print_line. Each call of this function
    will print a single line of the figure which requires a number of spaces followed by a number of
    asterisks to be printed. The number of spaces to be printed is provided in the first argument
    to print_line. The number of asterisks to be printed is provided in the second argument.


    1. Yor program must be able to print the figure in the previous section in various sizes (number
    of lines).
    2. The size will be one or more and will be an odd number.
    3. Modify function main to take one integer program argument that is the size of the figure.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bluesilver2009 View Post
    Typo, all I am getting is 7 lines, no matter what integer I input
    So? What's that mean? It's suppose to prompt for a number of lines, or what? That's why I keep asking for you to describe exactly how the program is supposed to work. Not draw ascii pictures, but to actually tell us what you mean the program to do:

    "Prompt the user for the number of lines to draw. Draw number / 2 lines counting down, and then back up again again."

    Is that what you're trying to do? Don't draw a picture. Put it in words.

    Edit - the above post came in as I was typing this.
    Now that you have told us what you intend to do, where in your program are you actually asking for the number of lines to type?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Its closer with that code in

    Now I get too many lines,

    If I input 5, I get 11 lines out

    If I input 7, I get 15 lines out,

    Do I have a math equation problem?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bluesilver2009 View Post
    Now I get too many lines,

    If I input 5, I get 11 lines out

    If I input 7, I get 15 lines out,

    Do I have a math equation problem?
    Sounds like you have twice as many lines as you need. (Actually, 2x + 1) If you have a total of X lines, you want to decrease for 1/2 X, and increase for 1/2 X.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Thanks to all that helped me out on this, I am definetly not cut out for programming.


    It finally works, thanks again!!

Popular pages Recent additions subscribe to a feed