Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > For loops help

Reply
Thread Tools

For loops help

 
 
paolo.arimado@gmail.com
Guest
Posts: n/a
 
      05-11-2006
Dear everyone,

Can someone please help me on my HW. I'm trying to write a program
that will display the following:

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

I'm having a hard time writing a code for this one. I tried several
times, but I can't get it to work properly. I live in Japan and I take
an online C++ course. The instructor lives in the US so whenever I am
awake, he's asleep. Therefore, I cannot contact him directly when I
need help. I have to get this program up and running ASAP. I tried
searching everywhere for help, but there's none except for this group.
My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
HELP I WILL GET!

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-11-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Dear everyone,
>
> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:
>
> *
> ***
> *****
> *******
> *********
> *********
> *******
> *****
> ***
> *
>
> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly. I live in Japan and I take
> an online C++ course. The instructor lives in the US so whenever I am
> awake, he's asleep. Therefore, I cannot contact him directly when I
> need help. I have to get this program up and running ASAP. I tried
> searching everywhere for help, but there's none except for this group.
> My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
> HELP I WILL GET!


Post some of the attempts you have made. Nobody will just give you the
solution. Also, read the link below, especially where it talks about
(or links to) how a homework question should be asked:

http://www.clc-wiki.net/wiki/Introdu...to_comp.lang.c

I can offer one solution, though I doubt that's what you're looking
for:

#include <stdio.h>

int main(void)
{
int i;

for (i=0; i<1; i++)
{
printf(" *\n");
printf(" ***\n");
printf(" *****\n");
printf(" *******\n");
printf("*********\n");
printf("*********\n");
printf(" *******\n");
printf(" *****\n");
printf(" ***\n");
printf(" *\n");
}

return 0;
}

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-11-2006
(E-Mail Removed) wrote:

> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:
>
> *
> ***
> *****
> *******
> *********
> *********
> *******
> *****
> ***
> *


#include <stdio.h>

int main(void)
{
puts(" *");
puts(" ***");
puts(" *****");
puts(" *******");
puts("*********");
puts("*********");
puts(" *******");
puts(" *****");
puts(" ***");
puts(" *");
puts("");
puts("Copyright 2006 Richard L. Bos - all rights reserved.");

return 0;
}

> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly. I live in Japan and I take
> an online C++ course.


Then a. you want to replace <stdio.h> with something like <cstdio.h++>;
b. you want something like stdout << instead of puts(; and c. you need
to ask in comp.lang.c++, not comp.lang.c.

> The instructor lives in the US so whenever I am awake, he's asleep.
> Therefore, I cannot contact him directly when I need help.


They don't have e-mail in the US? Gosh. I knew that country was a bit
primitive in some aspects, but that's truly backwards.

> I have to get this program up and running ASAP.


I really, really doubt that.

FWIW: DYODH. HTH; HAND.

Richard
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-11-2006
(E-Mail Removed) wrote:

> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:


(snip asterisks)

> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly.


Post your best attempt. We'll fix your code, but we won't write it
for you.

> I live in Japan and I take
> an online C++ course.


And post to the correct newsgroup. comp.lang.c++ is that way ---->

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-11-2006
(E-Mail Removed) wrote:

> *
> ***
> *****
> *******
> *********
> *********
> *******
> *****
> ***
> *


/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

#define FIVE 5

void function(char *array, int max, int limit);
char *str_rev(char *s);

int main(void)
{
char array[FIVE] = "";
int counter;

counter = FIVE;
while (counter-- != 0) {
function(array, FIVE - 1, counter);
}
for (counter = 0; counter != FIVE; ++counter) {
function(array, FIVE - 1, counter);
}
return 0;
}

void function(char *array, int max, int limit)
{
int count;

for (count = 0; count != limit; ++count) {
array[count] = ' ';
}
while (count != max) {
array[count] = '*';
++count;
}
printf(array);
putchar('*');
str_rev(array);
puts(array);
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}

/* END new.c */

--
pete
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      05-11-2006
<(E-Mail Removed)> wrote:

> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:
>
> *
> ***
> *****
> *******
> *********
> *********
> *******
> *****
> ***
> *
>
> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly. I live in Japan and I take
> an online C++ course. The instructor lives in the US so whenever I am
> awake, he's asleep. Therefore, I cannot contact him directly when I
> need help. I have to get this program up and running ASAP. I tried
> searching everywhere for help, but there's none except for this group.
> My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
> HELP I WILL GET!


Resolve to do it in two parts, the asterisks are growing or the asterisks
are shrinking.

For the growing asterisks:
Write two functions, one to print n spaces and another to print n asterisks.
Call these functions with the appropriate parameters, and have the main loop
provide the end of line. That is, one iteration of the for loop prints one
*line*.

Make a table like this:

line nbr nbr asterisks
1 1
2 3
3 5
4 6

Note the sequence and provide appropriate code in the for loop to provide
the appropriate value to the asterisk printing function.

Note well how the liberal use of functions empties the mind of extraneous
non-interesting (at the moment) things.

Now do the shrinking asterisks bit.

Done.
-----
I *finally* figured out what HW is. Homework!


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-11-2006
(E-Mail Removed) said:

> Dear everyone,
>
> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:
>
> *
> ***
> *****
> *******
> *********
> *********
> *******
> *****
> ***
> *
>
> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly. I live in Japan and I take
> an online C++ course. The instructor lives in the US so whenever I am
> awake, he's asleep. Therefore, I cannot contact him directly when I
> need help. I have to get this program up and running ASAP. I tried
> searching everywhere for help, but there's none except for this group.
> My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
> HELP I WILL GET!


My pleasure.

#define M 002354l
#define A 000644l
#define G 000132l
#define I 000322l
#define C 000374l

#define a ;
#define b for
#define c ++
#define d %
#define e int
#define f ,
#define g -
#define h 011
#define i =
#define j {
#define k )
#define l '\n'
#define m main
#define n <
#define o }
#define p >
#define q &&
#define r (
#define s ||
#define t ?
#define u putchar
#define v void
#define w '*'
#define x :
#define y ' '
#define _ /
#define C_O_O_L return

e u r e k a

e
m r
v k j
j j j j
j j j j j
j j j j j j
j j j j j j j
j e z a b r z i
M _ A _ G _ I _ C
a z n G a u r z d h
+ z _ h p M _ A q z d
h + z _ h n M _ G q z _
h n z d h + M _ I q z _ h
p z d h g M _ C t w x y k f
z d h g h + 1 s u r l k f z c
k a u r l k a j j j j j j j j j
j j C_O_O_L M _ A _ G _ I _ C a o
o o o o o o o o o o o o o o o o o o
o o o o
o o o o
o o o o
o o o o


--
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)
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-11-2006

Richard Heathfield wrote:
> (E-Mail Removed) said:
>
> > Dear everyone,
> >
> > Can someone please help me on my HW. I'm trying to write a program
> > that will display the following:
> >
> > *
> > ***
> > *****
> > *******
> > *********
> > *********
> > *******
> > *****
> > ***
> > *
> >
> > I'm having a hard time writing a code for this one. I tried several
> > times, but I can't get it to work properly. I live in Japan and I take
> > an online C++ course. The instructor lives in the US so whenever I am
> > awake, he's asleep. Therefore, I cannot contact him directly when I
> > need help. I have to get this program up and running ASAP. I tried
> > searching everywhere for help, but there's none except for this group.
> > My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
> > HELP I WILL GET!

>
> My pleasure.
>
> #define M 002354l
> #define A 000644l
> #define G 000132l
> #define I 000322l
> #define C 000374l
>
> #define a ;
> #define b for
> #define c ++
> #define d %
> #define e int
> #define f ,
> #define g -
> #define h 011
> #define i =
> #define j {
> #define k )
> #define l '\n'
> #define m main
> #define n <
> #define o }
> #define p >
> #define q &&
> #define r (
> #define s ||
> #define t ?
> #define u putchar
> #define v void
> #define w '*'
> #define x :
> #define y ' '
> #define _ /
> #define C_O_O_L return
>
> e u r e k a
>
> e
> m r
> v k j
> j j j j
> j j j j j
> j j j j j j
> j j j j j j j
> j e z a b r z i
> M _ A _ G _ I _ C
> a z n G a u r z d h
> + z _ h p M _ A q z d
> h + z _ h n M _ G q z _
> h n z d h + M _ I q z _ h
> p z d h g M _ C t w x y k f
> z d h g h + 1 s u r l k f z c
> k a u r l k a j j j j j j j j j
> j j C_O_O_L M _ A _ G _ I _ C a o
> o o o o o o o o o o o o o o o o o o
> o o o o
> o o o o
> o o o o
> o o o o


Thank you! You've definitely made my day! LoL

 
Reply With Quote
 
Robert Latest
Guest
Posts: n/a
 
      05-11-2006
On Thu, 11 May 2006 14:00:50 +0000,
Richard Heathfield <(E-Mail Removed)> wrote
in Msg. <(E-Mail Removed)>
>
> #define M 002354l
> #define A 000644l
> #define G 000132l
> #define I 000322l
> #define C 000374l


[...]

> h n z d h + M _ I q z _ h
> p z d h g M _ C t w x y k f
> z d h g h + 1 s u r l k f z c
> k a u r l k a j j j j j j j j j
> j j C_O_O_L M _ A _ G _ I _ C a o
> o o o o o o o o o o o o o o o o o o
> o o o o
> o o o o
> o o o o
> o o o o


OK, let's un-obfuscate this a little bit:

------
int putchar(int);

int main(void)
{
int z;

for (z =
002354l / 000644l / 000132l / 000322l / 000374l; z < 000132l;
putchar(z % 011 + z / 011 > 002354l / 000644l
&& z % 011 + z / 011 < 002354l / 000132l
&& z / 011 < z % 011 + 002354l / 000322l
&& z / 011 > z % 011 - 002354l / 000374l ? '*' : ' '),
z % 011 - 011 + 1 || putchar('\n'), z++);
putchar('\n');
return 002354l / 000644l / 000132l / 000322l / 000374l;
}
------

Ho do you come up with stuff like this? I don't get it. My mind doesn't
work that way.

Puzzled,
robert

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-11-2006
Robert Latest said:

> for (z =
> 002354l / 000644l / 000132l / 000322l / 000374l; z < 000132l;
> putchar(z % 011 + z / 011 > 002354l / 000644l
> && z % 011 + z / 011 < 002354l / 000132l
> && z / 011 < z % 011 + 002354l / 000322l
> && z / 011 > z % 011 - 002354l / 000374l ? '*' : ' '),
> z % 011 - 011 + 1 || putchar('\n'), z++);
> putchar('\n');
> return 002354l / 000644l / 000132l / 000322l / 000374l;
> }
> ------
>
> Ho do you come up with stuff like this? I don't get it. My mind doesn't
> work that way.


Well, I see no point using two loops when one will do, that's all.

I also don't think a program is sufficiently obfuscated if its workings are
immediately clear to the reader after running the source through cpp and
indent!

--
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with loops lsu420luv@yahoo.com C++ 4 03-11-2006 07:02 AM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
RE: Help with generators outside of loops. Robert Brewer Python 9 12-08-2004 07:14 PM
Newbee needs Help ref Using Function Statements not Loops(For) jstreet10 Python 1 12-03-2003 06:27 PM
help with infinite loops and scanf Rob C Programming 8 07-29-2003 12:28 AM



Advertisments