Programming Help Needed

Live forum: http://forum.freeipodguide.com/viewtopic.php?t=21710

Ryanish

11-09-2005 20:01:46

Hey,

I am trying to do the following using a for() statement.

It's in C (but if you know it in ANY language, its almost universal).

I need to outprint something liek this

lililililililili
lilililililili
lililililili
lilililili
lililili
lilili
lili
li

That would be the number "8" if the user inputted 8 it would do a for() loop and show that.

if it was a 7 they inputted, it would do this

lilililililili
lililililili
lilililili
lililili
lilili
lili
li

Ya know?

Please, I need this asap.

Thanks!

FreeOffersNow

11-09-2005 20:02:53

Is "inputted" really a word?

Ryanish

11-09-2005 20:04:31

That's beside the point ;)

hehe.

Anyone?

Admin

11-09-2005 20:06:22

Here's PHP. The logic is the same for java/c/etc
[code1b785c2bbda]
$input = readLine(); // or whatever

for(int $i = $input; $i > 0; $i--)
{
for(int $j = 0; $j < $i; $j++)
{
echo "li";
}
echo "\n";
}
[/code1b785c2bbda]

FreeOffersNow

11-09-2005 20:07:30

What is the highest # you want to allow them to input? I know in PHP you could just use if/elseif if it was a small enough span if numbers you want to work with.

bballp6699

11-09-2005 20:08:02

Yeah, it's a loop inside a loop. I was writing it out in C++, but someone walked in the room and I saw Admin already did it in another frame.

[code1d98cc98851]
cout << "How many stars?" << endl;
cin >> numstars; // or whatever

for(int i = numstars; i > 0; i--)
{
for(int j = 0; j < i; j++)
{
cout << "8";
}
cout << endl;
}
[/code1d98cc98851]

Theres c++ if it makes you feel better. I would have done it a tit bit different, but I'm lazy, so I just used his code.

Admin

11-09-2005 20:11:04

This was like day three of AP Java, ha ha.

Ryanish

11-09-2005 20:12:39

Awesome, thank you Admin.

Alright, now I got 2/4 done.

I also need to do this, which I have been stuck on for hours

Output

[code11ddb2d651d]
lililili
lilili
lili
li
[/code11ddb2d651d]

I know you would be checking for the number of spaces, but im stuck.. any clues?

Thank you Admin, by the way, I appreciate it

nusqax

11-09-2005 20:13:39

for the second question, this is in java.

[code1a2baf2d81d]
public static void recursion (int x, int y)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y - x; j++)
{ System.out.print ("li"); }
}

recursion (x-1, y);
}

public static void main (String[] args)
{
int x;

System.out.println ("Enter a number: ");
x = Stdin.readInt ();

recursion (x, x);
}[/code1a2baf2d81d]

bballp6699

11-09-2005 20:13:43

I'm glad they are asking you shit that you really need to know...

Ryanish

11-09-2005 20:15:06

Heh, yeah.

mr0x

11-09-2005 20:15:35

Assuming n is the number inputted

[code1c766c5e79a]
for (int i=0; i<n; i++)
{
for (int j=0; j<n-i; j++)
{
printf ("li");
}
printf ("\n");
}
[/code1c766c5e79a]

[quotec766c5e79a="Ryanish"]Hey,

I am trying to do the following using a for() statement.

It's in C (but if you know it in ANY language, its almost universal).

I need to outprint something liek this

lililililililili
lilililililili
lililililili
lilililili
lililili
lilili
lili
li

That would be the number "8" if the user inputted 8 it would do a for() loop and show that.

if it was a 7 they inputted, it would do this

lilililililili
lililililili
lilililili
lililili
lilili
lili
li

Ya know?

Please, I need this asap.

Thanks![/quotec766c5e79a]

FreeOffersNow

11-09-2005 20:16:10

Just make sure you actually understand the code...or you're not going to know what you're doing next week in class ;)

Ryanish

11-09-2005 20:17:36

Well, I know the code, it makes sense when I go through it in my head, but I am just stumped on loops, they confuse me a lot.

nusqax

11-09-2005 20:18:41

opps, i forgot to mention. my post in response to your second question utilizes recursion. i dont know if you guys touched that yet.

edit
[quoteba55f332fd]
I also need to do this, which I have been stuck on for hours

Output

Code

lililili
lilili
lili
li



I know you would be checking for the number of spaces, but im stuck.. any clues?
[/quoteba55f332fd]

check my post above

bballp6699

11-09-2005 20:19:38

Recursion is against my religion.

Ryanish

11-09-2005 20:20:12

[quote618b67be9b="nusqax"]opps, i forgot to mention. my post in response to your second question utilizes recursion. i dont know if you guys touched that yet.


I also need to do this, which I have been stuck on for hours

Output

Code

lililili
lilili
lili
li



I know you would be checking for the number of spaces, but im stuck.. any clues?
[/quote618b67be9b]

check my post above


We haven't got there quite yet,

Do you know how I would do that without 'recursion' or whatever?

It's all gotta be through a for() loop

mr0x

11-09-2005 20:24:03

[quoted8ca11bec6="Ryanish"]Well, I know the code, it makes sense when I go through it in my head, but I am just stumped on loops, they confuse me a lot.[/quoted8ca11bec6]

Here's a cool way to do it. Assuming n is the given number.

[code1d8ca11bec6]
charli outstring = (charli) malloc ( n+1);
int i;

outstring[n] = '\0';
for (i=0; i<n; i++)
{
memset (outstring, ' ', i);
memset (outstring, 'li', n-i);
printf ("%s\n", outstring);
}

free(outstring);
[/code1d8ca11bec6]

I don't have a compiler handyso I can't check to be sure ...

Anyway you should probably stick to a two for loops.

Ryanish

11-09-2005 20:27:12

Eh, yeah..
I haven't been able to get it to work..

Here's what ive got for outputting this
[code1a9072e8132]
userint = 7

lilililililili
lililililili
lilililili
lililili
lilili
lili
li

number = 7
li
lili
lilili
lililili
lilililili
lililililili
lilililililili
[/code1a9072e8132]


Eh.. having trouble!

i've got
[code1a9072e8132]
nextint = userint;
for (x = 0; x < userint; x++)
{
for (y = 0; y < (x - userint); y++)
{
printf("%c", &usersymbol);
}
printf("\n");
}
[/code1a9072e8132]

^^ don't work.

mr0x

11-09-2005 20:27:53

[quote953d352ee3="Ryanish"]Awesome, thank you Admin.

Alright, now I got 2/4 done.

I also need to do this, which I have been stuck on for hours

Output

[code1953d352ee3]
lililili
lilili
lili
li
[/code1953d352ee3]

I know you would be checking for the number of spaces, but im stuck.. any clues?

Thank you Admin, by the way, I appreciate it[/quote953d352ee3]

The double for loop version


[code1953d352ee3]
for (int i=0; i<n; i++)
{
for (int j=0; j<i; j++)
{
printf (" ");
}

for (int j=0; j<n-i; j++)
{
printf ("li");
}
printf ("\n");
}
[/code1953d352ee3]

Ryanish

11-09-2005 20:38:11

Thanks..

Ryanish

11-09-2005 20:43:52

Is there a more efficient way of doing this
[code11dff765284]userint = userint + 1;
for (x=0; x<userint; x++)
{
for (y=0; y<x; y++)
{
printf ("li");
}

for (y=0; y<userint-x; y++)
{
printf (" ");
}
printf ("\n");
} [/code11dff765284]

I can't seem to think how to make it outprint this

[code11dff765284]
li
lili
lilili
[/code11dff765284]

Any pointers?

BTW, once I see the stuff, I learn it really quickly, my brain just gets confused when I try to do like 2 for loops in my head and see the result. I guess it's just one of those gotta-knows ?

Vector

11-09-2005 20:44:59

you are a real novice ryanish, keep working on experimental projects and reading up on books. good luck man

ajrock2000

11-09-2005 20:46:01

lynda.com )

Ryanish

11-09-2005 20:46:15

[quote2f461912e2="Vector"]you are a real novice ryanish, keep working on experimental projects and reading up on books. good luck man[/quote2f461912e2]

Yeah, thanks.. ) I'm in an introductory class right now for C Programming / Java / C++, so I should learn a lot this semester.

I plan to go on to more advance classes in the future and learn outside of class too.

It's fun! P

nusqax

11-09-2005 20:48:34

[quote89944dda0f="Ryanish"]Is there a more efficient way of doing this
[code189944dda0f]userint = userint + 1;
for (x=0; x<userint; x++)
{
for (y=0; y<x; y++)
{
printf ("li");
}

for (y=0; y<userint-x; y++)
{
printf (" ");
}
printf ("\n");
} [/code189944dda0f]

I can't seem to think how to make it outprint this

[code189944dda0f]
li
lili
lilili
[/code189944dda0f]

Any pointers?

BTW, once I see the stuff, I learn it really quickly, my brain just gets confused when I try to do like 2 for loops in my head and see the result. I guess it's just one of those gotta-knows ?[/quote89944dda0f]

just do what you do for

lilili
lili
li

the other way around.

Vector

11-09-2005 20:49:29

Yea, you can get carried away in it and start thinking of crazy huge creations and projects. Don't let it get to your head though, that just makes things harder. Take it easy and keep working on it, soon you will be a pro, and classses help a lot!!!! So do online sites (but only if you are in the mood and have a very large interest)

Ryanish

11-09-2005 20:52:45

[quote0005082d03="nusqax"][quote0005082d03="Ryanish"]Is there a more efficient way of doing this
[code10005082d03]userint = userint + 1;
for (x=0; x<userint; x++)
{
for (y=0; y<x; y++)
{
printf ("li");
}

for (y=0; y<userint-x; y++)
{
printf (" ");
}
printf ("\n");
} [/code10005082d03]

I can't seem to think how to make it outprint this

[code10005082d03]
li
lili
lilili
[/code10005082d03]

Any pointers?

BTW, once I see the stuff, I learn it really quickly, my brain just gets confused when I try to do like 2 for loops in my head and see the result. I guess it's just one of those gotta-knows ?[/quote0005082d03]

just do what you do for

lilili
lili
li

the other way around.[/quote0005082d03]

Ive tried that, using
[code10005082d03]
for (x = 0; x <= userint; x++)
{
for(y=1;y<=x;y++)
{
printf("%c", &usersymbol);
}
printf("\n");
}
[/code10005082d03]

No worky..
That outputs
[code10005082d03]
li
lili
lilili
lililili
.etcurl==http://=http:///url
[/code10005082d03]

bballp6699

11-09-2005 21:09:32

Meh, only because I know what it's like to be stumped the day before the project is due...

[code12915ae0b01]int numstars;

cout << "How many stars?";
cin >> numstars;

for(int i = 0; i < numstars; i++)
{
for (int j=0; j < i; j++)
{
cout << " ";
}
for(int q = i; q < numstars; q++)
{
cout << "li";
}
cout << endl;
}
[/code12915ae0b01]

mr0x

11-09-2005 21:14:07

[quote62a2d9bf33="Ryanish"][quote62a2d9bf33="nusqax"][quote62a2d9bf33="Ryanish"]Is there a more efficient way of doing this
[code162a2d9bf33]userint = userint + 1;
for (x=0; x<userint; x++)
{
for (y=0; y<x; y++)
{
printf ("li");
}

for (y=0; y<userint-x; y++)
{
printf (" ");
}
printf ("\n");
} [/code162a2d9bf33]

I can't seem to think how to make it outprint this

[code162a2d9bf33]
li
lili
lilili
[/code162a2d9bf33]

Any pointers?

BTW, once I see the stuff, I learn it really quickly, my brain just gets confused when I try to do like 2 for loops in my head and see the result. I guess it's just one of those gotta-knows ?[/quote62a2d9bf33]

just do what you do for

lilili
lili
li

the other way around.[/quote62a2d9bf33]

Ive tried that, using
[code162a2d9bf33]
for (x = 0; x <= userint; x++)
{
for(y=1;y<=x;y++)
{
printf("%c", &usersymbol);
}
printf("\n");
}
[/code162a2d9bf33]

No worky..
That outputs
[code162a2d9bf33]
li
lili
lilili
lililili
.etcurl==http://=http:///url
[/code162a2d9bf33][/quote62a2d9bf33]

Sigh!

[code162a2d9bf33]
for (x = userint; x >= 0; x--)
{
for(y=1;y<=x;y++)
{
printf("%c", &usersymbol);
}
printf("\n");
}
[/code162a2d9bf33]

Ryanish

11-09-2005 21:19:41

Yup, I was fooling around with it and did it that way (before you posted) and I got it.

Thanks anyways.

Ryanish

11-09-2005 21:20:57

Finally finished this project, feels good that I did 70% of it on my own.

Hehe, I will defenitely learn not to do assignments 2 hours before they are due (it's due tonight online by 1200 AM PST.. and I forgot about it).

Thanks to all that helped.

Admin

11-09-2005 21:32:57

I gotta be honest... these are pretty basic logic exercises. they're really more tests of your logic and thoroughness and ability to think within a certain pattern than they are programming tests. if you're really struggling with these you might want to consider a different course. The level 2 programming course at my school has a 50% failure rate. You have to have a certain mind for it...

Ryanish

11-09-2005 21:52:19

Remember, I am learning, I'm not in any advanced courses yet.

If I started the assignment 2 days prior, I would have been able to do it. I get faster as I get more familiar with the stuff.. I can do while loops pretty easily now, just getting into for() loops now..

I don't think I'll have a problem, I'm pretty accustomed to most everything else.