-
Help writing a program
I need help writing a program. The program calls for a user to be asked a width and height to make a table of asterisks. The height can ONLY be 3 or 5, and the width can be between 4-40. The table must be out of asterisks. Here is the program I have so far, except i need to replace the word "width" with the desired width of asterisks...How can I do this?
Code:
"pe3.c" 23 lines, 228 characters
6 #include <stdio.h>
7
8 int main(void)
9
10 {
11
12 int height, width;
13
14 printf("Enter width:");
15 scanf("%d", &width);
16 printf("Enter height:");
17 scanf("%d", &height);
18
19 if (height == 3)
20 printf("%d\n", width);
21 printf("*%d*\n", width);
22 printf("*%d*\n", width);
23
24 return 0;
25
26 }
This is what the program is supposed to make:
Code:
Enter width: 20
Enter height: 3
********************
* *
* *
-
You'll need a loop. Do you know how to code a "for" loop?
-
The instructor said I would need a loop. Can you give me an example of what I need to do?At the rate I am going I will be here all night. What I am doing is wrong I think.
Code:
6 #include <stdio.h>
7
8 int main(void)
9
10 {
11
12 int height, width;
13
14 printf("Enter width:");
15 scanf("%d", &width);
16 printf("Enter height:");
17 scanf("%d", &height);
18
19 if (width == 4)
20 printf("****/n");
21 else if (width == 5)
22 printf("*****/n");
23 else if (width == 6)
24 printf("******/n");
25 else if (width == 7)
26 printf("*******/n");
27 else if (width == 8)
28 printf("********/n");
29 else if (width == 9)
30 printf("*********/n");
31 else if (width == 10)
32 printf("**********/n");
33 else if (width == 11)
34 printf("***********/n");
-
Loops would work something like this.
Code:
...
int i = 0;
int j = 0;
for(i = 0; i < height; i++) {
for(j = 0; j < width; j++) {
printf("*");
}
printf("\n");
}
...
Something along those lines should work.
-
I understand the for loop. My problem is trying to work with * rather than a number...Can I set an int such as width = * /
-
Well the number will tell your loop how many times to loop. Then inside the loop you can print your "*". This will print the "*" every iteration.
So when you get the users input, and you get the width, you lloop width amount of times and print "*".
-
Ok but wouldn't this leave more of a "filled box shape"? I need the inside to be empty so it looks like a table
The code you sent me should output this I believe
Code:
Enter width:10
Enter height:3
**********
**********
**********
Needed
Code:
Enter width: 10
Enter height: 3
**********
* *
* *
-
putc(' '); will print a space character
putc('*') - star cgharacter - you should mix them for the 2nd and futher lines
-
Here's a basic "for" loop. I'll write it, then I'll explain it.
Code:
int i ;
for ( i = 0 ; i < width ; i++ )
{
do-one-thing... ;
}
some-next-instruction ;
The code in RED is required for every "for" loop. (There is a shortcut to leave off the curly braces, but I won't get into that).
The "for" says to loop some number of times. The parens enclose the INITIALIZER, the CONDITION and the INCREMENT, in that order.
To start the loop, you set up an initializer. It is typical in a loop to control it with an INT. Here, variable i is the int. We set it with an initial values of 0. This is the most common thing to do.
The flow of execution is this:
1) the initializer is performed (this is only done once)
2) the condition is checked
3) if the condition is TRUE, then the code between the braces is executed. If the condition if false, then the loop is done, and control passes to the next instruction after the last curly brace.
4) If the code between the braces was executed because the condition was true, then the increment is performed. The process repeats again, starting with step 2 above.
So, let's take a simple example. Let's say you wanted to do the same thing (like print a '*') 5 times. You would initialize i to 0, you would set a condition for i to be less than 5 (because counting from 0 to 4 is "5 times"), and you would set an increment to bump i by one each loop.
Does that make sense?
This is just the very basics, but should be enough to get you going in the right direction.
Todd
EDIT - if not, hopefully the other posters who posted will have helped - who posted while I was writing this!! Man, you guys are fast!
-
Can I put whatever I want where the green text is? I need the width to be between 4 and 40.
So i == 4 < width < 40 ?
-
You should validate the width prior to entering the loop.
-
Ok thank you. Now I just have to work on where to put the printf(" "); at.
Thanks
Code:
6 #include <stdio.h>
7
8 int main(void)
9
10 {
11
12 int height, width;
13 int i = 0;
14 int j = 0;
15
16 printf("Enter width:");
17 scanf("%d", &width);
18 printf("Enter height:");
19 scanf("%d", &height);
20
21 for (i = 0; i < height; i++) {
22 for(j = 0; j < width; j++) {
23 printf("*");
24 }
25 printf("\n");
26 }
27
28 printf("PROGRAM ENDS\n");
29 return 0;
30
31 }
-
When you print the *, compare j and width to see where you are.
If i is 0, don't worry about the check, after that, if j is equal to 0 or j is equal to width you can print *, else you can print ' '.
-
Can anyone please show me where to put the printf(" ") in my code?
-
Make a flowchart, translate your code into the flowchart, insert appropriate spaces into flowchart, translate into code.