|
-
October 17th, 2010, 09:28 PM
#1
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.
-
October 17th, 2010, 09:29 PM
#2
Re: Printing patterns
The pattern didn't come out right in my post it suppose to be 2 triangles pointing at one another.
-
October 17th, 2010, 10:05 PM
#3
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?
-
October 17th, 2010, 10:14 PM
#4
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.
-
October 17th, 2010, 10:42 PM
#5
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?
-
October 17th, 2010, 10:49 PM
#6
Re: Printing patterns
None of that looks familiar so I guess not, and yeah its a C++ class.
-
October 17th, 2010, 11:19 PM
#7
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.
-
October 17th, 2010, 11:27 PM
#8
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.
-
October 17th, 2010, 11:28 PM
#9
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.
-
October 17th, 2010, 11:34 PM
#10
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?
-
October 17th, 2010, 11:57 PM
#11
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("%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.
-
October 18th, 2010, 12:00 AM
#12
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.
-
October 18th, 2010, 12:06 AM
#13
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.
-
October 18th, 2010, 04:33 AM
#14
Re: Printing patterns
 Originally Posted by adam86107
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
-
October 18th, 2010, 11:31 AM
#15
Re: Printing patterns
 Originally Posted by D_Drmmr
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|