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

looping problems

P: n/a
please tell me how to get the following output using loops...

1) *
* *
* * *
* * * *
* * *
* *
*
2) abcdefgfedcba
abcdefedcba
abcdedcba
abcdcba
abcba
aba
a
please do use "for" loop.

thankyou very much for solving my problem...

Jul 10 '07 #1
Share this Question
Share on Google+
24 Replies


P: n/a
Hello,

I think that nobody here want to do your homework.

Markus
Jul 10 '07 #2

P: n/a
well i am using turbo c++ compiler....
do remember to use for loop.......
and plese suggest me a nice e-book or refrence book for java
language.....

thanks!!

Jul 10 '07 #3

P: n/a
tanmay wrote:
well i am using turbo c++ compiler....
do remember to use for loop.......
and plese suggest me a nice e-book or refrence book for java
language.....
Please go to a Java newsgroup to ask about books on Java.
Jul 10 '07 #4

P: n/a
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,

I think that nobody here want to do your homework.

Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
so thats why i joined this group...thinking you people will help me
out in learning...
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Jul 10 '07 #5

P: n/a
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
>Hello,

I think that nobody here want to do your homework.

Markus

well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
What problems are those? What needs to be "clarified"? Can you be
specific, please?
so thats why i joined this group...thinking you people will help me
out in learning...
Doing your tasks *for* you is not helping you out in learning.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....
Yes, it's easy for most of us here. But if you want it to become easy
for *you* as well, *you* need to do it *yourself*.

Imagine somebody wanting to learn to ride a bicycle. It's not going to
help him/her to see somebody else do it. He/she needs to *get on the
damn bicycle* and ride it. If you (as you claim) are learning C++ "as
part" of your "hobby" (and what hobby is that, I wonder?), you need to
start *writing those programs* yourself. Otherwise, don't call it
"learning".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '07 #6

P: n/a
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
>Hello,

I think that nobody here want to do your homework.

Markus

well as u said ..let me clarify....
I haven't seen any /u/ in this group. Whatever he/she said, I don't know it.

Please make an effort to use correct language. /You/ has three letters, and
they're not hard to type, and one dot is more than enough to end a
sentence. There are many readers here who have problems enough with the
English language, and many others don't even bother reading 'obfuscated
English'.
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......
Whether it was you or your teacher who gave you your homework assignment
doesn't really matter. It's still homework.
well i had problem which i thought would be clarified
here.....
Sure. Try to do it yourself first.
so thats why i joined this group...thinking you people will help me
out in learning...
Many people here are happy to help. But you will not learn if you don't do
your homework yourself.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....
Indeed it is easy. However, we have better things to do than other people's
homework.

--
rbh
Jul 10 '07 #7

P: n/a
On Jul 10, 8:03 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
I think that nobody here want to do your homework.
Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....

What problems are those? What needs to be "clarified"? Can you be
specific, please?
so thats why i joined this group...thinking you people will help me
out in learning...

Doing your tasks *for* you is not helping you out in learning.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Yes, it's easy for most of us here. But if you want it to become easy
for *you* as well, *you* need to do it *yourself*.

Imagine somebody wanting to learn to ride a bicycle. It's not going to
help him/her to see somebody else do it. He/she needs to *get on the
damn bicycle* and ride it. If you (as you claim) are learning C++ "as
part" of your "hobby" (and what hobby is that, I wonder?), you need to
start *writing those programs* yourself. Otherwise, don't call it
"learning".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
thanks for showing your concern on the problem...well what comes into
my mind is that we ahave to apply 3 for loops for the first question
>one for space,one for star,and third for the space on the right
side.....in the second part i can think at all....well i tried to use
ascii codes for a to f in a for loop making two for loop one
incrementing and the other decrementing......nd yes third loop for
endl.....now tell me what should i do....
thanks

Jul 10 '07 #8

P: n/a
On Jul 10, 10:52 am, tanmay <agno3_10...@yahoo.comwrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
I think that nobody here want to do your homework.
Markus

well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
Bullshit.
so thats why i joined this group...thinking you people will help me
out in learning...
Sure, we'll help you out in learning, if that's what your really
want. But all you're doing is posting a problem and expecting an
answer. If you really want to learn something, try it yourself.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....
And I'd really appreciate it if you'd stop using all of those dots in
your writing. And did your own homework.

Jul 10 '07 #9

P: n/a
On Jul 10, 8:03 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
I think that nobody here want to do your homework.
Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....

What problems are those? What needs to be "clarified"? Can you be
specific, please?
so thats why i joined this group...thinking you people will help me
out in learning...

Doing your tasks *for* you is not helping you out in learning.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Yes, it's easy for most of us here. But if you want it to become easy
for *you* as well, *you* need to do it *yourself*.

Imagine somebody wanting to learn to ride a bicycle. It's not going to
help him/her to see somebody else do it. He/she needs to *get on the
damn bicycle* and ride it. If you (as you claim) are learning C++ "as
part" of your "hobby" (and what hobby is that, I wonder?), you need to
start *writing those programs* yourself. Otherwise, don't call it
"learning".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Jul 10, 8:03 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
I think that nobody here want to do your homework.
Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....

What problems are those? What needs to be "clarified"? Can you be
specific, please?
so thats why i joined this group...thinking you people will help me
out in learning...

Doing your tasks *for* you is not helping you out in learning.
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Yes, it's easy for most of us here. But if you want it to become easy
for *you* as well, *you* need to do it *yourself*.

Imagine somebody wanting to learn to ride a bicycle. It's not going to
help him/her to see somebody else do it. He/she needs to *get on the
damn bicycle* and ride it. If you (as you claim) are learning C++ "as
part" of your "hobby" (and what hobby is that, I wonder?), you need to
start *writing those programs* yourself. Otherwise, don't call it
"learning".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
thanks for showing your concern on the problem...well what comes into
my mind is that we ahave to apply 3 for loops for the first question
>one for space,one for star,and third for the space on the right
side.....in the second part i can't think at all....well i tried to
use ascii codes for a to f in a for loop making two for loop one
incrementing and the other decrementing......nd yes third loop for
endl.....now tell me what should i do....
thanks

Jul 10 '07 #10

P: n/a
"Victor Bazarov" writes:
Imagine somebody wanting to learn to ride a bicycle. It's not going to
help him/her to see somebody else do it. He/she needs to *get on the
damn bicycle* and ride it. If you (as you claim) are learning C++ "as
part" of your "hobby" (and what hobby is that, I wonder?), you need to
start *writing those programs* yourself. Otherwise, don't call it
"learning".
Learning to ride a bicycle has nothing in common with learning to program.
Anyone that ever rode a bicycle can get on one and ride it after no bicycle
exposure for many years. As far as I can tell, if you can still walk, you
can still ride a bicycle. I can't even write a program in languages that I
used to be quite fluent in. I can kind of read them, but not write them
without a refresher.

I would be amazed if there are places that claim to teach one to ride a
bicycle.

Please, no more bicycle analogies.
Jul 10 '07 #11

P: n/a
osmium wrote:
"Victor Bazarov" writes:
>Imagine somebody wanting to learn to ride a bicycle. It's not going
to help him/her to see somebody else do it. He/she needs to *get on
the damn bicycle* and ride it. If you (as you claim) are learning
C++ "as part" of your "hobby" (and what hobby is that, I wonder?),
you need to start *writing those programs* yourself. Otherwise,
don't call it "learning".

Learning to ride a bicycle has nothing in common with learning to
program. [..]

Please, no more bicycle analogies.
For you, anything.
Jul 10 '07 #12

P: n/a
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
>Hello,

I think that nobody here want to do your homework.

Markus

well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
so thats why i joined this group...thinking you people will help me
out in learning...
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....
Really? That seems to contradict what you said here:

http://groups.google.com/group/alt.c...35f97f9f9b2a6a

--
SM
rot13 for email
Jul 10 '07 #13

P: n/a
On Jul 10, 8:49 pm, Shadowman <funqbjzna...@pbzpnfg.argwrote:
tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
I think that nobody here want to do your homework.
Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
so thats why i joined this group...thinking you people will help me
out in learning...
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Really? That seems to contradict what you said here:

http://groups.google.com/group/alt.c...+/browse_threa...

--
SM
rot13 for email- Hide quoted text -

- Show quoted text -
hey what the contradiction.cant a comp. science engineer learn any
language by his own during vacations.its really dismal that some
people in here pay more importance to what really is unimportant.for
your kind information during my first year at enginnering i learnt
autocad and a little c language.its vacations now and i am learning c+
+ by my own.
any more problems to anyone.feel free to clarify.

Jul 10 '07 #14

P: n/a
tanmay wrote:
thanks for showing your concern on the problem...well what comes into
my mind is that we ahave to apply 3 for loops for the first question
>one for space,one for star,and third for the space on the right
side.....in the second part i can't think at all....well i tried to
use ascii codes for a to f in a for loop making two for loop one
incrementing and the other decrementing......nd yes third loop for
endl.....now tell me what should i do....
thanks

You problem has more to do with algorithmic thinking than programming in
c++.
First of all think about _every step_ you have to do to get these star
pattern.
Seceondly write it down with pseudo code.
At last, translate it to c++ code.
Markus
Jul 10 '07 #15

P: n/a
On 2007-07-10 18:09, tanmay wrote:
On Jul 10, 8:49 pm, Shadowman <funqbjzna...@pbzpnfg.argwrote:
>tanmay wrote:
On Jul 10, 7:43 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,
>I think that nobody here want to do your homework.
>Markus
well as u said ..let me clarify....
i am learning c++ as a part of my hobby...and i am not consulting any
teacher......well i had problem which i thought would be clarified
here.....
so thats why i joined this group...thinking you people will help me
out in learning...
sir,sorry to say,but instead of writing all this...i would have really
appriciated if you would have answered my query...as its very easy for
you....

Really? That seems to contradict what you said here:

http://groups.google.com/group/alt.c...+/browse_threa...

--
SM
rot13 for email- Hide quoted text -

- Show quoted text -

hey what the contradiction.cant a comp. science engineer learn any
language by his own during vacations.its really dismal that some
people in here pay more importance to what really is unimportant.for
your kind information during my first year at enginnering i learnt
autocad and a little c language.its vacations now and i am learning c+
+ by my own.
any more problems to anyone.feel free to clarify.
You claim to have learned some C, then the similarities between C and
C++ should be enough for you to be able to solve these problems, they
are not hard.

--
Erik Wikström
Jul 10 '07 #16

P: n/a
int main()
{
int p=0;
int j,n;
printf("Enter n");
scanf("%d",&n);
j=n-1;
printf("\n\n");
printf("\t\t");
for(int p=0;p<2*n-1;p++)
{
for(int i=abs(j);i<n;i++)
printf("* ");
j--;
if(j>=0)
{
printf("\n\t\t");
int k=n-1-j;
while(k>0)
{printf("\b\b");k--;}
}
else
{
printf("\n\t\t");
int k=n-1-abs(j);
while(k>0)
{printf("\b\b");k--;}
}
}
}

happy programming..
lets discuss on complexity in this respect...
can anyone suggest how can the program be made more efficient..

....
Coding is not a big deal but efficiency is...

Jul 11 '07 #17

P: n/a
sumedh..... wrote:
int main()
{
int p=0;
int j,n;
printf("Enter n");
scanf("%d",&n);
j=n-1;
printf("\n\n");
printf("\t\t");
for(int p=0;p<2*n-1;p++)
{
for(int i=abs(j);i<n;i++)
printf("* ");
j--;
if(j>=0)
{
printf("\n\t\t");
int k=n-1-j;
while(k>0)
{printf("\b\b");k--;}
}
else
{
printf("\n\t\t");
int k=n-1-abs(j);
while(k>0)
{printf("\b\b");k--;}
}
}
}

happy programming..
lets discuss on complexity in this respect...
can anyone suggest how can the program be made more efficient..

...
Coding is not a big deal but efficiency is...

I'd make the exact reverse argument. Given the poor indentation,
structure, variable names, and lack of comments, it's very difficult to
figure out what your program is supposed to do.

In the "Real World"(tm), programs aren't written and forgotten. They're
maintained by people, often people who didn't originally write them.
The coding style and readability/maintainability/scalability are much
more important than the sort of micro-optimization that you are asking
about.

Have you benchmarked this code to see if there's a bottleneck? How do
you know it's not "efficient"? For that matter, please define "efficient".
Jul 11 '07 #18

P: n/a
On Jul 10, 9:09 am, tanmay <agno3_10...@yahoo.comwrote:
hey what the contradiction.cant a comp. science engineer learn any
language by his own during vacations.its really dismal that some
people in here pay more importance to what really is unimportant.for
your kind information during my first year at enginnering i learnt
autocad and a little c language.its vacations now and i am learning c+
+ by my own.
any more problems to anyone.feel free to clarify.
Alright, I'll play...
Since you don't seem to want to learn anything about this problem
yourself, and just want a solution, I'll post one.

Just for the fun of it, I've combined both problems into a single
program though. Now if you need this for homework, at least you'll
have to figure out how to separate the problems yourself and have to
explain the coding style to your teacher (if there is one) :).

Here ya go:

#include <iostream>

const unsigned DIAMOND_HEIGHT = 9;

int main()
{
for( unsigned y=0; y<DIAMOND_HEIGHT; ++y )
{
unsigned chars = (y<DIAMOND_HEIGHT/2) ? y*2 + 1 :
(DIAMOND_HEIGHT-y)*2 - 1;
unsigned spaces = (DIAMOND_HEIGHT - chars) / 2;
char ch = 'a';

for( unsigned x = 0; x < chars + spaces; ++x )
std::cout << ((x < spaces) ? ' ' : (x < spaces + chars/2 ?
ch++ : ch--)) << ' ';

std::cout << std::endl;
}
}
Output:

a
a b a
a b c b a
a b c d c b a
a b c d e d c b a
a b c d c b a
a b c b a
a b a
a

Jul 12 '07 #19

P: n/a
You should try by yourself if you really want to learn something, but
for fun and for the discussion of efficiency both for computer and for
a human-being to read the program, I post my program, it's easier to
read and understand, thus more efficient.

1 #include <stdio.h>
2 #include <memory.h>
3
4 #define ROW 10
5 #define COL 19
6
7 int main()
8 {
9 int i = 0;
10 int j = 0;
11
12 int star_pos = 0;
13
14 int isStar[ROW][COL];
15 memset(isStar, 0, sizeof(int) * ROW * COL);
16
17 for(i = 0; i < ROW; i++) {
18 star_pos = COL/2 - i;
19 for(j = 0; j <= i; j++) {
20 isStar[i][star_pos] = 1;
21 star_pos += 2;
22 }
23 }
24
25 for(i = 0; i < ROW; i++) {
26 for(j = 0; j < COL; j++)
27 if(isStar[i][j])
28 printf("*");
29 else
30 printf(" ");
31 printf("\n");
32 }
33
34
35 return 0;
36 }

Jul 13 '07 #20

P: n/a
le**********@gmail.com wrote:
You should try by yourself if you really want to learn something, but
for fun and for the discussion of efficiency both for computer and for
a human-being to read the program, I post my program, it's easier to
read and understand, thus more efficient.

1 #include <stdio.h>
2 #include <memory.h>
3
4 #define ROW 10
5 #define COL 19
[...]

Is there also a C++ version of the code?

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jul 13 '07 #21

P: n/a
le**********@gmail.com wrote:
You should try by yourself if you really want to learn something, but
for fun and for the discussion of efficiency both for computer and for
a human-being to read the program, I post my program, it's easier to
read and understand, thus more efficient.
[code omitted]
My version takes readability one step further!

#include <iostream>

int main()
{
std::cout <<
" *\n"
" * *\n"
" * * *\n"
"* * * *\n"
" * * *\n"
" * *\n"
" *\n";

std::cout <<
"abcdefgfedcba\n"
" abcdefedcba\n"
" abcdedcba\n"
" abcdcba\n"
" abcba\n"
" aba\n"
" a\n";

// Satisfy requirement to use a for loop.
for (;0;);
}

--
Alan Johnson
Jul 13 '07 #22

P: n/a
On 7 13 , 5 24 , Alan Johnson <aw...@yahoo.comwrote:
leomayleo...@gmail.com wrote:
You should try by yourself if you really want to learn something, but
for fun and for the discussion of efficiency both for computer and for
a human-being to read the program, I post my program, it's easier to
read and understand, thus more efficient.
[code omitted]

My version takes readability one step further!

#include <iostream>

int main()
{
std::cout <<
" *\n"
" * *\n"
" * * *\n"
"* * * *\n"
" * * *\n"
" * *\n"
" *\n";

std::cout <<
"abcdefgfedcba\n"
" abcdefedcba\n"
" abcdedcba\n"
" abcdcba\n"
" abcba\n"
" aba\n"
" a\n";

// Satisfy requirement to use a for loop.
for (;0;);

}

--
Alan Johnson
What if you are given a star triangle with 1000 lines, you will write
by yourself or let the "loop" do it for you?

Jul 13 '07 #23

P: n/a
On 7 13 , 5 17 , "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.dewrote:
leomayleo...@gmail.com wrote:
You should try by yourself if you really want to learn something, but
for fun and for the discussion of efficiency both for computer and for
a human-being to read the program, I post my program, it's easier to
read and understand, thus more efficient.
1 #include <stdio.h>
2 #include <memory.h>
3
4 #define ROW 10
5 #define COL 19

[...]

Is there also a C++ version of the code?

--
Thomashttp://www.netmeister.org/news/learn2quote.html
Of course you can implement this idea with C++ as you wish. Change
#include <stdio.hto #include <iostreamand add "using namespace
std;" after it. Replace the macro definitions with "const int ROW =
10;" and "const int COL = 19;" and replace "printf" with "cout", you
will get your C++
implementation.

Jul 13 '07 #24

P: n/a

<le**********@gmail.comwrote in message...
On 7 13 , 5 24 , Alan Johnson <aw...@yahoo.comwrote:
[code omitted]
My version takes readability one step further!

#include <iostream>
int main(){
std::cout <<" *\n * *\n * * *\n"
"* * * *\n * * *\n * *\n *\n";
std::cout <<"abcdefgfedcba\n abcdefedcba\n"
" abcdedcba\n abcdcba\n"
" abcba\n aba\n a\n";
// Satisfy requirement to use a for loop.
for (;0;);
} // Alan Johnson

What if you are given a star triangle with 1000 lines, you will write
by yourself or let the "loop" do it for you?
What if the Sun went supernova, would you still post
'C'-like code to *this* NG?

--
Bob R
POVrookie
Jul 13 '07 #25

This discussion thread is closed

Replies have been disabled for this discussion.