...

View Full Version : Creating a Box in C



kinggrunt
10-31-2011, 03:56 PM
Question:

Create a program that creates a user defined square of X

If the user specifies they would like square to be 5 in width and 5 in height then the following output would be displayed:

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

5 in width x 5 in height.

I have to use a nested loop, and it has to be basic,

printf
scanf

for

etc.

Anyone can help?

sumit1
10-31-2011, 04:20 PM
I think Solution For your problem is here..
solution removed

oracleguy
10-31-2011, 04:40 PM
I think Solution For your problem is here..

Please don't just give away the answers to homework questions. The poster might get the question done but they won't learn anything and it will come back to get them when they have to take a test. See our homework policy: http://www.codingforums.com/showthread.php?t=53446


As to the original question, break the problem into pieces and tackle each one separately. You first need to ask the user for the width and then the height. Then you need to write the outer for loop and then the inner for loop. Then inside of the inner loop print an 'X'.

kinggrunt
10-31-2011, 04:56 PM
This is for my own knowledge, I have completed my tutorial but I want to add a little extra, my code so far is the following: But I cant figure out how to instead of just saying print X, how I can the user to pick a letter, so if they wanted to change X to I, it would do
iiiiii
iiiiii
iiiii
iiiii

Please see my coding so far:

#include <stdio.h>

#include <stdlib.h>

void main(void)
{
//setting int's

int width=0;
int height=0;

//user input

printf("What width would you like the box to be? \n", width);
scanf("%i", &width);
printf("What height would you like the box to be? \n", height);
scanf("%i", &height);

printf("Box Result: \n\n"); //output will be displayed under here.
//loop

for (int loop=0;loop < height; loop++)
{
for(int nestloop=0;nestloop < width ; nestloop++)
{
printf("X");
}
printf("\n");
}

system("pause");
}

oracleguy
10-31-2011, 07:08 PM
Good work so far. In order to make the character of the user's choosing, you would need to add another prompt and read in a single character. Then instead of just printing the X you would print your char variable.

kinggrunt
11-01-2011, 11:26 AM
I understand the theory behind it, but can you check my coding to see if Im on the same wave lenght? The following doesnt work, and Im wondering why, I was taught that for a Char, you use %c, and set inizalize variable its char [] = name, I think.?

#include <stdio.h>

#include <stdlib.h>

void main(void)
{
//setting int's

int width=0;
int height=0;
char [1] box;

//user input

printf("What width would you like the box to be? \n", width);
scanf("%i", &width);
printf("What height would you like the box to be? \n", height);
scanf("%i", &height);
printf("What Letter would you like the box to be? \n", box);
scanf("%c", &box);

printf("Box Result: \n\n"); //output will be displayed under here.
//loop

for (int loop=0;loop < height; loop++)
{
for(int nestloop=0;nestloop < width ; nestloop++)
{
printf("%c");}

printf("\n");
}

system("pause");
}

bobleny
11-01-2011, 03:50 PM
For future posts, if you wouldn't mind, please put your code in code tags. For example:


&#91;CODE&#93; Your Code Goes Between These Tags &#91;/CODE&#93;





The following doesnt work, and Im wondering why,[...]

Usually the compiler will tell you why. What are the errors you are getting?



I was taught that for a Char, you use %c, and set inizalize variable its char [] = name


Yes, the correct formating character to use for a value of type Char is %c. You may wish to read over this reference, it will help you fix a few errors in your printf functions.
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

The correct syntax for declaring an array in C is:


Type name[Length];
OR
Type name[] = {value1, value2, value3, (etc...)};


So, to declare a character array you would use:

Char box[1];

However, a character array is not the same as a character. For your program you are going to want to use a simple character variable.

Variables in C are declared like this:


Type name;
OR
Type name = value;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum