question about forming X shape in C++

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 helloworld1234  Icon User is offline

  • New D.I.C Head

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

question about forming X shape in C++

Posted 09 February 2013 - 03:51 PM

hey, can you guys please help me figure out how to make an x shape. so far, all i can do is get the one diagonal side with the following code, but i can't get the other side. i'd really appreciate any help. i really suck at for loops.
thanks a lot

#include <iostream>
using namespace std;

int main()
{
int number;


cout<< "Enter a number: ";
cin>> number;

while (number< 2 || number% 2 == 0)
{
cout << "Invalid response. Enter an odd number greater than or equal to 2:";
cin>> number;
}

for( int lineNum=0; lineNum<number; lineNum++)
{
cout <<"*";
for (int i=0; i<number-lineNum; i++)
cout<<" ";
cout<<endl;

for (int x= 0; x<= lineNum; x++)
cout<< " ";

}





cout<< endl;
system( "Pause");
return 0;

}


This post has been edited by ButchDean: 09 February 2013 - 05:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: question about forming X shape in C++

#2 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 09 February 2013 - 05:04 PM

Don't forget to check your code tags!

You are aware you don't need more than one loop, right?

Check out my solution to the diamond problem. You will need to understand the code and modify for your task.

http://www.dreaminco...diamond-problem
Was This Post Helpful? 0
  • +
  • -

#3 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 09 February 2013 - 08:00 PM

Enter an odd number > or equal to 2?


< wait for it >



Wouldn't that just be > = 3? ;)

As ButchDean indicated, two loops, one nested inside the other, will really help here. Outer loop handles the row counting. Inner loop handles the column printing in the row.

Pretty sweet, and makes it as simple as it can be.
Was This Post Helpful? 0
  • +
  • -

#4 moss65  Icon User is offline

  • New D.I.C Head

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

Re: question about forming X shape in C++

Posted 09 February 2013 - 08:20 PM

I think you need to use indents and divide your code by two loops like this code

#include <iostream>

using namespace std;
void X(int size, char character);   //prototype for X shape


int main()
{
	int		size;
	char	character;
	
	cout << "Enter size: " ;
	cin >> size;
	cout << "Enter a single character that will be used to make the X:  ";
	cin >> character;
	cout <<  "\n\n";
    

	X(size, character); //function call

	cout << "\n\n";
	system( "Pause");

	return 0;
}
	

	void X(int size, char character)  //function
	{
		int temp = size;
		int	i,
			r;
		cout << "\n\n";
for(r = size/2; r < size; r++)  // rows
		{  
			for(i= 0 ; i < temp; i++)  //indenting row to first X character
				cout << " ";
			temp++;
			cout << character;
			for(i=(size-r)*2-1;i>1; i--)  //determining how many spaces to include on a given row
				cout << " ";
				cout << character << endl;	
				
		}		
for(r = 1; r<=size/2; r++)  // rows
		{  
			for(i=2 ; i < temp;i++)  //indenting row to first X character
				cout << " ";
			temp--;
			cout << character;
			for(i=0; i<=2*r-1; i++)  //determining how many spaces to include on a given row
				cout << " ";
			cout << character << endl;

		}
		
	}
[code]


You can use the validation for odd numbers and if the X doesn't satisfy you, play with code to get what you want.
Was This Post Helpful? 0
  • +
  • -

#5 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 09 February 2013 - 09:56 PM

View PostAdak, on 09 February 2013 - 11:00 PM, said:

As ButchDean indicated, two loops, one nested inside the other, will really help here. Outer loop handles the row counting. Inner loop handles the column printing in the row.

Pretty sweet, and makes it as simple as it can be.

No I didn't, and no. Only one loop is required. Nested loops is a poor solution.
Was This Post Helpful? 0
  • +
  • -

#6 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:10 AM

Sorry ButchDean, I read your post incorrectly, as "do need", instead of "don't need".

I use 2 for loops for these kinds of pattern prints. I know you CAN do it with one loop, but that isn't as easy imo.

What is "poor" about it? It seems intuitive, since the problem has two dimensions, use one for loop for each dimension.
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,237
  • Joined: 20-March 10

Re: question about forming X shape in C++

Posted 10 February 2013 - 02:41 AM

View PostAdak, on 10 February 2013 - 09:10 AM, said:

What is "poor" about it? It seems intuitive, since the problem has two dimensions, use one for loop for each dimension.



Its not poor as such as any solution that provides the correct answer

cannot be considered poor.

Its just inelegant as the most elegant solution would use just one loop.

It is more elegant because its more efficient.

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

#8 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:54 AM

Bit hacks would be even more efficient. ;)
Was This Post Helpful? 0
  • +
  • -

#9 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:54 AM

View PostAdak, on 10 February 2013 - 05:10 AM, said:

What is "poor" about it? It seems intuitive, since the problem has two dimensions, use one for loop for each dimension.

O(n) vs. O(n^2). :\

View PostAdak, on 10 February 2013 - 05:54 AM, said:

Bit hacks would be even more efficient. ;)

Really? How?
Was This Post Helpful? 0
  • +
  • -

#10 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 - 10:02 AM

View PostAdak, on 09 February 2013 - 08:00 PM, said:

Enter an odd number > or equal to 2?


< wait for it >



Wouldn't that just be > = 3? ;)/>

As ButchDean indicated, two loops, one nested inside the other, will really help here. Outer loop handles the row counting. Inner loop handles the column printing in the row.

Pretty sweet, and makes it as simple as it can be.



sorry i should have been more clear. the number must be odd and can be 2 or greater. number cannot be less than 2 or even. so 2 is a valid response whereas 1 or 4 isn't
Was This Post Helpful? 0
  • +
  • -

#11 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 - 10:15 AM

I understand what you guys are saying about only needing two loops or one loop, but as i said, I am not good at for loops at all.

What should i change/add in my current code? I understand you guys feel strongly about doing others' homework but I would really appreciate feedback on specific things i should add or take out. I am going to try different things myself and see how it works out.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#12 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 - 11:16 AM

@ButchDean: uses smaller constants. The output is way slower than the computation, in either case.


I was just kidding you about "2 or greater and odd". Since obviously 2 is even.

I would use two for loops - and variables of: row, col, rowmax, and ok. row and col are counters, rowmax is the number of rows in the square. ok is the "ok to print the char c = '*';

   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);
         else
            putchar(' ');
      }
      putchar('\n'); 
   }
   putchar('\n');


This post has been edited by Adak: 10 February 2013 - 11:18 AM

Was This Post Helpful? 1
  • +
  • -

#13 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 - 12:45 PM

Smaller constants? What does that have to do with anything? Using smaller constants over nested loops is still less efficient than a larger constant value for a single loop. Secondly, you also don't need a 2D array.

View Posthelloworld1234, on 10 February 2013 - 01:15 PM, said:

I understand what you guys are saying about only needing two loops or one loop, but as i said, I am not good at for loops at all.

What should i change/add in my current code? I understand you guys feel strongly about doing others' homework but I would really appreciate feedback on specific things i should add or take out. I am going to try different things myself and see how it works out.

Thanks again

Kind of what we're debating about here, you should be picking this up and amending your code accordingly.
Was This Post Helpful? 0
  • +
  • -

#14 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:09 PM

View PostButchDean, on 10 February 2013 - 12:45 PM, said:

Smaller constants? What does that have to do with anything? Using smaller constants over nested loops is still less efficient than a larger constant value for a single loop. Secondly, you also don't need a 2D array.

View Posthelloworld1234, on 10 February 2013 - 01:15 PM, said:

I understand what you guys are saying about only needing two loops or one loop, but as i said, I am not good at for loops at all.

What should i change/add in my current code? I understand you guys feel strongly about doing others' homework but I would really appreciate feedback on specific things i should add or take out. I am going to try different things myself and see how it works out.

Thanks again

Kind of what we're debating about here, you should be picking this up and amending your code accordingly.




I am trying to pick up, but since i have very little knowledge of programming, i don't understand most of what you're talking about. but anyway, thanks for trying.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 01:35 PM

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. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2