Tag: cs50

控制可能在C中达到非空函数的结束

我有这个错误,我无法调试它 helpers.c:136:1:错误:控制可能达到非空函数的结束 IDE在最后一行中说} 这是我的代码 // Calculates frequency (in Hz) of a note int frequency(string note) { // TODO if (strlen(note) == 2) { if (note[0] == ‘A’) { int freq = round(440 * (pow(2, (((int) note[1]) – 52)))); return freq; } else { if (note[0] == ‘A’) { if (note[1] == ‘#’) { int freq […]

继续收到此编译错误

当我编译时,我不断收到此错误。 mario.c:4:1: error: expected identifier or ‘(‘ 我已经尝试过更换东西,然后修改它们并更改其他东西,然后修复它们,但似乎没有任何帮助。 我是新来的。 有人可以帮忙吗? #include #include int main(void); { int n; do { n = GetInt(); } while (n=<0); }

为什么我无法正确打印第47个斐波纳契数?

我使用64位操作系统,然后我也无法正确打印第46个斐波那契数字,这个数字少于40亿。 #include #include 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]); }

C – 舍入问题(CS50)

我已经谷歌搜索了好几天,我迷路了。 所以在网上做CS50似乎无法掌握这一轮数字。 我的程序搞乱了像2.10这样的浮点数乘以100这样的整数输出209.xxxxxxxx 就像我说我已经阅读了无数的post,我应该使用ceilf并包含 但是我收到了一个错误 greedy.c :(。text + 0x74):未定义引用`ceilf’collect2:错误:ld返回1退出状态make:*** [greedy]错误1 adam @ beethoven:〜/ projects / atom / edx / pSet1 /贪婪$ 我已经看过关于-lm和某个文件的post,但如果我说实话,我不明白这意味着什么。 我绝不是在寻找一个彻底的解决方案,只是在改进方面提供指导。 这是我的代码,可能不像某些人希望的那样精简,但我回到这里的基础;) #include #include int main() { // Initialize Variables int coinsTotal = 0, quarter = 25, dime = 10, nickel = 5, penny = 1, cents; float changeDue; do { printf(“How much […]

我的简短C代码中的小错误。 为什么?

我无法弄清楚为什么这对90%的输入起作用,而不是其他输入。 这是为了告诉你在变化中会得到多少硬币。 大多数测试数量都可以正常工作,但是如果输入4.20(或4.20美元),它将返回23个硬币……它应该是18个硬币(16个季度和2个镍币)。 这个bug在哪里? 这是我的代码: #include #include int main(void){ float change = 0.00; printf(“How much change is owed? “); change = GetFloat(); float quarters = change/.25; change-= (int)quarters*.25; float dimes = change/.10; change-= (int)dimes*.10; float nickels = change/.05; change-= (int)nickels*.05; float pennies = (change+.005)/.01; change-=(int)pennies*.01; int total = (int)quarters+(int)dimes+(int)nickels+(int)pennies; printf(“%d\n”, total); return 0; }

C – 在Mac OSX Lion上编译时,架构x86_64的未定义符号

我在Mac OSX Lion上编译一个非常简单的name.c文件时遇到了一些问题。 现在,我开始在cs50.net上关注哈佛CS50课程。 我不是全新的编程,但我很好奇这门课程的教学方式。 这是name.c的来源: #include #include int main(void) { printf(“State your name:\n”); string name = GetString(); printf(“O hai, %s!\n”, name); return 0; } 如您所见,它需要这个库: https : //manual.cs50.net/CS50_Library 。 现在,当我编译它时,会发生这种情况: Undefined symbols for architecture x86_64: “_GetString”, referenced from: _main in name-vAxcar.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with […]

创建“马里奥风格金字塔”

我正在浏览哈佛CS50在线课程,其中一个问题是使用空格和哈希创建一个“马里奥风格的金字塔”。 我已经解决了空间,但是哈希给了我麻烦。 这是代码: #include #include int main(void) { //get height between 1 and 23 int height; do { printf(“Please enter a height: “); height = GetInt(); } while (height 23); //build pyramid for (int i = 0; i = 0; space–) printf(” “); //add hashtags for (int hash = 2 + i; hash <= height; […]

制作哈希金字塔

目前正在进行CS-50课程,并想知道是否有人可以帮助我。 我应该创建一个程序,它会要求用户在1-23之间的高度(并不断提示用户,直到给出一个有效的答案) – 我能够编码该部分。 #include #include int main(void) { int height; do { printf(“please give me a height between 1-23: “); height = GetInt(); } while (height 23); } do while循环似乎做了它的预期。 现在,程序,给定变量“高度”现在需要创建该高度的金字塔。 金字塔的底部与终端的左下角对齐,其最后的“行”是用2个哈希完成的: 高度为4的样本金字塔: ## ### #### ##### 但是代码需要在任何高度的金字塔1-23中都是通用的。 这是我遇到困难的地方(实际上是在制作代码来绘制这个)。 我已经注意到,对于每一行,所需的哈希数量(如果我们将顶行称为“行1”而后续的“行2”等等…是行号+ 1。至于空格的数量是需要的,可以用高度行数表示 。如果有人能够向我解释如何用C编写这个程序,那将非常感激!:)

在C中包含外部库

我正在尝试将C库用于哈佛大学的开放课程。 可以在此处找到教师有关设置外部库的说明。 我正在按照ubuntu特有的说明进行操作,因为我试图在我的ubuntu盒子上使用这个lib。 我按照页面上的说明进行设置,但是当我使用cs50库函数运行一个简单的helloWorld.c程序时,gcc不想播放。 例: helloWorld.c #include #include int main(void){ printf(“What do you want to say to the world?\n”); string message = GetString(); printf(“%s!\n\n”, message); } $ gcc helloWorld.c /tmp/ccYilBgA.o: In function `main’: helloWorld.c:(.text+0x16): undefined reference to `GetString’ collect2: ld returned 1 exit status 我按照指示中的说明按照说明进行了操作,但它们对我不起作用。 我正在运行ubuntu 12.04。 如果我能进一步澄清我的问题,请告诉我。