how should i do if i meet this problem?

J

JetQi Tan

Task:

Write a program in C to produce the following shapes.
The size of the pattern is determined by an integer value which is
supplied by user at runtime.
The number must be in the range 1 to 10. When a valid number is
entered, the pattern is generated.
See below for examples.
Example 1:
If user enter 5, the program should produce:

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Example 2:
If user enter 3, the program should produce:

*
* *
* * *
* *
*
 
R

red floyd

Task:

Write a program in C to produce the following shapes.
[blatant "do my homework for me" redacted]

1. This is comp.lang.c++, not comp.lang.c
2. Just post your instructor's email, so we can send him
the answer directly, and save you the trouble.

You have already been told we won't do homework for you.
 
J

JetQi Tan

no,this is the extra exercise i search online,below is my solution but
i think i am totally out of topic,i cant differentiate what is
comp.lang.c++, and comp.lang.c






#include<stdio.h>
#include<conio.h>

main()
{
int row, column;
//print the first 3 rows
for (row = 1; row <= 3; row++){
for(column = 1; column <= row; column++)
{
printf("*");
}
printf("\n");
}
//print the second last rows
for (row = 1; row <= 2; row++){
for(column = 2; column >= row; column--)
{
printf("*");
}
printf("\n");
}
getch();
}
 
L

LR

JetQi said:
no,this is the extra exercise i search online,below is my solution but
i think i am totally out of topic,i cant differentiate what is
comp.lang.c++, and comp.lang.c

It looks to me like the code you're posting is in C and not C++,
although I'm pretty sure it will compile with a C++ compiler.

Did the instructor in your course say what language he expects to use?

You wrote else thread.
If user enter 5, the program should produce:

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

The stars complicate things. Try to write a program that when given the
input 5, produces the output: 1 2 3 4 5 4 3 2 1

And for input 3 produces: 1 2 3 2 1

Then add the output for the '*'s.
#include<stdio.h>
#include<conio.h>

main()
{
int row, column;


Print the first three rows? Is this program an attempt to make it work
only if the user enters 3?
//print the first 3 rows
for (row = 1; row <= 3; row++){
Why is this '3' a constant?

Maybe change that to something like (I've written this half snippet in
C++, but didn't try compiling it.)
const int maxNumberOfStars = 5; // or whatever.
for(int row = 0; row < maxNumberOfStars; row++) {
...


at first, just write the number of '*'s in the row here.


I guess this loop is only for the case where the user entered '3' too.
//print the second last rows
for (row = 1; row <= 2; row++){

at first, just write the number of '*'s in the row here.
}
getch();
}

If you are writing in C, and not C++ you would probably be better off
posting in comp.lang.c or maybe even alt.comp.lang.learn.c-c++, although
that seems to get little traffic.
 
A

Alexander Bartolich

Paul said:
[...]
It seems nobody is prepared to post code for fear of helping you with
homework, so I too will refrain from posting code on this occassion.

On the other hand it might be lulzy to post solutions that OP can't
use because they are too complicated to be plausible.
One
idea that immediately comes to mind maybe just to create a string of chars
including newline* and do one output opertaion.

Challenge accepted.

1 #include <cstdlib>
2 #include <iostream>
3
4 static std::string r(const long i, const long n)
5 {
6 std::string p(i, '*');
7 p += '\n';
8 return i == n ? p : p + r(i + 1, n) + p;
9 }
10
11 int main(int argc, char** argv)
12 {
13 if (argc != 2) { return EXIT_FAILURE; }
14 char* endp;
15 const long n = strtol(argv[1], &endp, 0);
16 if (*endp != 0 || n < 1) { return EXIT_FAILURE; }
17 std::cout << r(1, n);
18 return EXIT_SUCCESS;
19 }

--
 
J

JetQi Tan

oh i see,thanks a lot^^
i wonder that my teacher can do this as well as you guys or not,hahaXD
anyway ,thanks for your guys opinion,it really helps...
 
M

MikeP

JetQi said:
Task:

Write a program in C to produce the following shapes.
The size of the pattern is determined by an integer value which is
supplied by user at runtime.
The number must be in the range 1 to 10. When a valid number is
entered, the pattern is generated.
See below for examples.
Example 1:
If user enter 5, the program should produce:

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Example 2:
If user enter 3, the program should produce:

*
* *
* * *
* *
*

Writing a program to do that is overkill. Just put a sheet of paper in
the Smith Corona and type it directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top