CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Sep 2010
    Posts
    22

    Printing patterns

    Hello again, I got some really good help here once before and was hoping for the same this time. I'm not really just looking for the answer but just a little nudge in the right direction, I really do wanna learn this stuff.

    I have been task to print the following pattern:

    * *
    ** **
    *** ***
    **** ****
    ***** *****
    ************
    ***** *****
    **** ****
    *** ***
    ** **
    * *

    I have to write the program with the function main as such;

    int main (void) {
    print_1_9();
    print_2_7();
    print_3_5();
    print_4_3();
    print_5_1();
    print_6_0();
    print_5_1();
    print_4_3();
    print_3_5();
    print_2_7();
    print_1_9();
    return 0;

    I have to use function main as shown unmodified. So does this mean that I'm to have another function that calls main? If so what would the other function be called?

    I'm so lost on this one, we haven't touched on this much in class. I know its going to involve a loop, but that's about it. Any help would be much appreciated.

  2. #2
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    The pattern didn't come out right in my post it suppose to be 2 triangles pointing at one another.

  3. #3
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    main is the entry point to your program. Other functions don't call main.

    I'm confused by the assignment. Your main calls a bunch of functions and returns. So why not implement the functions to print out the needed characters?

  4. #4
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    I'm not real sure what he's asking for either. Here are the instructions that we were given:

    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:
    * *
    ** **
    *** ***
    **** ****
    ***** *****
    ************
    ***** *****
    **** ****
    *** ***
    ** **
    * *
    2. Your program will define function main as shown below:
    int main (void) {
    print_1_9();
    print_2_7();
    print_3_5();
    print_4_3();
    print_5_1();
    print_6_0();
    print_5_1();
    print_4_3();
    print_3_5();
    print_2_7();
    print_1_9();
    return 0;
    }
    3. You must provide appropriate definitions for the 6 functions called by main. Each function call
    will print a single line of the figure. The functions have been named accoring to the number of
    spaces and asterisks that it should print. For example, print_1_9() should print 1 asterisk,
    followed by 9 spaces followed by 1 asterisk followed by a newline. This can be done with a
    single call to printf.

    4. Use function main exactly as shown. Do not modify it.

    Suggested Strategy
    Create the four functions named as required above. Each function can consist of a single printf
    statement that consists of some spaces, asterisks, and a newline.

  5. #5
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    Do you know how to create a function?

    Code:
    void myFunction(); //function prototype (declaration)
    
    int main(){
    myFunction();
    return 0;
    }
    
    void myFunction(){
    	printf("%c         %c", '*', '*');
    //appropriate printing statement here - use printf to print 
    }
    Is this a C class?

  6. #6
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    None of that looks familiar so I guess not, and yeah its a C++ class.

  7. #7
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    Okay. Well, here's just a simple and quick explanation. main is the entry point for your program. This means that when you run your program it will begin executing the statements in main. You don't call main yourself, it is called automatically when you start your program.

    From main, you can call other functions, but you have to define them of course.

    If this is a C++ class, I have to wonder why he is asking you to use printf and not cout. In C++, when you want to write to the standard output, you do so as follows:

    Code:
    cout << "Hello!" << endl;
    This will write Hello! to the standard output followed by a newline.

    When you create a function, you need to specify a return type. The return type for myFunction is void. This means it doesn't return a value. It is called to do a job and no value is returned.

    The function prototype is needed above because main calls myFunction. myFunction has to be at least declared before anything tries to call it. Alternatively, you implement myFunction first and then you wouldn't need the function prototype:

    Code:
    void myFunction(){
    	cout << "*         *" << endl;
    }
    
    int main(){
    myFunction();
    return 0;
    }
    Last edited by ttrz; October 17th, 2010 at 11:25 PM.

  8. #8
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    Since you are new I'd say don't worry about return types or the function prototype for now. Just look at the last piece of code I posted and see if you understand what is happening.

  9. #9
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    Thanks for the info, I don't recall ever being told why we put void in there with the function main and still have no idea what the headers are meant for I just know that we always use #include <stdio.h>. So does the assignment make any since to you, using printf? It just seems like it would be impossible without modifying main at least a little.

  10. #10
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    Yeah, I think I understand your code. Main is calling myfunction and telling it to print the asterisk and spaces, I'm assuming endl; means end line which we use \n. Is this right?

  11. #11
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    For now think of them as the same thing. Learn the basics first and then research more. (They are not exactly the same but you are still too new to worry about that right now.)

    Any C++ book will teach you to use cout. The printf function is how C programmers write to the standard output. It will still work, so don't worry about it.

    Yes, you are correct. main is calling myFunction which is writing the characters to the standard output.

    This is exactly what you are being assigned to do - write a bunch of different myFunctions. You also don't need that void in int main(void). But since the assignment says you can't change main, better leave it there.

    Code:
    int main (void) {
    myFunction1();
    myFunction2();
    myFunction3();
    //etc, etc
    return 0;
    }
    You can still use printf to print out the characters you need like I did above:
    Code:
    printf("&#37;c         %c\n", '*', '*');
    I've never used printf, but I was taught C++ from the start.
    Last edited by ttrz; October 18th, 2010 at 12:05 AM.

  12. #12
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    Thanks for all your help, I know it must get old having those of us that are new to programming coming in and asking questions. I really appreciate you giving your time to help point me into the right direction.

  13. #13
    Join Date
    Sep 2010
    Posts
    66

    Re: Printing patterns

    No problem. We all have to start somewhere.

    If you need a good C++ book that explains things, I'd recommend picking up Accelerated C++.

    Good luck.

  14. #14
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Printing patterns

    Quote Originally Posted by adam86107 View Post
    and yeah its a C++ class.
    Are you sure about that? Everything in the assignment hints to this being a C course. In C++, you wouldn't use printf, but std::cout.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  15. #15
    Join Date
    Sep 2010
    Posts
    22

    Re: Printing patterns

    Quote Originally Posted by D_Drmmr View Post
    Are you sure about that? Everything in the assignment hints to this being a C course. In C++, you wouldn't use printf, but std::cout.
    The instructor referred to it as a C++ class anyway.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured