468,760 Members | 1,787 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,760 developers. It's quick & easy.

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.
Oct 29 '06 #1
5 7416
nbras
4
hi !

this is very easy

please try to use this code
Expand|Select|Wrap|Line Numbers
  1. int size;
  2.     cout<<"Please enter the size of a square:"<<endl;
  3.     cin>>size;
  4.     for(int i=0;i<size;i++)
  5.     {
  6.         cout<<endl;
  7.     for(int j=0;j<size;j++)
  8.     {
  9.         if(i==0||i==size-1)
  10.             cout<<"*";
  11.         else
  12.             if(j==0||j==size-1)
  13.                 cout<<"*";
  14.             else
  15.                 cout<<" ";
  16.     }
  17.     }
Oct 29 '06 #2
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?




hi !

this is very easy

please try to use this code
Expand|Select|Wrap|Line Numbers
  1. int size;
  2.     cout<<"Please enter the size of a square:"<<endl;
  3.     cin>>size;
  4.     for(int i=0;i<size;i++)
  5.     {
  6.         cout<<endl;
  7.     for(int j=0;j<size;j++)
  8.     {
  9.         if(i==0||i==size-1)
  10.             cout<<"*";
  11.         else
  12.             if(j==0||j==size-1)
  13.                 cout<<"*";
  14.             else
  15.                 cout<<" ";
  16.     }
  17.     }
Oct 29 '06 #3
jessy
106 100+
I sent you the Code hunny check it again on ur compiler
Oct 29 '06 #4
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.



I sent you the Code hunny check it again on ur compiler
Oct 29 '06 #5
jessy
106 100+
#include<iostream.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();

}
Oct 29 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Anthony Jones | last post: by
7 posts views Thread by brian.digipimp | last post: by
5 posts views Thread by Matt | last post: by
1 post views Thread by Rahul | last post: by
8 posts views Thread by darnnews | last post: by
3 posts views Thread by JWest46088 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.