looping problems

B

BobR

sumedh..... said:
int main(){
int p=0;

Good, you inited 'p'. Problem: you never use this 'p'.

Bad, you didn't init them. Or document what they are!!
( hint: give them a better name.)

printf("Enter n");

scanf("%d",&n);

How do you know 'n' contains a valid value?
( I thought you wanted to be 'efficient'?)

Where did 'j' come from? Oh, way up there! Why?
Why did you subtract one from 'n'? Document it!!

int j( n-1 ); // 'n' decremented by one, because I wanted to.
printf("\n\n");
printf("\t\t");

Why couldn't you combine those two?
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--;
}
} // if(j)
else{
printf("\n\t\t");
int k = n - 1 - abs( j );
while( k > 0 ){
printf("\b\b");
k--;
}
} // else [if(j)]
} // for(p)

See, the first 'p' has never been touched. How 'efficient' is that?

return 0; // optional. give main() a proper burial.
} // main()

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

What Red said.
 
I

int2str

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
 
G

Guest

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[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[j])
28 printf("*");
29 else
30 printf(" ");
31 printf("\n");
32 }
33
34
35 return 0;
36 }
 
T

Thomas J. Gritzan

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?
 
A

Alan Johnson

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;);
}
 
G

Guest

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;);

}

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

Guest

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?

Of course you can implement this idea with C++ as you wish. Change
#include <stdio.h> to #include <iostream> and 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.
 
B

BobR

[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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top