Jump to content

assignment

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
bluetxxth

bluetxxth

    Newbie

  • Members
  • Pip
  • 3 posts
Hi all,

I have an assignment in which I am supposed to make write a program which writes a square to the screen this program is supposed to allow the user to choose character to use, width height and indentation.

I am able to do all except when it comes to the indentation. I am only succeeding in implemented the indentation for the first row of the square but not the other.

Please anyone...help will be apprecciated. See code below
#include <stdio.h>
#include <stdlib.h>

/* This program makes a square filled with the characters of choice*/


int main(int argc, char *argv[])
{
  
  char symbol[2]; // takes care of the symbol to print figures.
  char a[10]; // takes care of wolcome message name input which is later the greeting on the screen.
 	int height; // squares heigth
 	int width; // squares  width
 	int row; // variable for rows
 	int column; // variable for columns
 	char c = ' '; /*this is not used but I think it could be used to do the same as I do without it - check commented function above*/
  int desired_spacing; // holds desired spaces
  int spacing; // just a variable used as a counter

 
  
  printf("Enter a name with 10 characters: "); scanf("%s" ,a);
  printf("Hello: %s\n",a);
  printf("\nWelcome to the tester!!!");
  printf("\n-----------------------\n");
  // Here can choose symbol
		printf("\nplease enter a symbol to use: "); scanf("%s",symbol);
		// Here can choose number of spaces
		printf("\nplease enter desired spacing: "); scanf("%d" ,&desired_spacing);
		// Here can choose width
		printf("\nplease enter desired width: "); scanf("%d", &width);
		// Here can choose heigth
		printf("\nplease enter desired heigth: "); scanf("%d", &height);
		
		  
 // HERE BELOW COMES THE TROUBLE
  
  for(spacing = 1; spacing <=desired_spacing; spacing++)  /* the actual blank --- HERE IS WHAT I WANT TO DO WITH ALL THE ROWS. I CAN ONLY ACHIEVE IT WITH THE FIRST ROW*/
      printf(" ");
 		 
			{
			 for(row=0; row<width; row++) // row
		           printf("%s",symbol);
			}
		 		
			  for(column=1; column<=height-1; column++) //column
						
				  {
					printf("	");
					printf("\n");
					  for(row=0; row<=width-1; row++) //row
				            printf("%s",symbol); 
										
																									
		}
		
		
		
  system("PAUSE");	
  return 0;
}

Edited by Jaan, 26 March 2009 - 08:37 AM.
Please use code tags when you're posting your codes!


#2
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Please remember to code tags to maintain indentation - help us to help you! ;)

The first row you draw is fine (as you noted) but the others are drawn in a different way that doesn't add any spacing. If you look at your code for the first row:

for(spacing = 1; spacing <= desired_spacing; spacing++)
{
    printf(" ");
}

for(row=0; row < width; row++) // row
{
    printf("%s", symbol);
}

You can see that is prints desired_spacing number of spaces and then width number of symbols. However, inside your loop to draw the other lines, you only draw width number of symbols:

printf(" \n");
for(row=0; row <= width-1; row++) //row
{
    printf("%s",symbol);
}

This code should draw the line with padding, like done above. Can you see how to proceed?

#3
bluetxxth

bluetxxth

    Newbie

  • Members
  • Pip
  • 3 posts
Hi Python,

Thank you much for the answer!! it was very helpful. I re-wrote the program and it works perfectly!! I will continue building on this program to add indentation up and down. I had an exam question which I failed back in the days because if this issue so I hope it is okay I keep asking questions surrounding this problem.

By the way excuse me for not coding the tags the first time I am new to this forum and I did not know how to. I presume the option "wrap around php tags around the text" is the correct one...

Here is the final version just in case there is another beginner wanting to know how to do this: ( testing tags for indentation ;))



  for(spacing = 1; spacing <=desired_spacing; spacing++) /* the actual blank --- HERE IS WHAT I WANT TO DO WITH ALL THE ROWS. I CAN ONLY ACHIEVE IT WITH THE FIRST ROW*/

 		 printf(" ");

 		 

			{

				

			 for(row=0; row<width; row++) // row

				 printf("%s",symbol);

					}

		 		

						for(column=1; column<=height-1; column++) //column

						

				  {

						 		printf("	");

							   printf("\n");

							    

											for(spacing = 1; spacing <= desired_spacing; spacing++) // added this for it to work

			          printf(" ");// added this for it to work

      							 for(row=0; row<=width-1; row++) //row

											    printf("%s",symbol); 

											

										

																									

		}


#4
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
I'm glad you got it working.

And good to see you using tags. :) The indentation looks a little clunky though, it might be worth reading Wikipedia's article on K&R indentation. Try modifying your program to follow those conventions - it adds clarity.

Any way, congrats. :)

#5
bluetxxth

bluetxxth

    Newbie

  • Members
  • Pip
  • 3 posts
Hi again,

I have thought to post here because I am trying to do another assignment which looks almost the same with the exception that now the square is supposed to be hollow. That is, only the contour should be showing.

I am having problems with the first column to indent.

// here is where the action starts

  

  for(spacing = 1; spacing <=desired_spacing; spacing++) // blank for the first row 

		 printf(" ");

		 {	

	    for(row=0; row<width; row++) // this loop builds the first row

      printf("%s",symbol);

		 }

											

			// THIS IS WHERE THE PROBLEM IS THE COLUMN DOES NOT INDENT								

		for(spacing = 1; spacing <=desired_spacing; spacing++) // blank for the first row 

		 printf(" ");

	  {

     for(column=0; column<=height; column++) // this loop builds the first colummn

      printf("\n%s",symbol);

   }															

														

		

	 for(spacing = 1; spacing <=desired_spacing; spacing++) // blank for the first row 

		 printf(" ");

		 {	

     for(row=0; row<width; row++) // this loop builds the first row

					 printf("%s",symbol);

			}	


it seems that this method of indentation does not work column-wise.

Any suggestion?



Cheers,

bluetxxth

PS. By the way I am trying to indent better I actually read that wiki article. However it happens that bloodshed, which is what I use, does not let me configure the tab size. Even if I set it to one space it does like 4 or 5 and the above tabbing does not correspond with what I wanted. Any suggestions for that too would be appreciated.