Help with a program that makes a square

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punkybrewster
    New Member
    • Oct 2006
    • 11

    Help with a program that makes a square

    My task is to write a program that reads in from the user the size of the sides of a square and prints a hollow square of that size in asterisks and spaces. A nested loop should be done. An example of the loop should look like the following:

    Enter the size of a square: 5

    * * * * *
    * *
    * *
    * *
    * * * * *

    No matter what number the user input the program should produce a hollow square except when the user input 0 or a negative number.

    The middle of the square should have no asterisk I was trying my best to show an example.

    I will really appreciate the help.
  • nbras
    New Member
    • Oct 2006
    • 4

    #2
    hi !

    this is very easy

    please try to use this code
    Code:
    int size;
    	cout<<"Please enter the size of a square:"<<endl;
    	cin>>size;
    	for(int i=0;i<size;i++)
    	{
    		cout<<endl;
    	for(int j=0;j<size;j++)
    	{
    		if(i==0||i==size-1)
    			cout<<"*";
    		else
    			if(j==0||j==size-1)
    				cout<<"*";
    			else
    				cout<<" ";
    	}
    	}

    Comment

    • punkybrewster
      New Member
      • Oct 2006
      • 11

      #3
      Thank you so much. It work for all scenario. You could you help me with another situation. I have to write a program that computes and displays a user-supplied base number (which can be fractional) raised to a user-supplied integer exponent (which can be positive, negative or zero. This should be the result example:

      Enter a base number: 1.75
      Enter a non-negative integer exponent: -2
      1.75 raised to the power -2 = 0.326531

      A while loop should be used.

      Also based on your solution to my square problem how would you write it as a while loop instead of for loops?




      Originally posted by nbras
      hi !

      this is very easy

      please try to use this code
      Code:
      int size;
      	cout<<"Please enter the size of a square:"<<endl;
      	cin>>size;
      	for(int i=0;i<size;i++)
      	{
      		cout<<endl;
      	for(int j=0;j<size;j++)
      	{
      		if(i==0||i==size-1)
      			cout<<"*";
      		else
      			if(j==0||j==size-1)
      				cout<<"*";
      			else
      				cout<<" ";
      	}
      	}

      Comment

      • jessy
        New Member
        • Oct 2006
        • 106

        #4
        I sent you the Code hunny check it again on ur compiler

        Comment

        • punkybrewster
          New Member
          • Oct 2006
          • 11

          #5
          I got you solution but it doesn't give me the results when 1.75 is raised to the -2 to be equal 0.326531.



          Originally posted by jessy
          I sent you the Code hunny check it again on ur compiler

          Comment

          • jessy
            New Member
            • Oct 2006
            • 106

            #6
            #include<iostre am.h>
            #include<conio. h>
            #include<math.h >

            void main()

            {
            clrscr();
            double x, y;

            double product=1 ;

            cout << "Enter a base number: ";
            cin >> x;
            cout << "Enter an integer exponent: ";
            cin >> y;



            int i = 1;

            if (y<0)
            {
            y=-y;

            while (y>=i)

            { product *= x;

            i++;
            }
            cout<<x<<"to the power of "<<y<<"equals=" <<(1/product);
            }
            else {


            while ( y >= i)
            {

            product *= x;

            i++;
            }


            cout<<x<<"to the power of "<<y<<"equals=" <<product;
            }
            getch();

            }

            Comment

            Working...