C++ question!

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
However, if you combine the idea of my pseudo code with his code you'd be drawing both lines at one time. You'd probably have to modify mine a bit cuz it took some shortcuts.

Think about it this way:
You have a variable specifying the place to make a mark for the tl to br line. You have the same for the tr to bl line. tlbr starts at zero and goes up one each time you start a new row. trbl starts at width and goes down one each time you start a new row.

For each row you iterate over the width and each time your index matches either tlbr or trbl you print an X instead of a space. It would get a little trickier if you're dealing with something that's not square, however.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: kamper
Originally posted by: notfred
I forget the syntax for cout, but the logic is correct.

for( int ii = 0; ii < height; ii++ ){
for( int jj = 0; jj < width; jj++ ){
if (ii == jj){
cout << "Z";
}
else{
cout << " ";
}
}
cout << "\n";
}

Maybe I'm reading that wrong but won't that draw a line from top-left to bottom-right?

So reverse it. He said he was having trouble drawing a diagonal. If he wants to do it the ther direction, he olny has to change one line:
for( int jj = width; jj > 0; jj-- ){
 

imported_jediknight

Senior member
Jun 24, 2004
343
0
0
Originally posted by: BingBongWongFooey[
edit: and remember to free() whatever you malloc() (or calloc() for that matter) after you're done with it; I forgot to. ;)

edit2: nevermind, I forgot again that you're using c++. don't use malloc/free

Yes, it's new and delete, or new[] and delete[] (don't mix and match!)

 

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
Originally posted by: notfred
Originally posted by: kamper
Originally posted by: notfred
I forget the syntax for cout, but the logic is correct.

for( int ii = 0; ii < height; ii++ ){
for( int jj = 0; jj < width; jj++ ){
if (ii == jj){
cout << "Z";
}
else{
cout << " ";
}
}
cout << "\n";
}

Maybe I'm reading that wrong but won't that draw a line from top-left to bottom-right?

So reverse it. He said he was having trouble drawing a diagonal. If he wants to do it the ther direction, he olny has to change one line:
for( int jj = width; jj > 0; jj-- ){

omg. never thought of changing the for :S i also have to add a + 1 to count == linecount, or else its not aligned. thanks guys!
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: WarDemon666
omg. never thought of changing the for :S i also have to add a + 1 to count == linecount, or else its not aligned. thanks guys!

Yeah, to make your Z's go the other way, count down through them instead of up :)

It's obvious you're new to this, but once you get the hang of it, this kind of thing becomes intuitive.
 

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
Originally posted by: notfred
Originally posted by: WarDemon666
omg. never thought of changing the for :S i also have to add a + 1 to count == linecount, or else its not aligned. thanks guys!

Yeah, to make your Z's go the other way, count down through them instead of up :)

It's obvious you're new to this, but once you get the hang of it, this kind of thing becomes intuitive.


You think theres another way to do this? now im stuck on the X. lol. But you don't have to help, ill try and figure this one on my own
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: jediknight
Originally posted by: BingBongWongFooey[
edit: and remember to free() whatever you malloc() (or calloc() for that matter) after you're done with it; I forgot to. ;)

edit2: nevermind, I forgot again that you're using c++. don't use malloc/free

Yes, it's new and delete, or new[] and delete[] (don't mix and match!)

But it's best to just avoid explicit memory allocation/deletion altogether, by allocating things on the stack, and using STL containers. (I pretty much never use new or delete)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: WarDemon666
Originally posted by: notfred
Originally posted by: WarDemon666
omg. never thought of changing the for :S i also have to add a + 1 to count == linecount, or else its not aligned. thanks guys!

Yeah, to make your Z's go the other way, count down through them instead of up :)

It's obvious you're new to this, but once you get the hang of it, this kind of thing becomes intuitive.


You think theres another way to do this? now im stuck on the X. lol. But you don't have to help, ill try and figure this one on my own

Change one different line:
if ( (ii == jj) || (width-ii == jj) ){
 

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
ah crap, i got the V, but its supposed to look like this:

edit: was messed up, check out the code

any ideas? gotta move it over one and add one in the middle at the end, but thats not 'good' programming is it?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: WarDemon666
ah crap, i got the V, but its supposed to look like this:

edit: was messed up, check out the code

any ideas? gotta move it over one and add one in the middle at the end, but thats not 'good' programming is it?

Good work! If you can do the V you can probably handle most letters that have more than one line at a time. I think that your problem will depend entirely on whether or not an even or odd width is specified. If it's even you'll get that double at the bottom and there's not much you can do except modify the width (which would go against your requirements). Put in an odd width and it should line up nicely.
 

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
Originally posted by: kamper
Originally posted by: WarDemon666
ah crap, i got the V, but its supposed to look like this:

edit: was messed up, check out the code

any ideas? gotta move it over one and add one in the middle at the end, but thats not 'good' programming is it?

Good work! If you can do the V you can probably handle most letters that have more than one line at a time. I think that your problem will depend entirely on whether or not an even or odd width is specified. If it's even you'll get that double at the bottom and there's not much you can do except modify the width (which would go against your requirements). Put in an odd width and it should line up nicely.

In the example they used 5 as the width, thats what I used, and i didnt get the same thing :(

edit: i got it. had to change count == linecount +1 to count == linecount +2 WTF.

ok this is weird. tell me what you guys think of the code, is there a better way?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I figured that a width of 5 would produce the V shown below. By saying that count = columns * 2 you've doubled your width, imo.

However, if this is what you want then I would change count = columns * 2 to count = (columns * 2) - 1. Or +1, depending on if you want round down or up.
 

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
Originally posted by: kamper
I figured that a width of 5 would produce the V shown below. By saying that count = columns * 2 you've doubled your width, imo.

However, if this is what you want then I would change count = columns * 2 to count = (columns * 2) - 1. Or +1, depending on if you want round down or up.

We have to do it exactly as on the sheet, and it looks like how I did it, so im guessing she had to double her width also?

Anyways, ill leave it like that, any suggestions?

I used a "switch case" to find the code for the specific letter instead of using an if statement, does this make sense? we didnt go over the switch yet but i found it out, as I already knew about the select case from vb.

im tired

Thanks ppl
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
A switch is fine. It's a little more dangerous, syntactically speaking, since you have no curly braces to delimit each option and you have to remember the break statement everytime but it makes more sense, semantically speaking, for constant values like characters. Wow, I'm tired too :shocked: