# Row/Col :S

This topic is 4520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I actually wanted to make this shape in C++: 00*00 0***0 ***** 0***0 00*00 For this purpose I first defined the number of rows and number of cols above. Then, I made it with five loops. I know it's not a good approach. I tried hard to find a connection between row and column to confine it in two loops but I couldnt make one succesful thing. Can anyone help? I also want to practice on such patterns, can anyone suggest a site where I can find tutorials on it.

##### Share on other sites
Well, you could certainly do that using only two loops, but I'm not sure how your pattern is defined when rows!=cols or when the number of rows/cols is even.

Ignoring those two cases, try using a variable like "numberToSkip" that starts out equal to cols/2. (Since you want to write 2 0's before the first asterisk, and integer 5/2 is 2. If there were seven columns you'd want to skip 3, etc...)

Now, if the current column is greater than the "number to skip", and less than the number of columns MINUS the "number to skip", the character is an asterisk. With each row, you skip one less character.

I'll leave it to you to figure out the rest, such as what to do when the number to skip becomes less than zero.

##### Share on other sites
Quote:
 Original post by bjleWell, you could certainly do that using only two loops, but I'm not sure how your pattern is defined when rows!=cols or when the number of rows/cols is even.Ignoring those two cases, try using a variable like "numberToSkip" that starts out equal to cols/2. (Since you want to write 2 0's before the first asterisk, and integer 5/2 is 2. If there were seven columns you'd want to skip 3, etc...)Now, if the current column is greater than the "number to skip", and less than the number of columns MINUS the "number to skip", the character is an asterisk. With each row, you skip one less character.I'll leave it to you to figure out the rest, such as what to do when the number to skip becomes less than zero.

Well, I also thought of the similar sort of logic before but I used float for that. First I divided col/2 and in my case: 5/2 = 2.5 then I rounded it off to make 3 and then I printed a '*' at that location. (I was using a two dimensional array to do all that ...)

But by using that logic I couldn't figure out how to proceed to next step :'(

##### Share on other sites
Are you sure this isn't homework ?

Assuming:  numberOfColumns == numberOfRows == N            N % 2 != 0           You number the rows from 1 to N

The number of spaces preceeeding, following and the number of stars on row x is given by:

spacesBeforeStars = |(x - (N-1)/2)|
spacesAfterStars = spacesBeforeStars
numberOfStars = totalRows - (spacesBeforeStars + spacesAfterStars)

You then proceed to loop trough each row and compute the necessary things.

##### Share on other sites
Quote:
 Original post by xMcBaiNxAre you sure this isn't homework ?Assuming: numberOfColumns == numberOfRows == N N % 2 != 0 You number the rows from 1 to NThe number of spaces preceeeding, following and the number of stars on row x is given by: spacesBeforeStars = |(x - (N-1)/2)| spacesAfterStars = spacesBeforeStars numberOfStars = totalRows - (spacesBeforeStars + spacesAfterStars)You then proceed to loop trough each row and compute the necessary things.

and please tell if it is the correct translation:

for(int x=1;x<=N;x++){ spacesBeforeStars = |(x - (N-1)/2)| spacesAfterStars  = spacesBeforeStars numberOfStars  = totalRows - (spacesBeforeStars + spacesAfterStars)          for(........) // What should I put here?}

Moreover, using your logic, I'd again have to use more than two loops, so no it no use for me. :(

##### Share on other sites
for(int x=1;x<=N;x++){  spacesBeforeStars = |(x - (N-1)/2)|  spacesAfterStars  = spacesBeforeStars  numberOfStars  = totalRows - (spacesBeforeStars + spacesAfterStars)          for(int y=1; y<=N; y++) {    //you have the row and the column (x,y), and the number of spaces and stars       //for the column.  I'm sure you are able to decide if a point is a space or a     //star here using only the info you have, without another loop.  }}

Note: My C++ is rusty, I don't remember how you can do |...| (which is the absolute value). Probably an abs() function or some such. Or you can do it by hand (if the number is smaller than 0, multiply it by -1).

[Edited by - xMcBaiNx on September 3, 2005 11:24:16 AM]

##### Share on other sites
Quote:
 Original post by xMcBaiNx *** Source Snippet Removed ***Note: My C++ is rusty, I don't remember how you can do |...| (which is the absolute value). Probably an abs() function or some such. Or you can do it by hand (if the number is smaller than 0, multiply it by -1).

Well, I do not understand what do you mean by
'<'
when you wrote them in conditional part of the program.

And no, I'd have to use three or probably four loops because the condition will only work for the first part, i.e,

00*00
0***0
*****

for the rest of the part, I'd certainly require other two loops.

But, apparently, the problem for the first segment isn't solved yet.

##### Share on other sites
Quote:
Original post by BaldPython
Quote:
 Original post by xMcBaiNx *** Source Snippet Removed ***Note: My C++ is rusty, I don't remember how you can do |...| (which is the absolute value). Probably an abs() function or some such. Or you can do it by hand (if the number is smaller than 0, multiply it by -1).

Well, I do not understand what do you mean by 'x<' and 'y<'when you wrote them in conditional part of the program.

x is lesser than N
y is lesser than N

Quote:
 And no, I'd have to use three or probably four loops because the condition will only work for the first part, i.e,00*00 0***0*****

I' sorry, you need to start counting the rows from 0 to n-1. So if there are 5 rows, they will be labeled 0 to 4. The absolute values takes care of the problem, see the example below for 5 rows.

spacesBeforeStarsRow1 = |(0 - (5-1)/2)| = 2
spacesBeforeStarsRow2 = |(1 - (5-1)/2)| = 1
spacesBeforeStarsRow3 = |(2 - (5-1)/2)| = 0
spacesBeforeStarsRow4 = |(3 - (5-1)/2)| = 1
spacesBeforeStarsRow5 = |(4 - (5-1)/2)| = 2

(Note that the loops have to start from 0 now.)

[Edited by - xMcBaiNx on September 3, 2005 12:38:15 PM]

##### Share on other sites
Quote:
 Original post by xMcBaiNxx is lesser than Ny is lesser than N

I was actually talking about that "& l t" character in your code, now that character is changed into "<" in your porgram code.

Although, the first impression really confused me, but when I copied the same thing in my post it changed into "<", so what can I say ... it was just someone else' fault.

##### Share on other sites
Quote:
 I was actually talking about that "& l t" character in your code, now that character is changed into "<" in your porgram code.Although, the first impression really confused me, but when I copied the same thing in my post it changed into "<", so what can I say ... it was just someone else' fault.

<: followed by a semi-colon is the entity for the < character in HTML. The forums had some crazy foo going.

Here's a [far from perfect] working solution.

#include <iostream>#include <cmath>int main() {    int sizeOfDiamond=0;    std::cout << "Enter the size of the diamond: ";    std::cin  >> sizeOfDiamond;	      for(int x=0; x<sizeOfDiamond; x++) {      int spacesBeforeAndAfter = std::abs((x - (sizeOfDiamond-1)/2));      int numberOfStars  = sizeOfDiamond - (spacesBeforeAndAfter*2);            for(int y=0; y<sizeOfDiamond; y++) {        (y>=spacesBeforeStars && y<=sizeOfDiamond-spacesAfterStars-1) ? std::cout << "X" : std::cout << " ";      }      std::cout << std::endl;    }    return 1;}

[Edited by - xMcBaiNx on September 3, 2005 12:59:48 PM]

##### Share on other sites
Quote:
 Original post by xMcBaiNxI' sorry, you need to start counting the rows from 0 to n-1. So if there are 5 rows, they will be labeled 0 to 4. The absolute values takes care of the problem, see the example below for 5 rows.spacesBeforeStarsRow1 = |(0 - (5-1)/2)| = 2spacesBeforeStarsRow2 = |(1 - (5-1)/2)| = 1spacesBeforeStarsRow3 = |(2 - (5-1)/2)| = 0spacesBeforeStarsRow4 = |(3 - (5-1)/2)| = 1spacesBeforeStarsRow5 = |(4 - (5-1)/2)| = 2(Note that the loops have to start from 0 now.)

Sir, I've been saying that it would require four loops, one to move from 0 to N-1. Second to print spaces before Stars, third to print asteriks and fourth to print spaces after stars.

I was looking for an algorithm/pseudocode/hint-program to solve it in two loops.

##### Share on other sites
Quote:
Original post by BaldPython
Quote:
 Original post by xMcBaiNxI' sorry, you need to start counting the rows from 0 to n-1. So if there are 5 rows, they will be labeled 0 to 4. The absolute values takes care of the problem, see the example below for 5 rows.spacesBeforeStarsRow1 = |(0 - (5-1)/2)| = 2spacesBeforeStarsRow2 = |(1 - (5-1)/2)| = 1spacesBeforeStarsRow3 = |(2 - (5-1)/2)| = 0spacesBeforeStarsRow4 = |(3 - (5-1)/2)| = 1spacesBeforeStarsRow5 = |(4 - (5-1)/2)| = 2(Note that the loops have to start from 0 now.)

Sir, I've been saying that it would require four loops, one to move from 0 to N-1. Second to print spaces before Stars, third to print asteriks and fourth to print spaces after stars.

I was looking for an algorithm/pseudocode/hint-program to solve it in two loops.

And I've been trying to help you see the light. The "hint" program is ready to be compiled in the post before your last one.

##### Share on other sites
Quote:
 Original post by xMcBaiNx*** Source Snippet Removed ***

Thank you sir, this is what I came up with:

#include <iostream>#include <cmath>int main() {	      int spacesBeforeStars=0;      int spacesAfterStars=0;      int numberOfStars=0;      int sizeOfDiamond=0;        std::cout << "Enter the size of the diamond: ";    std::cin  >> sizeOfDiamond;	      for(int x=0; x<sizeOfDiamond; x++) {       spacesBeforeStars = abs((x - (sizeOfDiamond-1)/2));       spacesAfterStars  = spacesBeforeStars;       numberOfStars	 = sizeOfDiamond - (spacesBeforeStars*2);            for(int y=0; y<sizeOfDiamond; y++) {        (y>=spacesBeforeStars && y<=sizeOfDiamond-spacesAfterStars-1) ? std::cout << "X" : std::cout << " ";      }      std::cout << std::endl;    }    return 1;}