cs50

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

狂风中的少年 提交于 2019-12-06 05:39:35
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 owed?\n"); } while(n == EOF); int minimumamountofcoins = 0; if (n/.25 >=1){ do { n -= .25;

malloc vs array in C

此生再无相见时 提交于 2019-12-05 17:19:21
I am taking a MOOC course CS50 from Harvard. The last lecture I had was about memory allocation and pointers (two concepts which are absolutely new to me). What was taught is that malloc(10*sizeof(char)) allocates enough bytes on the heap to store 10 characters and returns a pointer to the first byte which can be saved in another variable as follows char *x = malloc(10*sizeof(char)) . To free the memory one would use free(x) . But there is another way to make a computer to reserve enough memory to store 10 characters i.e. char c[10] . Is in the code snippet above c also a pointer of type char*

Keep getting this compiling error [closed]

妖精的绣舞 提交于 2019-12-04 07:15:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . When I compile, I keep getting this error. mario.c:4:1: error: expected identifier or '(' I've tried changing things and then fixing them and changing other things and then fixing those but nothing seems to help. I'm new at this. Can anyone help? #include <stdio.h> #include <cs50.h> int main(void); { int n; do {

CS50 Greedy Need Advice

梦想与她 提交于 2019-12-04 06:54:38
问题 I am doing the cs50 problem set "Greedy". Basically asking the user for how much change is owed and then outputting the minimum amount of coins that can equal the inputed amount. It works perfectly, except when I input 4.2 it outputs 22 when it should output 18. #include <cs50.h> #include <stdio.h> int main(void) { float n; do { n = get_float("How much change is owed?\n"); } while(n == EOF); int cents = (int)(n * 100); int minimumamountofcoins = 0; if (cents/25 >= 1){ while (cents/25 >= 1) {

Why am i not able to print 47th fibonacci number correctly?

不想你离开。 提交于 2019-12-02 23:52:06
问题 I am using 64 bit operating system ,then also i am not able to print 46th fibonacci number correctly which is less than 4 billion. #include<cs50.h> #include<stdio.h> int main(void) { unsigned int n=50; int array[n]; array[0]=0; array[1]=1; printf("%i\n",array[0]); printf("%i\n",array[1]); for(int i=2;i<n;i++) { array[i]=array[i-1]+array[i-2]; printf("%i\n",array[i]); } 回答1: You have to use long long as your data type of the array. because You are going to store out-range numbers of the

Expression Result Unused Greedy Algorithm

北慕城南 提交于 2019-12-02 21:28:29
问题 I have run this program and get the error expression result unused. I may be doing something simple wrong, but I have spent the day trying to figure it out to no avail. Any help you can provide is greatly appreciated. #include <stdio.h> #include <cs50.h> int main() { int x, y = 0; printf("Enter the amount of change "); x = GetFloat() * 100; while (x != 0) { if (x >= 25) { x - 25; y = y + 1; } if (x >= 10 && x < 25) { x - 10; } y = y + 1; if (x >= 5 && x < 10) { x - 5; } y = y + 1; if (x >= 1

C - Rounding issues (CS50)

巧了我就是萌 提交于 2019-12-02 15:21:17
问题 I have been Googling this for days now and I am lost. So doing CS50 online and can't seem to get a handle on this rounding of numbers. My program is messing up multiplying floats like 2.10 with integers like 100 it would output 209.xxxxxxxx Now like I say I have read countless posts on that I should use ceilf and include but I am getting an error greedy.c:(.text+0x74): undefined reference to `ceilf' collect2: error: ld returned 1 exit status make: *** [greedy] Error 1 adam@beethoven:~

Why am i not able to print 47th fibonacci number correctly?

给你一囗甜甜゛ 提交于 2019-12-02 13:43:47
I am using 64 bit operating system ,then also i am not able to print 46th fibonacci number correctly which is less than 4 billion. #include<cs50.h> #include<stdio.h> int main(void) { unsigned int n=50; int array[n]; array[0]=0; array[1]=1; printf("%i\n",array[0]); printf("%i\n",array[1]); for(int i=2;i<n;i++) { array[i]=array[i-1]+array[i-2]; printf("%i\n",array[i]); } You have to use long long as your data type of the array. because You are going to store out-range numbers of the integer range.(-2,147,483,648 to 2,147,483,647) And declaration of int i should be before the for loop. #include

Expression Result Unused Greedy Algorithm

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 11:59:19
I have run this program and get the error expression result unused. I may be doing something simple wrong, but I have spent the day trying to figure it out to no avail. Any help you can provide is greatly appreciated. #include <stdio.h> #include <cs50.h> int main() { int x, y = 0; printf("Enter the amount of change "); x = GetFloat() * 100; while (x != 0) { if (x >= 25) { x - 25; y = y + 1; } if (x >= 10 && x < 25) { x - 10; } y = y + 1; if (x >= 5 && x < 10) { x - 5; } y = y + 1; if (x >= 1 && x < 5) { x - 1; y= y + 1; } } printf("The number of coins neccessary is %d", y); } if (x >= 25) { x

C - Undefined symbols for architecture x86_64 when compiling on Mac OSX Lion

浪尽此生 提交于 2019-11-30 18:19:59
I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion. Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught. This is the source of name.c: #include <stdio.h> #include <cs50.h> int main(void) { printf("State your name:\n"); string name = GetString(); printf("O hai, %s!\n", name); return 0; } As you can see, it requires this library: https://manual.cs50.net/CS50_Library . Now, when I compile it, this happens: Undefined symbols for architecture x86_64: "_GetString",