question about forming X shape in C++

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1605 Views - Last Post: 22 February 2013 - 05:24 PM Rate Topic: -----

#16 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,214
  • Joined: 20-March 10

Re: question about forming X shape in C++

Posted 10 February 2013 - 01:48 PM

It depends on the X shape you want to form

is it

x x
 x
x x


or
 x       x
  x     x
   x   x
    x x
     x
    x x
   x   x
  x     x
 x       x


or something else..

however I urge you to work this out on graph paper because its very easy to make this program.

Snoopy.

This post has been edited by snoopy11: 10 February 2013 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#17 helloworld1234  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 09-February 13

Re: question about forming X shape in C++

Posted 10 February 2013 - 01:59 PM

View PostButchDean, on 10 February 2013 - 01:35 PM, said:

So, maybe amend your code with what you do understand, post the amendments and let us comment? Sitting back and expecting us you point out issues with your code - effectively fixing it for you is not the way to learn. This is an evolutionary process in which you are the driver, not us, so drive. :)/>


I have tried "amending" it and nothing i've tried has worked so far. I don't know what to fix, where to start, where to stop, which is why i posted here. Isn't that the point of a forum? I am obviously not expecting anyone to write the code for me but telling me stuff such as "you can do it in one loop instead of 3" doesn't help me because i don't know how to do that.

once again, thanks for your input. If there's nothing else you'd like to add in terms of the code, that's fine.

I also don't mean to sound ungrateful because i appreciate everyone's response on this. I am just having a hard time with this specific shape, which i need to create using for loops. And as I have said before, for loops are a concept that i am struggling with so even looking at other examples, I am unable to come up with the correct solution.

but yeah, i can gather you guys have contributed as much as you're willing to, so thanks a lot for your help.

View Postsnoopy11, on 10 February 2013 - 01:48 PM, said:

It depends on the X shape you want to form

is it

x x
 x
x x


or
 x       x
  x     x
   x   x
    x x
     x
    x x
   x   x
  x     x
 x       x


or something else..

however I urge you to work this out on graph paper because its very easy to make this program.

Snoopy.



The shape depends on what number the person enters. so if they enter 5, there should be five asterisks going diagonal both ways.
Was This Post Helpful? 0
  • +
  • -

#18 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: question about forming X shape in C++

Posted 10 February 2013 - 02:25 PM

You started with two loops, so I'm sticking with that. Look at this. Do you see the need for these variables, and how they're basically starting the two loops?
for(row=0;row<rowmax;row++) {
   for(col=0;col<rowmax;col++) {
      ok=0;
      if(row equals 0 or row equals rowmax-1)
         //we want ok to switch to 1, because we need to print out the 
         //top and bottom rows, right here
      else {
         //this handles all the interior rows printing:
         //what goes in here?

      }
      if(ok)
         putchar(c);  //char c = '*', btw.
      else
         putchar(' ');
   }
   putchar('\n'); 
}
putchar('\n');



Do you see how the if(statement) prints the first and the last row correctly? That's the easy part.

The else part handles all the other rows. What should turn ok to 1 (and thus cause a * to be printed)?

: if col is 0 or col is rowmax-1 (the last column is rowmax-1 in value), since the box is a square.

: if col is equal to the row number, then ok should be assigned one. That handles the top-left to bottom-right diagonal
printing of *'s.

Study the above and work it through with pen and paper (or use an editor like notepad), until you can see that much, clearly.

The upper-right to lower left diagonal is worked out with similar arithmetic.

This post has been edited by Adak: 10 February 2013 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#19 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,214
  • Joined: 20-March 10

Re: question about forming X shape in C++

Posted 10 February 2013 - 02:59 PM

Hi,

you could do something like this

have two loops one loop from 1 to the number of X's to appear
the other loop from 1 to less than the number of X's to appear
put a couple of test conditions in to print out X's or spaces inside the loops

something like where a is an incremented variable and y is a loop variable.

if ((a==y) || (y==number))
{
  printf("x ");
}
else
{
printf("  ");
}


Snoopy.
Was This Post Helpful? 1
  • +
  • -

#20 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,583
  • Joined: 16-October 07

Re: question about forming X shape in C++

Posted 10 February 2013 - 05:29 PM

How would I do this? Two loops:
char getPixelForX(const int row, const int col, const int number);

bool drawX(const int number) {
	if (number < 2 || number % 2 == 0) {
		cout << "Value must be an odd number greater than or equal to 2";
		return false;
	}
	for(int row=0; row<number; row++) {
		for(int col=0; col<number; col++) {
			cout << getPixelForX(row, col, number);
		}
		cout << endl;
	}
	return true;
}



You see, figuring out if that pixel is lit should be a function of those three values. That's the logic.

Can you do that in one loop? Sure:
bool drawX(const int number) {
	if (number < 2 || number % 2 == 0) {
		cout << "Value must be an odd number greater than or equal to 2";
		return false;
	}
	for(int i=0, j=number*number; i<j; i++) {
		int row = i / number;
		int col = i % number;
		if(col==0) { cout << endl; }
		cout << getPixelForX(row, col, number);
	}
	cout << endl;
	return true;
}



Is one loop better? I don't think so. It's honestly kind of a mess. Could you write it cleaner? Sure. But you'd still be going through pointless extra effort when row and col are implicitly clear.

What does getPixelForX look like? Well, that's an exercise of the reader. Perhaps boiling it down like this offers a new way to look at the problem.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#21 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: question about forming X shape in C++

Posted 10 February 2013 - 06:57 PM

No, that isn't it either. You can do it with one loop and a 1D array, there is no need for a 2D array!

Please see my diamond example that I posted earlier in this thread.
Was This Post Helpful? 0
  • +
  • -

#22 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: question about forming X shape in C++

Posted 10 February 2013 - 07:03 PM

How goes your program, Helloworld1234?
Was This Post Helpful? 0
  • +
  • -

#23 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,583
  • Joined: 16-October 07

Re: question about forming X shape in C++

Posted 10 February 2013 - 07:51 PM

View PostButchDean, on 10 February 2013 - 08:57 PM, said:

Please see my diamond example that I posted earlier in this thread.


The code that obfuscates the columns by using a statically sized c-string? It's a novel solution, but I'm not sure how clear it might be.

Why would you want any array?
Was This Post Helpful? 0
  • +
  • -

#24 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: question about forming X shape in C++

Posted 10 February 2013 - 08:42 PM

View Postbaavgai, on 10 February 2013 - 10:51 PM, said:

Why would you want any array?

If you analyze the code you will see that for every iteration two locations are overwritten, and you need an index to work out which locations are updated in the array.
Was This Post Helpful? 0
  • +
  • -

#25 helloworld1234  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 09-February 13

Re: question about forming X shape in C++

Posted 22 February 2013 - 04:04 PM

View PostAdak, on 10 February 2013 - 07:03 PM, said:

How goes your program, Helloworld1234?


it went alright. the professor explained it in class.
thanks for asking=)
Was This Post Helpful? 0
  • +
  • -

#26 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: question about forming X shape in C++

Posted 22 February 2013 - 05:24 PM

What was the professor's solution?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2