Jump to content
  • Advertisement
Sign in to follow this  
BaldPython

Row/Col :S

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by bjle
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.



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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by xMcBaiNx
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.


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by xMcBaiNx
x is lesser than N
y 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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!