Row/Col :S

Started by
11 comments, last by BaldPython 18 years, 7 months ago
Quote:Original post by xMcBaiNx
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.)


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.
Advertisement
Quote:Original post by BaldPython
Quote:Original post by xMcBaiNx
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.)


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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
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;}

This topic is closed to new replies.

Advertisement