6 Replies - 22937 Views - Last Post: 15 September 2007 - 05:00 AM Rate Topic: -----

#1 gplogz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-July 07

making a square using iterative loops

Post icon  Posted 22 July 2007 - 03:29 AM

Write a program that will create a rectangle based on length and width. Use 2
iterative/loop statements.
ex. if length = 5
width = 10
display
**********
*........*
*........*
*........*
**********


thats what they instructed us to do...

and this is the code i made

main()
{int nLength, nWidth, A, B;
printf ("enter length: ");
scanf ("%d", nLength);
printf ("enter Width: ");
scanf ("%d", nWidth);
for(A=1;1<=nWidth;A++)
printf ("*");
printf ("\n");
for (A = 2; A <= nLength -1; A++)
printf ("*");
for (B = 2; B <= nWidth -1; B++)
printf ("*");
printf ("\n"); 



everytime i try my program it doesnt display the square its just blank...can any1 help me with this?

Is This A Good Question/Topic? 0
  • +

Replies To: making a square using iterative loops

#2 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: making a square using iterative loops

Posted 22 July 2007 - 05:34 AM

Try improving this code after looking at my response to your other post.

You should try to indent your code to improve reading ease.
It is also a good idea to comment the code so as you and others can easily see what you are trying to do. To comment you can use // which will comment the remainder of the line, or enclose it within the comment delimiters /* and */

eg

int main (void)
{
	int integer; /* This is a comment */

	printf ("\nEnter a number: "); // This is also a comment
	scanf ("%d", &integer); // Assign the input to the variable 'integer'
	
	printf ("The integer you just entered was %d", integer); /* Output the
value held in the variable integer. */

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: making a square using iterative loops

Posted 22 July 2007 - 07:22 AM

You are using scanf() wrong.
scanf ("%d", nLength);
you are passing the number inside nLength, instead of the address to nLenght.

scanf ("%d", &nLength); is the way to do it.

also
for(A=1;1<=nWidth;A++
this wil always return true if nWidth is 1 or larger, witch is probarly are.
and you shuld use some whitespaces as enpey said's.

Zy
Was This Post Helpful? 0
  • +
  • -

#4 redknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-September 07

Re: making a square using iterative loops

Posted 12 September 2007 - 03:34 AM

Im making the same thing with a little difference..
instead of asking user to input the lenght and width, I only ask them
to input a number.. and based on that number the program should make
a rectangle. ex: if I insert 5, the program should display:

*****
*.....*
*.....*
*.....*
*****

the code is:
int main ()
{
	int num, x, y;
	
	/* Asks the user to insert a number between 1 - 20 */

	printf("Input the size of the square (1 - 20): ");
	scanf("%d", &num);
	
   /* Checks if the user insert a correct number */

	if (num > 20)
			{
			printf("Number must be between 1 - 20!\n");
			}
			
	else if (num < 0)
			{
			printf("Number cannot be negative!\n");
			}
	  
   /* the confusing part */
	  
	else
			{
				 for (x = 0; x < num; x++)
				 {
					 for (y = 0; y < num; y++)
					 {
						 if (x==0 || y==0 || x==(num-1))
						 {
							 printf("*");
						 }
						 else if (y==(num-1))
						 {
							 printf("*\n"); 
						 }
						 else 
						 {
							 printf(" ");
						 }
					 }
				 }
			}
			
	getch();
	return 0;
	
}



but.. instead of displaying the correct rectangle, the program shows something like this:

(assuming that the user insert a 5)

******.....* <---- the second line printed right next to the first one..
*.....*
*.....*
*****


please help me and tell me where I did wrong.. thanks.. ;)
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: making a square using iterative loops

Posted 13 September 2007 - 01:12 AM

With a square like this, your first and last lines are 'special' cases - In which case, one approach may be to treat them differently to the rest.

Pseudocode
Begin
   Read( Rows, Columns )
   Print( First row )
   For( I = 0 to Rows-2 )
	  Print( Middle row )
   End For
   Print( Last row )
End


Incidentally, the reason that you're getting the first & 2nd rows together in your program, is that your first row never reaches the else.
//Always true for the first row - x == 0 
if (x==0 || y==0 || x==(num-1))
{
	printf("*");
}

//First row - When x is 0, this condition is never checked.
else if (y==(num-1))
{
	printf("*\n");
}



In general, when you know that you need something to happen once at the end of a nested loop every time that loop runs, you would be better off to put it outside the loop, after the end, eg.
for( x = 0; x < 10; ++x )
{
	for( y = 0; y < 5; ++y )
	{
		//Nested loop
	}

	//Happens after nested loop finishes
	printf("End of nested loop");
} 

This post has been edited by Bench: 13 September 2007 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 redknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-September 07

Re: making a square using iterative loops

Posted 13 September 2007 - 03:22 AM

thank you so much for your help.. my program works well now..
all I did was:

if (x==0 || y==0 || x==(num-1)) <---------						  
{										| I switch these
printf("*"); <----------------------------										
}


else if (y==(num-1))   <------------------
{										 | with these.. hehe
	printf("*\n"); <----------------------
}


Was This Post Helpful? 0
  • +
  • -

#7 YasirMX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 07

Re: making a square using iterative loops

Posted 15 September 2007 - 05:00 AM

View Postgplogz123, on 22 Jul, 2007 - 03:29 AM, said:

Write a program that will create a rectangle based on length and width. Use 2
iterative/loop statements.
ex. if length = 5
width = 10
display
**********
*........*
*........*
*........*
**********


thats what they instructed us to do...

and this is the code i made

main()
{int nLength, nWidth, A, B;
printf ("enter length: ");
scanf ("%d", nLength);
printf ("enter Width: ");
scanf ("%d", nWidth);
for(A=1;1<=nWidth;A++)
printf ("*");
printf ("\n");
for (A = 2; A <= nLength -1; A++)
printf ("*");
for (B = 2; B <= nWidth -1; B++)
printf ("*");
printf ("\n"); 



everytime i try my program it doesnt display the square its just blank...can any1 help me with this?


I guess this comes from Deitel & Deitel, C++ how to program. Well I found this question tough and sought a solution online. Here's the C++ version:


#include <cstdlib>
#include <iostream>

using namespace std;
// squareSize.cpp : Defines the entry point for the console application.
//
int main(int argc, char * argv[])
{
int space = 1, squareSize, verticalCounter=0, horizontalCounter = 0;
cout<<"Enter square size : ";
cin>>squareSize;
if (squareSize < 3 )  {
//cannot form hollow square with size 1 or 2
cerr<<"Cannot form square ! ";
} else {
//place cursor on next line
cout<<endl;
//Print asterisks horizontally
while (squareSize > verticalCounter++)  {
if  ( verticalCounter==1 ) {
while ((squareSize > horizontalCounter++)) {	  //draw firstline
cout<<"*";
}
cout <<endl;
horizontalCounter = 0;
} else if (verticalCounter == squareSize) {		 //draw lastline
while (squareSize > horizontalCounter++ ) {
cout<<"*";
}
cout <<endl;
horizontalCounter = 0;
} else {
cout<<"*";
while (squareSize - 2 > horizontalCounter++) {
cout<<" ";
}
cout<<"*";
cout<<endl;
horizontalCounter = 0;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}




Now you should translate C++ code into pseudo code and should also be able to follow the logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1