the problem set asks us to create a half pyramid using hashes. Here is a link to an image of how it should look-
I get the idea and have written the program until printing the spaces (which I have replaced by "_" just so that I can test the first half of it.
However, when I try to run my program, it doesn't go beyond the do-while loop. In other words, it keeps asking me for the height of the pyramid and does not seem to run the for loop at all. I've tried multiple approaches but this problem seems to persist.
Any help would be appreciated!
Below is my code-
# include <cs50.h>
# include <stdio.h>
int main(void)
{
int height;
do
{
printf("Enter the height of the pyramid: ");
height = GetInt();
}
while (height > 0 || height < 24);
for (int rows = 1; rows <= height, rows++)
{
for (int spaces = height - rows; spaces > 0; spaces--)
{
printf("_");
}
}
return 0;
}
Running this program yields the following output-
Enter the height of the pyramid: 11
Enter the height of the pyramid: 1231
Enter the height of the pyramid: aawfaf
Retry: 12
Enter the height of the pyramid:
Your do/while loop condition is incorrect - change:
do {
...
} while (height > 0 || height < 24);
to either:
do {
...
} while (height <= 0 || height >= 24);
or:
do {
...
} while (!(height > 0 && height < 24));
(whichever you consider to be more readable/intuitive).
is this more simple
for(int i=0;i<8;i++) {
for(int j=0;j < (8-i); j++)
{
System.out.print(" ");
}
for(int k=0;k<=(i+1);k++)
{
System.out.print("#");
}
System.out.println();
}
Related
I have started the CS50x problem set 1; however my programme seems to stop after I have input the the number for height.
I.e. the programme will ask for a number between 8 and 23 (repetitively until it gets a correct input) and once I do so the code stops.
What have I done wrong here?
Below is the code I have written. All help would be greatly appreciated as I have looked at various questions on here S.E. but none addressing this problem.
include stdio.h
include cs50.h
int main (void)
{
printf("ok lets make a pyramid :)\n");
// height = x
int x;
// spaces = z
int z;
// hashes = a
int a;
// Get correct number for height
do
{
printf("Give me a positive number between 8 and 23 for the height of the pyramid\n");
x = GetInt();
}
while (x < 8 || x > 23);
// Build the phantom pyramid
for (int q = 0; q == x; q++)
{
// Spaces
for (z = x - 1; z == 0 ; z--)
{
printf(" ");
}
// Hashtags
for (a = 1; a == q; a++)
{
printf("#\n");
}
}}
Aside from that your #include syntax is wrong (#include <stdio.h>), your fundamental problem here, and the reason that your program exits without printing anything, is that the main for loop is never entered. C for loops are executed while the controlling expression is true, not until is is true. You have:
for (int q = 0; q == x; q++){ ... }
Since q == x evaluates to 0 (false), because q is 0 and x is between 8 and 23, this loop never executes, and the program exits. Each of your loops has this problem. You can fix it:
for (int q = 0; q < x; q++)
{
// Spaces
for (z = x - 1; z > 0 ; z--)
{
printf(" ");
}
// Hashtags
for (a = 0; a <= q; a++)
{
printf("#");
}
printf("\n");
}
Here, note that in the first pass through the loop, q is 0, so a must start at 0 to print a single hash on the first line. Also, the newline character is not printed until the loop is finished printing the line. These changes give this output for a height of 8:
#
##
###
####
#####
######
#######
########
I am not sure if this is the output that you want. The spacing on the left is tied to the height of the pyramid. If you want the pyramid steps on the left, you can change the associated for statement:
for (z = x - 1; z > q ; z--)
But I was under the impression that the Mario Pyramid had the steps on the left, and two hashes in the top line. You could modify your loop to do this, but here is a different loop. You don't need the variable a, and rather than thinking of z as "spaces", think of it as representing the line-position:
for (int q = x; q > 0; q--) {
// print spaces at beginning of line
for (z = 1; z < q; z++) {
printf(" ");
}
// print hashes at end of line
for ( ; z < x + 2; z++) {
printf("#");
}
// print newline when finished printing line
printf("\n");
}
The new loop gives this output for a height of 8:
##
###
####
#####
######
#######
########
#########
#include <stdio.h>
main()
{
int n;
int x;
int h=24;
int s=9;
scanf("%d",&n);
while (n>0)
{
n=n-1;
scanf("%d",&x);
while (x<=9)
{
if (x==1)
printf("2\n");
if (x==2)
printf ("3\n");
if (x==3)
printf ("5\n");
if (x==4)
printf("7\n");
if (x==5)
printf("11\n");
if (x==6)
printf("13\n");
if (x==7)
printf("17\n");
if (x==8)
printf("19\n");
if (x==9)
printf("23\n");
break;
}
while (23<h<542 && x>9)
{
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 )
{
s=s+1;
if (x==s)
printf("%d\n",h);
}
}
}
}
The question for the code is to enter n which will be the number of the following inputs. Each input must give the xth prime number.
example:
input:
3
,4
,20
,50
output:
7
,71
,229.
x can be between 1 and 100. (the first 100th prime numbers)
Now my problem is with x>9.after entering one value for it, it won't accept anymore values for x.
I would like to know why this happens and how to fix it.
I'm also very new to programming and haven't learned arrays yet.(I know scanfs aren't the best thing but that's all I've learned so far)
This line:
while (23<h<542 && x > 9)
is creating an infinite loop when x is greater than 9. 23 < h < 542 doesn't test whether h is between 23 and 542. That expression is equivalent to (23 < h) < 542. (23 < h) is 1 if 23 is less than h, which is always the case because h starts as 24 and the loop increases it. And 1 is always less than 542.
What you want is:
if (x > 9) {
while (h < 542)
...
}
}
There's no need to test x each time through the loop, because it never changes within the loop. And there's no need to test 23 < h, because that's always true.
When you do need to check whether a variable is between two numbers, the way to do it is with 23 < h && h < 542.
Another problem is that when you enter multiple numbers higher than 9, you're not resetting h and s back to their initial values before the while loop that looks for higher primes. You should initialize those values right before the loop, not just at the top of the program.
You can also make use of cascaded if/else if/else so you don't need to test x at the end (or you could use switch/case).
Here's the full rewritten program:
#include <stdio.h>
main() {
int n;
int x;
scanf("%d",&n);
while (n>0) {
n=n-1;
scanf("%d",&x);
if (x==1) {
printf("2\n");
} else if (x==2) {
printf ("3\n");
} else if (x==3) {
printf ("5\n");
} else if (x==4) {
printf("7\n");
} else if (x==5) {
printf("11\n");
} else if (x==6) {
printf("13\n");
} else if (x==7) {
printf("17\n");
} else if (x==8) {
printf("19\n");
} else if (x==9) {
printf("23\n");
} else {
int h = 24;
int s = 9;
while (h<542) {
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 ) {
s=s+1;
if (x==s) {
printf("%d\n",h);
}
}
}
}
}
}
DEMO
I`m am doing a nested loop problem and I was wondering how I could make the numbers after they reach a certain number start to decrease. The things I have tried start an infinite loop in the console. Here is an Example of the output wanted.
1
222
33333
4444444
555555555
4444444
33333
222
1
here is my code :
public class DisplayPattern {
public static void main(String[] args) {
int odd = 1;
int numbOfSpaces = 4;
for (int i = 1; i <= 9; i++) {
for (int j = numbOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= odd; j++) {
System.out.print(i);
}
System.out.println();
if(i < 5) {
odd = odd + 2;
numbOfSpaces = numbOfSpaces - 1;
}
else {
odd = odd -2;
numbOfSpaces = numbOfSpaces + 1;
}
}
}
}
Here is the output I am getting:
1
222
33333
4444444
555555555
6666666
77777
888
9
The easiest way to do this with you code (meaning least amount of mods) is to add a variable NumberToPrint = 1; next to the other declared variables and instead of printing i you print NumberToPrint.
Next you have to increment and decrement the number
if(i < 5) {
NumberToPrint++;
//the other stuff still goes here
...
}
else {
NumberToPrint--;
//the other stuff still goes here
...
Having said that, this is not the right approach. Try to think of a way to make this generic and parametrize everything including the maximum number to go to. See how you can fix you code to work with numbers that have 2 digits or 3... so one.
So for my assignment I have to take the inputs of length and width and print out patterns of "*" based on the inputs. The minimum height is 7 and only goes up by odd integers and width is any multiple of 6.
The basic format of the output using a height of 7 and width of 12:
************
************
*** ***
*** ***
*** ***
************
************
So basically the first and last 2 lines are straight through the entire width, with the odd numbered rows containing 3 asterisks followed by 3 spaces, until it reaches the end of the width. The even numbered rows start off with 3 spaces.
I've figured out how to print the first two lines using the following code:
do
{
printf("*");
++i;
}while(i<width);
printf("\n");
do
{
printf("*");
++j;
}while(j<=width);
printf("\n");
But for the life of me, I cannot come up with the correct way to use basic nested loops to print out the inside pattern. I asked a programmer friend who is unfamiliar with C but wrote up a basic program in Java. I don't know Java and have tried to translate it but notice some big discrepancies in the logic between the two languages that is causing me headaches. Here is his code:
// LOGGING
var consoleLine = "<p class=\"console-line\"></p>";
console = {
log: function (text) {
$("#console-log").append($(consoleLine).html(text));
}
};
// PATTERN PARAMETERS
var rows = 6;
var cols = 7;
// hard code a space so html respects it
var space = " "
console.log("cols: " + cols + " rows: " + rows);
for (y = 0; y < rows; ++y) {
var line = "";
for (x = 0; x < cols; ++x) {
// First two and last two rows do not have patterns and just print filled
if (y == 0 || y == 1 || y == rows - 1 || y == rows - 2) {
line += "*";
} else {
if (y % 2 == 0) {
// Even row
line += x % 6 < 3 ? "*" : space;
} else {
// Odd row
line += x % 6 >= 3 ? "*" : space;
}
}
}
console.log(line);
}
Please help me or point me in the right direction!! I've searched online but can't seem to find a solution that's worked yet!
Edit- forgot to mention that all "printf" uses can only print one character at a time... Such as a single *
Edit edit- I GOT IT WORKING!!!! Thank you all so, so much for your input and guidance! Here's what I have that is working perfectly:
for (y = 0; y < height; ++y)
{
printf("\n");
for (x = 0; x < width; ++x)
{
// First two and last two rows do not have patterns and just print filled lines
if (y == 0 || y == 1 || y == height - 1 || y == height - 2)
{
printf("*");
}
else
{
if (y % 2 == 0)
{
if(x%6<3)
{
printf("*");
}
else
{
printf(" ");
}
} else {
// Odd row
if(x%6>=3)
{
printf("*");
}
else
{
printf(" ");
}
}
}
}
printf("\n");
Write a function with 3 arguments n,a,b that prints n groups of 3 of each argument a and b alternately. You can call this function to print the 4 different kinds of lines. You can make a loop to print the middle section repeatedly. Have fun!
A simpler alternative:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int row, col, height = atoi(argv[1]), cols = atoi(argv[2]);
for (row = 0; row < height; row++) {
for (col = 0; col < cols; col++) {
putchar(row < 2 || row >= height - 2 ||
col % 6 / 3 == row % 2 ? '*' : ' ');
}
putchar('\n');
}
}
I'm trying to get the following output from the given array
Apples 200 Grapes 900 Bananas Out of stock
Grapefruits 2 Blueberries 100 Orangess Coming soon
Pears 10000
Here's what I came up so far (feels like I'm overdoing it), however, I'm still missing something when padding the columns. I'm open to any suggestions on how to approach this.
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
char *fruits[][2] = {
{"Apples", "200"},
{"Grapes", "900"},
{"Bananas", "Out of stock"},
{"Grapefruits", "2"},
{"Blueberries", "100"},
{"Oranges", "Coming soon"},
{"Pears", "10000"},
};
int get_max (int j, int y) {
int n = ARRAY_SIZE(fruits), width = 0, i;
for (i = 0; i < n; i++) {
if (i % j == 0 && strlen(fruits[i][y]) > width) {
width = strlen(fruits[i][y]);
}
}
return width;
}
int main(void) {
int n = ARRAY_SIZE(fruits), i, j;
for (i = 0, j = 1; i < n; i++) {
if (i > 0 && i % 3 == 0) {
printf("\n"); j++;
}
printf("%-*s ", get_max(j, 0), fruits[i][0]);
printf("%-*s ", get_max(j, 1), fruits[i][1]);
}
printf("\n");
return 0;
}
Current output:
Apples 200 Grapes 900 Bananas Out of stock
Grapefruits 2 Blueberries 100 Oranges Coming soon
Pears 10000
You are computing widths wrong. In essence, you want to be able to compute the width of a particular column. Thus, in your get_max function, you should be able to specify a column. We can then pick out the elements from the list based on whether their index mod 3 is equal to the column. This can be accomplished as such:
int get_max (int column, int y) {
...
if (i % 3 == column /* <- change */ && strlen(fruits[i][y]) > width) {
...
}
Then in your main loop, you want to choose the widths of the columns based on what column you are currently in. You can do that by taking the index mod 3:
for (i = 0, j = 1; i < n; i++) {
...
printf("%-*s ", get_max(i % 3 /* change */, 0), fruits[i][0]);
printf("%-*s ", get_max(i % 3 /* change */, 1), fruits[i][1]);
}
This should work as you expect.
I dint try understanding your logic but i think you can space the data using tab with "\t":
printf("%s \t %d","banana", 200);