I'm working on Preset 1 for CS50, Mario More Comfortable. I've been trying to compile the below code, but I'm receiving an 'error: expected expression' message. I've checked the spelling, semicolons, brackets, and parentheses, but everything seems in order. Thoughts?
Code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// declare h
int h;
// get integer (user input) between 1 and 8, inclusive
do
{
h = get_int("Height: \n");
// if integer is greater than 8 or less than 1, prompt the user again until they enter a valid input
}while(h > 8 || h < 1);
// outer loop uses i to print a row of bricks h times
for (int i = -1; i < h; i++)
{
// inner loop uses j to print a column h times
for (int j = 0; j <= h; j++)
{
// e.g. if 8 + 8 > (8 - 1), print hash; else print blank: right-aligns
if (i + j > (h - 1))
{
printf("#");
}
printf(" ");
// e.g. if 8 + 8 < (8 - 1), print hash; else print blank: left-aligns
else if (i + j < (h - 1))
{
printf("#");
}
}
printf("\n");
}
}
Error message:
error: expected expression
else (i + j < (h - 1))
The expected results of this exercise:
# #
## ##
### ###
#### ####
##### #####
###### ######
####### #######
######## ########
You're having error because of the else if
The if statement is offering to computer to check a condition and proceed with a statement inside the curly braces {}. When you exit from these curly braces and write another statement or expression you basicly leaving behind that condition checking. After that if you want to check a condition, you should use if again. You can use else if right after if statement to check if another conditions is valued.
h = 5;
x = 3;
if (h < x)
printf("h is less than x\n");
x = 10;
if (h < x)
printf("h is less than x\n");
else if (h > x)
printf("h is bigger than x\n);
else
printf("h is equal to x\n");
the program will check the first if condition than evaluate the expression x = 10 then keep reading the next lines and check the second if condition with the new values, then check the else if and later else.
after I've fixed this I've checked your code and it doesn't give the pyramid but square print. If you haven't solved this problem set try to add printf(" "); to print spaces to create pyramid shape. Logic is to decrement the size of spaces while incrementing the size of #'s. If you change your for loop conditions you can achieve the results you want. While you are decrementing the first loop you can increment the nested loop. First loop will print a downward space pyramid while second loop will print a upward # pyramid. Hope make sense.
Related
I have a string like this :
h12pw3Bb4
I want decompressed to :
hhhhhhhhhhhhpwwwBbbbb
for numbers less than 10 i wrote this code but it isn't work for numbers greater than 10
for(int j = 0;j< strlen(txt);j++){
if(isdigit(txt[j])){
int x = txt[j];
x = x - 49;
while(x > 0){
printf("%c" , txt[j-1]);
x--;
}
}else{
printf("%c" , txt[j]);
}
}
See h12 implies print 'h' 12 times right. But you are reading character by character then when '1' is read 1 h will be print and next when 2 is read 2 h's are printed. So based on your code you are not going to achieve what you desire. You need to change it.
So, I think this should help:
int st = 0;
for(int j = 0;j< strlen(txt);) {
int x = 0;
while(j < strlen(txt) && isdigit(txt[j])) {
x = (x * 10) + (txt[j] - '0');
j++;
}
x--;
while(x > 0){
printf("%c" , txt[st]);
x--;
}
if (j < strlen(txt))
printf("%c" , txt[j]);
st = j;
j++;
}
Assumptions :
The first number must be a letter.
Please change them accordingly to your requirements.
Right, with multi-digit numbers you need several iterations of the loop before knowing the value of x.
So personally what I would do is this:
define and initialize two variables int x and char c outside of the loop
in the if block: calculate the value of x (multiplying it by 10 and adding txt[j] - 49 ?)
in the else block: print c x times, reset x to 0, set c to txt[j]
I am working on project euler #14 :
Question:
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
Problem:
When I run this code, the first for loop runs as expected. But then the variable num seems to not increment in the for loop and stays on 2 (which isn't even in the perameters of num I gave to start out with), giving the output:
2
0
1
1
repeating over and over again. Not sure why this is happening and can't find anything online.
Code:
#include <stdio.h>
int main() {
int maxcount = 0;
for (int num = 5; num < 2000000; num++) {
printf("%d\n0\n\n", num);
int count = 0;
while (num >= 1) {
count++;
if (num == 2) {
num = 1;
printf("1\n%d\n\n", count);
}
if (num > 1) {
if (num % 2 == 0) {
num = num / 2;
printf("%d\n%d\n\n", num, count);
}
else {
num = (3 * num) + 1;
printf("%d\n%d\n\n", num, count);
}
}
if (num == 1) {
break;
}
}
if (count > maxcount) {
maxcount = count;
}
}
printf("%d", maxcount);
return 0;
}
You need to introduce a new variable. You are doing:
for (int num = 5; num < 2000000; num++) {
/* collatz stuff, which modifies num, and eventually
causes it to become 1
*/
}
so when the num++ portion of the for loop runs, num becomes 2.
What you want is
for (int start = 5 ; start < 2000000 ; start++) {
int num = start;
/* collatz stuff, which modifies num */
}
since the variable start is never modified by the loop body, it will keep going up in sequence by 1, without interference.
In the while loop, you change the value of the loop variable (num).
You start with num = 5, and the inner while loop then follows Collatz's rules and gets to 1, at which point the while-loop ends.
Then the for-loop ends, and num is incremented to 2 by num++, and the next iteration of the loop begins.
In the second iteration, the while-loop immediately sees num == 2, so it sets num = 1, and the while loop ends. Then the for-loop ends, incrementing num to 2, and the cycle continues forever.
As others have said, you just need to leave the loop variable alone and use a different variable in the inner loop.
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:
##
###
####
#####
######
#######
########
#########
i have an assignment to do a right angle triangle with an odd number (user gives us what number) and to make it to the right
example if user gives us 5:
*****
***
*
i can use only stdio.h
where am i wrong ?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(){
int x,i,y=0;
printf("select odd number:\n");
scanf("%d", &x);
for (i=x ; (i <= x)&&(i>0); i=i-2){
for (y; y > 0; y=y-2){
printf("_");
}
for (x; x >= 0; x-=2){
printf("*");
}
x = x - 2;
printf("\n");
}
return 0;
}
Your outer loop is testing against x but within that you are again using x as a loop variable and when that loop ends you are setting x less than 0 with x = x - 2. So the outer loop condition isn't going to work.
I did this in c :
#include<stdio.h>
int main (void)
{
int n,i;
scanf("%d", &n);
for(i=2;i<=n;i=i+2)
{
if((i*i)%2==0 && (i*i)<= n)
printf("%d \n",(i*i));
}
return 0;
}
What would be a better/faster approach to tackle this problem?
Let me illustrate not only a fast solution, but also how to derive it. Start with a fast way of listing all squares and work from there (pseudocode):
max = n*n
i = 1
d = 3
while i < max:
print i
i += d
d += 2
So, starting from 4 and listing only even squares:
max = n*n
i = 4
d = 5
while i < max:
print i
i += d
d += 2
i += d
d += 2
Now we can shorten that mess on the end of the while loop:
max = n*n
i = 4
d = 5
while i < max:
print i
i += 2 + 2*d
d += 4
Note that we are constantly using 2*d, so it's better to just keep calculating that:
max = n*n
i = 4
d = 10
while i < max:
print i
i += 2 + d
d += 8
Now note that we are constantly adding 2 + d, so we can do better by incorporating this into d:
max = n*n
i = 4
d = 12
while i < max:
print i
i += d
d += 8
Blazing fast. It only takes two additions to calculate each square.
I like your solution. The only suggestions I would make would be:
Put the (i*i)<=n as the middle clause of your for loop, then it's checked earlier and you break out of the loop sooner.
You don't need to check and see if (i*i)%2==0, since 'i' is always positive and a positive squared is always positive.
With those two changes in mind you can get rid of the if statement in your for loop and just print.
Square of even is even. So, you really do not need to check it again. Following is the code, I would suggest:
for (i = 2; i*i <= n; i+=2)
printf ("%d\t", i*i);
The largest value for i in your loop should be the floor of the square root of n.
The reason is that the square of any i (integer) larger than this will be greater than n. So, if you make this change, you don't need to check that i*i <= n.
Also, as others have pointed out, there is no point in checking that i*i is even since the square of all even numbers is even.
And you are right in ignoring odd i since for any odd i, i*i is odd.
Your code with the aforementioned changes follows:
#include "stdio.h"
#include "math.h"
int main ()
{
int n,i;
scanf("%d", &n);
for( i = 2; i <= (int)floor(sqrt(n)); i = i+2 ) {
printf("%d \n",(i*i));
}
return 0;
}