By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,591 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,591 IT Pros & Developers. It's quick & easy.

A simple program , need to see where's the mistake :)

P: n/a
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as lines
as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}
main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug:
I always get a number after stars such as this:
Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)

Oct 25 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Odinn wrote:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as lines
as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}
main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug:
I always get a number after stars such as this:
Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)
Not sure what you were trying to accomplish by calling the star
function from within the printf. Maybe I am missing something. If you
just take that out and add the printing of the new line in your star
function it seems to output what you described as wanting:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
printf("\n");
}

printstar(int x){
int start = 1;
int end = x;
while (start <= end)
{
star(start) ;
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
printstar(veri);
}

Oct 25 '06 #2

P: n/a
Odinn <da*******@gmail.comwrote:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
Well, you've tried at least, and that's a great start. Not bad at
all, but there are some things to note:
#include <stdio.h>
star(int x)
This is valid - the return type defaults to "int" if you don't supply
one explicitly - but it isn't a great idea, and it also isn't
supported by the latest C language standard. Given that you don't
return anything, perhaps "void" would be appropriate here.
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}
print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
Here's where those numbers you're seeing come in. That %d in printf()
tells it to look for a number, which is what star(start) returns.
Since you didn't actually return anything, though, the number that
gets printed can't be predicted. Watch out for that.

star(start);
printf( "\n" );

will work quite nicely without printing that number.
start++;
}
}
main()
This would be better as

int main(void)

....
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
....which means you'll want to return something here. 0 means success,
so

return 0;
}
Probably better than my first C program...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 25 '06 #3

P: n/a
Thanks alot for the help Mr Christopher , aswell as the help. Really
appreciated.
Uluc Aydin
Christopher Benson-Manica yazdi:
Odinn <da*******@gmail.comwrote:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.

Well, you've tried at least, and that's a great start. Not bad at
all, but there are some things to note:
#include <stdio.h>
star(int x)

This is valid - the return type defaults to "int" if you don't supply
one explicitly - but it isn't a great idea, and it also isn't
supported by the latest C language standard. Given that you don't
return anything, perhaps "void" would be appropriate here.
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}
print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );

Here's where those numbers you're seeing come in. That %d in printf()
tells it to look for a number, which is what star(start) returns.
Since you didn't actually return anything, though, the number that
gets printed can't be predicted. Watch out for that.

star(start);
printf( "\n" );

will work quite nicely without printing that number.
start++;
}
}

main()

This would be better as

int main(void)

...
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);

...which means you'll want to return something here. 0 means success,
so

return 0;
}

Probably better than my first C program...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 25 '06 #4

P: n/a
On Wed, 2006-10-25 at 12:34 -0700, Odinn wrote:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
No problem. Remember to format your code correctly when posting it
online. (Use a 2 or 4-space tab, and no // comments.) I've corrected
your spacing below as an example for you.

<snip program description>
This is the program I wrote:

#include <stdio.h>

star(int x)
Don't rely on implicit int. It is invalid in C99, and therefore limits
your future-proofness.
{
int i=1;
while(i <= x)
Start at 0 and use < instead of <=. It seems counter-intuitive at first,
but it's how computers tend to work.
{
printf("*");
i++;
}
Hey hey hey! This function returns int. You need to return something
from it, or change the return type to void.
}

print(int x){
Be consistent in your bracing style. While you're learning, it's okay to
experiment, but keep to one style per source file. (And always follow
in-house coding standards.)
int start = 1;
int end = x;
while (start <= end)
int start = 0;
while (start < x)
{
printf("%d \n", star(start) );
Here's your problem. %d prints an integer. To do what you want, use:
star(start);
putchar("\n");
start++;
}
}
main()
int main(void)

See how much more explicit that is?
{
printf("Enter value \n");
int veri;
Mixed declaration and code are prohibited in C90, and so for decent
portability you should not do this. (What I mean is, you can't have
"int n" after a printf() statement.)
scanf("%d", &veri);
print(veri);
What is this print()? Where is it defined?
Also, you need to return something from main():
return 0;
}
Hope that helps. :-)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #5

P: n/a
On Wed, 2006-10-25 at 12:34 -0700, Odinn wrote:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
<snip code, which I've already commented on>
Here's my version of your program:

#include <stdio.h>

void print_stars(int num) {
int i;
for(i = 0; i < num; i++)
putchar('*');
putchar('\n');
}

int main(void) {
int num, ctr;
scanf(&num);
for(ctr = 0; ctr < num; ctr++)
print_stars(ctr);
return 0;
}

Consider the following:
The function name clearly shows what it does, and the argument is
named clearly. By simply reading the prototype, one can figure out
exactly what the function does and how to use it.
I've used putchar() instead of printf() for a number of reasons. The
first is that putchar() is tailor-made for printing one character, and
therefore can be much more efficient than printf(). Another reason is
that printf() needs to scan each character and check for format strings.
putchar() eliminates this overhead. Thirdly, putchar() shows my intent
more clearly.
I moved the printing of the newline into the print_stars() function,
which is where it logically belongs.
Notice also that I formatted consistently and didn't rely on implicit
return values. On those functions that /did/ return something, I made
sure to include a return statement.

My final comment is that you shouldn't be using scanf() for user input,
as it's very unfriendly and doesn't handle erroneous input properly.
Better to write your own get_int() function using fgets() or fgetc(). I
recommend that as your next project.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #6

P: n/a
Odinn wrote:
>
I am trying to make a program that gets a number and prints out
starts as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as
lines as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug: I always get a number after stars
such as this: Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)
Not bad for a beginning. You show promise. With gcc set to accept
C89 standard:

[1] c:\c\junk>cc junk.c
junk.c:4: warning: return type defaults to `int'
junk.c: In function `star':
junk.c:11: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:13: warning: return type defaults to `int'
junk.c: In function `print':
junk.c:21: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:25: warning: return type defaults to `int'
junk.c: In function `main':
junk.c:27: warning: ISO C89 forbids mixed declarations and code
junk.c:30: warning: control reaches end of non-void function

After reformatting (don't use tabs, use spaces) and correcting the
errors (altered or moved lines marked with /**/) it works.

#include <stdio.h>

/**/void star(int x)
{
int i = 1;

while (i <= x) {
printf("*");
i++;
}
}

/**/void print(int x)
{
int start = 1;
int end = x;

while (start <= end) {
/**/ star(start);
/**/ printf("\n");
start++;
}
}

/**/int main(void)
{
int veri;

/**/printf("Enter value \n");
scanf("%d", &veri);
print(veri);
/**/return 0;
}

Study the error messages and the changes made, and think about why
I made them.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 25 '06 #7

P: n/a
This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}

Oct 26 '06 #8

P: n/a
On 25 Oct 2006 20:16:18 -0700, "fermineutron" <fr**********@yahoo.com>
wrote:
>This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}
What have you got against for loops?
Remove del for email
Oct 26 '06 #9

P: n/a
fermineutron said:
This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}
Some sample runs:

me@here./foo
enter a number1

*me@here./foo
enter a numberSIX
me@here./foo
enter a numberme@here>

For the love of Mike, if you must use the thrice-accursed scanf function,
look up the meaning of its return value, and act upon that knowledge. What
if you get input that isn't a number? What if the user signals EOF?

And think more closely about your end conditions.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 26 '06 #10

P: n/a
Andrew Poelstra <ap*******@false.sitewrote:
int num, ctr;
scanf(&num);
What is this, my lord? Surely hell hath no fury like a terribly
incorrect scanf() call?
My final comment is that you shouldn't be using scanf() for user input,
as it's very unfriendly and doesn't handle erroneous input properly.
Better to write your own get_int() function using fgets() or fgetc(). I
recommend that as your next project.
And you helpfully provided an example of how *not* to use scanf() :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 26 '06 #11

P: n/a
On Thu, 2006-10-26 at 12:51 +0000, Christopher Benson-Manica wrote:
Andrew Poelstra <ap*******@false.sitewrote:
int num, ctr;
scanf(&num);

What is this, my lord? Surely hell hath no fury like a terribly
incorrect scanf() call?
Oops! I meant
scanf("%d", &num);
My final comment is that you shouldn't be using scanf() for user input,
as it's very unfriendly and doesn't handle erroneous input properly.
Better to write your own get_int() function using fgets() or fgetc(). I
recommend that as your next project.

And you helpfully provided an example of how *not* to use scanf() :-)
:-}

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 26 '06 #12

P: n/a

"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
Odinn wrote:
>>
I am trying to make a program that gets a number and prints out
starts as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as
lines as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug: I always get a number after stars
such as this: Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)

Not bad for a beginning. You show promise. With gcc set to accept
C89 standard:

[1] c:\c\junk>cc junk.c
junk.c:4: warning: return type defaults to `int'
junk.c: In function `star':
junk.c:11: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:13: warning: return type defaults to `int'
junk.c: In function `print':
junk.c:21: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:25: warning: return type defaults to `int'
junk.c: In function `main':
junk.c:27: warning: ISO C89 forbids mixed declarations and code
junk.c:30: warning: control reaches end of non-void function

After reformatting (don't use tabs, use spaces) and correcting the
errors (altered or moved lines marked with /**/) it works.

#include <stdio.h>

/**/void star(int x)
{
int i = 1;

while (i <= x) {
printf("*");
i++;
}
}

/**/void print(int x)
{
int start = 1;
int end = x;

while (start <= end) {
/**/ star(start);
/**/ printf("\n");
start++;
}
}

/**/int main(void)
{
int veri;

/**/printf("Enter value \n");
scanf("%d", &veri);
print(veri);
/**/return 0;
}

Study the error messages and the changes made, and think about why
I made them.
Another version (why use printf for a single character?):

#include <stdio.h>
void printstars( int n ) {
while ( n-- ) {
putc( (int)'*', stdout );
}
putc( (int)'\n', stdout );
}

int main( int argc, char *argv[] )
{
if ( argc 1 ) {
n = atoi(argv[1]);
/* or better, use strtod and check for errors */

for ( i=1; i <= n; i++ ) {
printstars(i);
}
return 0;
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 26 '06 #13

P: n/a
Fred Kleinschmidt wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
>Odinn wrote:
>>>
I am trying to make a program that gets a number and prints out
starts as this:
.... snip ...
>>
Study the error messages and the changes made, and think about why
I made them.

Another version (why use printf for a single character?):
....

Because I wanted to minimize the changes so the OP would recognize
the code.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 26 '06 #14

P: n/a
"Fred Kleinschmidt" <fr******************@boeing.comwrites:
[...]
#include <stdio.h>
void printstars( int n ) {
while ( n-- ) {
putc( (int)'*', stdout );
}
putc( (int)'\n', stdout );
}
[...]

What is the purpose of the casts? (Hint: none.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 26 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.