Need Help with a program in C

Need Help with a program in C

Post by AscaL on Tue Oct 26, 2010 6:33 pm
([msg=48163]see Need Help with a program in C[/msg])

hi all, sorry to bother you but i just started Computer Science and i gotta do an exercise

i gotta make a program that reads a char and a number N, and then print a triangle of N lines with the char, like this
if N=5 and char="a"

a
aa
aaa
aaaa
aaaaa

unfortunatly i can't rly do it, i tried all day but can't come to a solution... all i know is that it gotta be a loop with FOR and another for in it like
for(..) {
for(...)

this is what i came up with, plz help me fix it and point me in the right direction
thx

#include <stdio.h>
int main() {
int lett,n,i;

printf("Insert a letter -> ");
scanf("%c",&lett);
printf("Insert a number -> ");
scanf("%d",&n);
printf("A Triangle will be designed with the '%c' letter composing it, and of %d lines \n",lett,n);

for(i=1;i<=n;i++) {
for ()
printf("%c",lett);
printf("\n"); }




system("PAUSE");
return 0;
}
AscaL
New User
New User
 
Posts: 5
Joined: Fri Jul 23, 2010 12:18 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by msbachman on Tue Oct 26, 2010 6:39 pm
([msg=48164]see Re: Need Help with a program in C[/msg])

You're on the right track.

Try this as a small improvement.
Code: Select all
#include <stdio.h>
int main() {
int lett,n,i;

printf("Insert a letter -> ");
scanf("%c",&lett);
printf("Insert a number -> ");
scanf("%d",&n);
printf("A Triangle will be designed with the '%c' letter composing it, and of %d lines \n",lett,n);
int g=0;
for(i=1;i<=n;i++) {
//for ()
g=i;
while(g)
{
printf("%c",lett);
--g;
}

printf("\n"); }


//system("PAUSE");
return 0;
}
"I'm going to get into your sister. I'm going to get my hands on your daughter."
~Gatito
User avatar
msbachman
Contributor
Contributor
 
Posts: 681
Joined: Mon Jan 12, 2009 10:22 pm
Location: In the sky lol
Blog: View Blog (0)


Re: Need Help with a program in C

Post by AscaL on Tue Oct 26, 2010 6:52 pm
([msg=48165]see Re: Need Help with a program in C[/msg])

thx a lot for the help man... only thing is that i still didn't do the "while", i can't really use it ^^
as far as i think it should just be something with the two for thing

beside i'm not sure even what a for in a for does... i just started and i'm still learning
ps: i didn't even do the "--g"... just did the x++ in the for loop
AscaL
New User
New User
 
Posts: 5
Joined: Fri Jul 23, 2010 12:18 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by Avery17 on Tue Oct 26, 2010 7:26 pm
([msg=48168]see Re: Need Help with a program in C[/msg])

AscaL wrote:thx a lot for the help man... only thing is that i still didn't do the "while", i can't really use it ^^
as far as i think it should just be something with the two for thing

beside i'm not sure even what a for in a for does... i just started and i'm still learning
ps: i didn't even do the "--g"... just did the x++ in the for loop



Read up on recursion... what am I saying.

http://www.cprogramming.com/tutorial/c/lesson1.html

Go there and follow all the tutorials one at a time and understand each one. Recursion is the one you want to understand the most for this. But you must read about loops and basic syntax before you begin to think about recursion. Good luck!
Avery17
Experienced User
Experienced User
 
Posts: 81
Joined: Fri Sep 17, 2010 11:28 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by tgoe on Tue Oct 26, 2010 10:33 pm
([msg=48183]see Re: Need Help with a program in C[/msg])

@msbachman

Unformatted code *within* code tags? How is this possible? xD
User avatar
tgoe
Contributor
Contributor
 
Posts: 650
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Need Help with a program in C

Post by AscaL on Wed Oct 27, 2010 2:49 am
([msg=48190]see Re: Need Help with a program in C[/msg])

i'm studying at the university, not at home... i can't just go and skip things or use knowledge i'm not supposed to have to make an exercise... there would be no point....
for now what i did is:
-if/if else
-for loop
-the &&/||/! operator
-printf() and scanf()
-all kind of variables (likechar/int/float...)

so that's all i gotta use for the program to work

anyway thx for the help :)
AscaL
New User
New User
 
Posts: 5
Joined: Fri Jul 23, 2010 12:18 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by msbachman on Wed Oct 27, 2010 7:04 am
([msg=48193]see Re: Need Help with a program in C[/msg])

tgoe wrote:@msbachman

Unformatted code *within* code tags? How is this possible? xD



Oh shit another motherfucker's going to rag on my about my lack of indentation now, huh? :P

-- Wed Oct 27, 2010 7:20 am --

AscaL wrote:i'm studying at the university, not at home... i can't just go and skip things or use knowledge i'm not supposed to have to make an exercise... there would be no point....


About your comment about the for within a for loop, all that does is for each iteneration of the outside loop, it will run the inside loop. Then the counter or whatever will increment/decrement, and it will run it again.

Here's an example of recursion:

Code: Select all

int nonsense_function(int g)
{
g+=g;
nonsense_function(g);
if(g>=100000)
return g;
}


that function doesn't do much of anything of importance, the real thing to recognize is that you can call a function within a function. Outside of class, think for a bit about how this concept might be able to help you out in your programs.
"I'm going to get into your sister. I'm going to get my hands on your daughter."
~Gatito
User avatar
msbachman
Contributor
Contributor
 
Posts: 681
Joined: Mon Jan 12, 2009 10:22 pm
Location: In the sky lol
Blog: View Blog (0)


Re: Need Help with a program in C

Post by thetan on Thu Oct 28, 2010 11:31 am
([msg=48260]see Re: Need Help with a program in C[/msg])

msbachman wrote:
Code: Select all
int nonsense_function(int g)
{
g+=g;
nonsense_function(g);
if(g>=100000)
return g;
}


That function doesn't do anything at all, instead it will perform head recursion endlessly until stack space is entirely consumed and then it will error out. (yes, this program does nothing more then eat ~8mb of ram and then crashes via segmentation fault)

So to conclude on your last post, i'd say that such a method is useless.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Need Help with a program in C

Post by Avery17 on Thu Oct 28, 2010 12:18 pm
([msg=48261]see Re: Need Help with a program in C[/msg])

thetan wrote:
msbachman wrote:
Code: Select all
int nonsense_function(int g)
{
g+=g;
nonsense_function(g);
if(g>=100000)
return g;
}


That function doesn't do anything at all, instead it will perform head recursion endlessly until stack space is entirely consumed and then it will error out. (yes, this program does nothing more then eat ~8mb of ram and then crashes via segmentation fault)

So to conclude on your last post, i'd say that such a method is useless.


I think he just posted it as an example, he even stated that it doesn't do anything. Just to show him the concept of how it works. With some added checks it could prove quite useful for this problem.
Avery17
Experienced User
Experienced User
 
Posts: 81
Joined: Fri Sep 17, 2010 11:28 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by thetan on Thu Oct 28, 2010 1:21 pm
([msg=48262]see Re: Need Help with a program in C[/msg])

Avery17 wrote:I think he just posted it as an example, he even stated that it doesn't do anything. Just to show him the concept of how it works.

It's one thing to do nothing and another thing entirely to error out and crash.
Avery17 wrote:With some added checks it could prove quite useful for this problem.

With some added checks it would just be a lame abstraction to a even simpler problem.

When in doubt apply K.I.S.S. - Keep It Simple Stupid

This is not the topic to begin conversation on tail vs head recursion, especially when the passage into the topic is as fail as it is.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests