cs50

How to reuse(loop) key in vigenere cipherkey cs50 pset2

旧巷老猫 提交于 2019-12-12 04:55:36
问题 I was making a program for Vigenere cipher. I made the program print the cipher text successfully. But, I can't loop the key. so if my key was 'abc' and my plain text was hello, it should print 'hfnlp' not 'hfn'. #include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h> int main(int argc, string argv[]) { if(argc != 2) { printf("\aError\n"); return 1; } else { string a = argv[1]; // converts argv[1] printf("plaintext: "); string b = get_string(); // takes the plaintext

C - cs50.h GetString error

五迷三道 提交于 2019-12-11 19:01:44
问题 Hello I am completely new to the world of programming an I am attempting to take Harvard's CS50 course online. While making my "Hello World" program, I downloaded 'cs50.h' to define GetString and string (at least I think). So this is the code I wrote: file.c: #include "cs50.h" #include <stdio.h> int main(int argc, string argv[]) { string name; printf("Enter your name: "); name = GetString(); printf("Hello, %s\n", name); } However, whenever I try to make file , this happens: cc file.c -o file

How to solve error: expected identifier or '('

帅比萌擦擦* 提交于 2019-12-11 10:58:23
问题 I've got a problem with something I'm programming. I get this error over and over; jharvard@appliance (~/Dropbox/pset1): make mario clang -ggdb3 -O0 -std=c99 -Wall -Werror mario.c -lcs50 -lm -o mario mario.c:23:5: error: expected identifier or '(' do ^ mario.c:32:1: error: expected identifier or '(' do ^ 2 errors generated. I searched all over the internet but couldn't find the problem.. removing the ; after int main(void) didn't help This is my code: #include <stdio.h> #include <cs50.h>

cash division error in C with floating points

谁都会走 提交于 2019-12-11 05:58:48
问题 Im currently working on the cs50 "cash" problem: estimating the amount of coins needed to pay some change. ex: $0.41 owed = 1 quarters, 1 dime, 1 nickel, 1 penny. however, when estimating the amount of coins needed i end up being off with the pennies i believe is due to error on my part as it always seems to be off by one or 2 coins (pennies). I have included multiple printf statement to try and track what i could be doing but i can't seem to figure out why the division isn't working.

Integer overflow in greedy coin counting

拟墨画扇 提交于 2019-12-11 04:14:34
问题 #include <stdio.h> #include <cs50.h> #include <math.h> int main (void) { printf ("Enter amount: "); float amount = GetFloat(); int coins = 0; while (amount != 0) { if (fmod(amount, 0.25) == 0) { amount = amount - 0.25; coins += 1; } else if (fmod(amount, 0.10) == 0) { amount = amount - 0.10; coins += 1; } else if (fmod(amount, 0.05) == 0) { amount = amount - 0.05; coins += 1; } else { amount = amount - 0.01; coins += 1; } } printf ("Coins : %d\n", coins); } I'm trying to implement a small

How to make the pyramid (CS50 Mario Program) formed by this code to be right aligned?

百般思念 提交于 2019-12-10 12:36:34
问题 Please help me create the pyramid with height "n" print correctly using hashes and spaces that is right-aligned. I have posted the code itself below. The program correctly asks for the user input, but doesn't build the pyramid right-aligned. If anyone can fix this, please help. #include <stdio.h> #include <cs50.h> int main(void) { int i=0; int n=0; do { printf("Height:"); n=GetInt(); } while (n<0 || n>23); for (i=0; i<n; i++) { printf(" "); for (int x=0; x<i+2; x++) { printf("#"); } printf("

Small bug in my short C code. Why?

北慕城南 提交于 2019-12-09 03:46:38
问题 I can't figure out why this works for 90% of the inputs, but not the others. It is meant to tell you how many coins you would get back in change. Most test amounts work fine, but if you enter 4.20 (or $4.20), it returns 23 coins... it should be 18 coins (16 quarters and 2 nickels). Where is the bug? Here is my code: #include <stdio.h> #include <cs50.h> int main(void){ float change = 0.00; printf("How much change is owed? "); change = GetFloat(); float quarters = change/.25; change-= (int

Why my program works correctly for some tests and not for others? [duplicate]

纵然是瞬间 提交于 2019-12-08 14:11:03
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 2 years ago . This is the output of my tests: :) greedy exists :) greedy compiles :( input of 0.41 yields output of 4 expected "4\n", not "3\n" :( input of 0.01 yields output of 1 expected "1\n", not "0\n" :) input of 0.15 yields output of 2 :) input of 1.6 yields output of 7 :) input of 23 yields output of 92 :) input of 4.2 yields output of 18 :) rejects a negative input like -.1 :) rejects a non

C string from GetString() when strlen produces segmentation fault [closed]

此生再无相见时 提交于 2019-12-08 13:20:23
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I am running a program in C. When I run the program I get a segmentation fault error. IN gdb when I backtrace it tells me Program received signal SIGSEGV

Learning C, need some assistance with “Greedy” CS50 solution

一曲冷凌霜 提交于 2019-12-07 20:08:12
问题 I am very new to C. I come from a python background. I would like to know where I went wrong with my code. I am doing the cs50 greedy problem. What is wrong with my code? It works with some numbers but others don't work. I am trying to get an input from the user asking how much change to give back, then calculate the minimum number of coins I can give back using only $.25, $.10, $.05, $.01 #include <cs50.h> #include <stdio.h> int main(void) { float n; do { n = get_float("How much change is