What is wrong with my code? I'm currently getting these messages (sad faces are the errors):
:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
\ expected an exit code of 0, not output of " \n"
:( handles a height of 1 correctly
\ expected output, but not " \n# #\n"
:( handles a height of 2 correctly
\ expected output, but not " \n # #\n## ##\n"
:( handles a height of 23 correctly
\ expected output, but not " \n ..."
:) rejects a height of 24
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
The program seems to work fine other than being exactly the way CS50 is looking for.
#include <stdio.h>
#include <cs50.h>
int main(void) {
int height;
int i;
do {
printf("Height:");
height = get_int();
}while(height < 0 || height > 23);
int x = height;
int y = 0;
for (i = 0; i < height + 1; i++)
{
for (i = 0; i < x; i++)
{
printf("%s", " ");
}
for (i = 0; i < y; i++)
{
printf("#");
}
printf("%s", " ");
for (i = 0; i < y; i++)
{
printf("#");
}
printf("\n");
x = x - 1;
y = y + 1;
}
return 0;
}
Try including an if statement after the user inputs height. That will at least get you the proper result for a height of 0.
if (height == 0)
{
return 0;
}
Those /messages are key to fixing your code. You've got this.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int number;
do
{
number = get_int("Height:");
}
while (number < 1 || number > 8);
for (int i = 0; i < number; i++)
{
for (int j = number - i - 2; j >= 0; j--)
{
printf(" ");
}
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf(" ");
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf("\n");
}
}
Related
Basically i have a school task to make a C program that asks user for width input = n and makes the shape of a parallelogram/diamond.
Enter width: 4
/\
/ \
/ \
/ \
\ /
\ /
\ /
\/
This is my output:
Enter width: 4
/
/ \
/ \
/ \
\ /
\ /
\
This is my current code:
#include <stdio.h>
int main() {
int n, i, j, l;
printf("Enter width: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j++) {
printf(" ");
}
for (l = 1; l <= 2 * i - 1; l++) {
if (l == 1)
printf("/");
else if (l == (2 * i - 1))
printf("\\");
else
printf(" ");
}
printf("\n"); // novi red
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j >= i; j--) {
printf(" ");
}
for (l = 1; l <= 2 * i - 1; l++) {
if (l == 1)
printf("/");
else if (l == (2 * i - 1))
printf("\\");
else
printf(" ");
}
printf("\n"); // novi red
}
return 0;
}
But it does not print it like the one on the example output. I really tried to fix the problem but i just cant seem to make it work. I think the problem is somewhere in the second for loop just under the middle comment // novi red
Well, working and optimized code:
#include <stdio.h>
void printSpaces(int count);
int main() {
int n;
printf("Enter width: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printSpaces(n-i);
printf("/");
printSpaces(2*i-2);
printf("\\");
printf("\n"); // new line
}
for (int i = n; i >= 1; i--) {
printSpaces(n-i);
printf("\\");
printSpaces(2*i-2);
printf("/");
printf("\n"); // new line
}
return 0;
}
void printSpaces(int count){
for(int i = 0; i < count; i++){
printf(" ");
}
}
But don't just copy-paste it, read my explanation and tips, because you need to learn few things.
Firstly when you do something more times in your code, than extract that part of code to a function and than call it every time you need that code. This is the principle of functional programming and it makes your code much cleaner and save you a lot of time. In this case I created function to print spaces, because there were many for-loops just to print spaces.
The second main change I did in your code was your for-loop to print a line. I completely replaced it with my code and I will try to explain you why. When you think of your for-loop, it prints / at the first run, than prints spaces and at the end it prints one . And it doesn't really make sense when you do something only at the first run of the loop, because you can just write it before the loop and you get the same result. So instead of:
for(int i = 0; i<n; i++){
if(i==0){
//doSomething
}else{
//doSomethigElse
}
}
Do:
//doSomething
for(int i = 1; i<n; i++){
//doSomethigElse
}
This is much more readable, logical and also faster.
So when I did this correction to your code, I end up with:
for (int i = 1; i <= n; i++) {
printSpaces(n-i);
printf("/");
for (int j = 2; j <= 2 * i - 1; j++) {
printf(" ");
}
printf("\\");
printf("\n"); // new line
}
And because now the for-loop is just printing spaces, I called my printSpaces function instead:
for (int i = 1; i <= n; i++) {
printSpaces(n-i);
printf("/");
printSpaces(2*i-2);
printf("\\");
printf("\n"); // new line
}
But where was your mistake? Well, it was somewhere in the second condition of your if-else, but it doesn't matter that much. Much more important is to write clean and logical code, because than you can find errors and mistakes much more easily. Hope I helped you with your coding style.
in the cs50 project, we have to make a Mario hash pyramid then right align it to the left and I'm having a pretty hard time doing it and I hope someone could give me some advice
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int h = 0;
int l = 0;
int c = 0;
do {
h = get_int("Height: ");
} while (h > 8 || h < 1);
{
for (int w = 0; w < h; w++)
{
for (int j = -1; j < w; j++)
{
printf("#");
}
printf("\n");
}
}
}
output:
what is needed:
try to make two-loop one for the empty space you can also use - and second for #..
after the align become to left del the -
Before checking the code that I'll provide, I strongly advise you to do hard coding for every single line of pyramide. This is the only way you will see and understand the design pattern of the following algorithm. Then, feel free to check the following code snippet.
Here is the code that builds pyramid right-aligned.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height = 0;
do
{
height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");
}
while (height > 8 || height < 1);
for(int i = 0; i < height; i++)
{
for(int j = height - i; j > 1; j--)
{
printf(" "); // print empty spaces.
}
for(int k = 0; k <= i; k++)
{
printf("#"); // print hashes.
}
printf("\n"); // go to next line.
}
}
I want the user to input a height in range 2-8 inclusive if any other number is entered it should re-prompt user ... i have tried
int main(void)
{
int height;
do{
height = get_int("Enter The Height: ");
}
while (height<=1 && height<=9);
return height;
}
the program runs with no errors but even if I input 9 it does not re-prompt me..
You want to ask again as long as the height is outside of the desired range. So you want to ask again as long as the height is less than the minimum or greater than the maximum.
do {
...
} while (height < 2 || height > 8);
You were using && where you should have be using ||, and <= where you should have been using >=.
Your logic is incorrect. It should be while (!(2 <= height && height <= 8)).
For example when height == 9 in your original logic it ends up being false && true, which is false.
well you need the "OR" operator instead of "AND", to get in the loop you need one of the conditions should be different from the other ie., "< >".
#include <stdio.h>
int main(){
int i, j, k, h;
int a, b;
do
{
printf("Enter the heights: ");
scanf("%d", &h);
} while (h < 0 || h > 8);
for (i = 0; i <= h; i++)
{
for (k = h; k >= i; k--)
{
printf(" . ");
}
for (j = 0; j <= i; j++)
{
printf(" # ");
}
for (a = 0; a < 2; a++)
{
printf(" . ");
}
for (b = 0; b <= i; b++)
{
printf(" # ");
}
printf("\n");
}
}
I am trying to build a square/rectangle using "Xs" on a 1:1 scale with the length and width, but the logic seems to be not perfect
void draw (float x, float y) {
int i, j;
int length = (int)x + 0;
int width = (int)y + 0;
for (i = 1; i < length; i++) {
for (j = 1; j < width; j++) {
if (((i = 1) || (i = length)) && ((j = 1) || (j = width))) {
printf("x");
} else {
printf(" ");
}
}
printf("\n");
}
}
The problem is that the loop iterates endlessly printing x's everywhere. I'm expecting Xs to be printed out in either a square or rectangular shape (depending on the length or width).
I see 3 flaws in your logic.
you mix = (assignment) with == comparison.
This is why your loop never ends: you always reset i to 1 with your if (((i = 1) || (i = length)) &...
you're not going far enough with your variables:
if i < length, then you'll never have it reach length and print the bottom line of X's
you can't draw a rectangle because your && in the test (((i = 1) || (i = length)) && ((j = 1) || (j = width))) is too restrictive. It can't work if width different from length.
You must learn the logic by yourself using tutorials:
Here such questions will be discarded by sometime-sad people. But as a welcome, here it is (you can replace y in the second case by x, but I thought it would help you understand):
#include <stdio.h>
#include <string.h>
void draw (float x, float y)
{
int i,j;
int length = (int)x + 0;
int width = (int)y + 0;
for(i=1; i<=length; i++) {
for(j=1;j<=width;j++) {
if(((i==1)||(i==length))) {
printf("x");
} else {
if (((j==1)||(j==width))) {
printf("y");
} else {
printf(" ");
}
}
}
printf("\n");
}
}
I am attempting to create a diamond in c with the constraints of only 3 printfs and 3 n\t. this requires me to use loops. I know how to make an upside down triangle and a triangle but cant use that because there are too many printfs. i will attach my code so far. I am aware it does not make a diamond, and some awfully strange shape, but that it what i'm trying to work off and edit to make into a diamond, I just haven't been able to figure it out.
if (type_of_shape == 5)
{
for (i = 0; i < width; i++)
{
for (j = 0;j < ((width - 1) / 2) - i ||(width -1)/2 < i && j + (width-1)/2 < i; j++)
{
printf(" ");
}
for (k = 0;k<width && k < (j*2+1) ; k++)
{
printf("*");
}
printf("\n");
}
}
//int width = 5;
int row, col;
int spaces, stars;
int half, rate;
half = width / 2 + 1;
rate = 1;
for(row = 1; 0 < row && row <= half; row += rate) {
spaces = half - row;
stars = row * 2 -1;
printf("%*s", spaces, "");
for (col = 0; col < stars; col++)
printf("*");
printf("\n");
if(row == half)
rate = -rate;
}
I got it down to a single line which has a single loop, with a single printf statement.
It involved some tricky use of abs.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int w = 9;
for(int l=0; l < w; ++l) printf("%*.*s\n", abs(w/2 - l)+abs((2*l+1)-(2*l+1>w)*2*w), abs((2*l+1)-(2*l+1>w)*2*w), "**************");
return 0;
}
2 loops (one for, one while).
2 printf statements.
Note:
This works with odd Widths.
An even width produces a diamond with Width+1
My IDEOne code
int main(void)
{
int width = 9;
int layer;
width+=2;
for(layer=0; layer<width/2; ++layer)
{
printf("%*.*s\n", width/2+layer + 1,layer*2 + 1, "**************************");
}
layer--;
while (layer --> 0)
{
printf("%*.*s\n", width/2+layer + 1,layer*2 + 1, "**************************");
}
return 0;
}
Output
Success time: 0 memory: 2168 signal:0
*
***
*****
*******
*********
*******
*****
***
*
Here's a solution with no loops at all. (looping accomplished via recursion), and 3 printf statements:
#include <stdio.h>
void drawDiamond(int width, int stars)
{
static const char* txt = "*****************************";
if (stars == width) {
printf("%*.*s\n",width, width, txt);
return;
}
printf("%*.*s\n", (width+stars)/2, stars, txt);
drawDiamond(width, stars+2);
printf("%*.*s\n", (width+stars)/2, stars, txt);
}
int main(void)
{
drawDiamond(9, 1);
return 0;
}