英文:
When running my function it won't return me a value, what am I doing wrong?
问题
以下是您要翻译的代码部分:
我目前正在进行CS50第1周的现金作业,一切都很顺利,直到我处理了零钱部分。任务是从用户获取他们欠多少零钱的输入,然后创建函数来找到返回的最小零钱(例如,欠60美分,然后显示3,表示2个25美分和1个10美分)。
顶部部分是已经提供的部分:
现在我实现了while函数,因为我知道我只想在特定条件下执行它,但我无法让它返回特定的函数到主函数。这是我做的一切:
我已经将代码部分翻译成中文,如您所需。
英文:
I'm currently working on the Cash assignment for Week 1 in CS50, and everything was running smoothly until I moved past quarters. The objective is to get the input form the user on how much change they owe, then create functions that find the least minimum change possible to give back (i.e. 60 cents is owed, then it would show 3 to represent 2 quarters and 1 dime).
The top part is what was already provided:
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
Now I implemented the while function because I know I want it to do it only under a certain condition, but I can't get it to return the specific function back to the main.
This is everything I've done:
int get_cents(void)
{
int cents;
do{
cents = get_int("Change Owed: ");
}
while (cents<0);
return cents;
}
int calculate_quarters(int cents)
{
// TODO
int quaters;
while(24<cents)
{
quaters = cents/25;
}
return quaters;
}
int calculate_dimes(int cents)
{
// TODO
int dimes;
while(9<cents)
{
dimes = cents/10;
}
return dimes;
}
int calculate_nickels(int cents)
{
// TODO
int nickels;
while(4<cents)
{
nickels= cents/5;
}
return nickels;
}
int calculate_pennies(int cents)
{
// TODO
int pennies;
while(0<cents)
{
pennies=cents/1;
}
return pennies;
}
答案1
得分: 1
以下是您提供的代码的翻译部分:
在函数 calculate_quarters
中,当 cents
大于 24
时,存在一个无限循环:
int calculate_quarters(int cents)
{
// TODO
int quaters;
while(24<cents)
{
quaters = cents/25;
}
return quaters;
}
因为变量 cents
没有被改变。看起来您只需要简单地定义函数如下:
int calculate_quarters(int cents)
{
return cents / 25;
}
类似的问题也存在于其他函数,如 calculate_dimes
、calculate_nickels
和 calculate_pennies
中。
而这个调用:
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies( cents );
是多余的。
另外,尝试使用具有命名常量的方式,而不是像 25
这样的魔术数字。
请注意,为了防止使用负值,最好使用无符号整数类型 unsigned int
而不是带符号类型 int
作为变量类型。
如果使用类型 int
,则您的程序可以更简单地使用位于头文件 <stdlib.h>
中声明的标准 C 函数 div
来定义函数,例如:
#include <stdlib.h>
//...
// Calculate the number of quarters to give the customer
div_t result = div( cents, 25 );
int quarters = result.quot;
cents = result.rem;
// Calculate the number of dimes to give the customer
result = div( cents, 10 );
int dimes = result.quot;
cents = result.rem;
// and so on
在这种情况下,您的程序可以如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int get_cents( void )
{
int cents;
do
{
cents = get_int( "Change Owed: " );
} while (cents < 0);
return cents;
}
int main( void )
{
const int CENTS_PER_QUATER = 25;
const int CENTS_PER_DIME = 10;
const int CENTS_PER_NICKEL = 5;
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
div_t result = div( cents, CENTS_PER_QUATER );
int quarters = result.quot;
cents = result.rem;
// Calculate the number of dimes to give the customer
result = div( cents, CENTS_PER_DIME );
int dimes = result.quot;
cents = result.rem;
// Calculate the number of nickels to give the customer
result = div( cents, CENTS_PER_NICKEL );
int nickels = result.quot;
cents = result.rem;
// Calculate the number of pennies to give the customer
int pennies = cents;
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf( "%d\n", coins );
}
英文:
At least in the function calculate_quarters
you have an inifinte while loop when cents
are greater than 24
int calculate_quarters(int cents)
{
// TODO
int quaters;
while(24<cents)
{
quaters = cents/25;
}
return quaters;
}
because the variable cents
is not being changed. It seems you need to define the function simply like
int calculate_quarters(int cents)
{
return cents / 25;
}
Similar problems exist for other functions like calculate_dimes
, calculate_nickels
, and calculate_pennies
.
And this call
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies( cents );
is just redundant.
Also try to use named constants instead of magic numbers like for example 25
.
Pay attention to that instead of using the signed type int
for your variables it is much better to use unsigned integer type unsigned int
to prevent using negative values.
If to use the type int
then your program can look simpler without defining the functions by means of the standard C function div
declared in header <stdlib.h>
For example
#include <stdlib.h>
//...
// Calculate the number of quarters to give the customer
div_t result = div( cents, 25 );
int quarters = result.quot;
cents = result.rem;
// Calculate the number of dimes to give the customer
result = div( cents, 10 );
int dimes = result.quot;
cents = result.rem;
// and so on
In this case your program can look like
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int get_cents( void )
{
int cents;
do
{
cents = get_int( "Change Owed: " );
} while (cents < 0);
return cents;
}
int main( void )
{
const int CENTS_PER_QUATER = 25;
const int CENTS_PER_DIME = 10;
const int CENTS_PER_NICKEL = 5;
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
div_t result = div( cents, CENTS_PER_QUATER );
int quarters = result.quot;
cents = result.rem;
// Calculate the number of dimes to give the customer
result = div( cents, CENTS_PER_DIME );
int dimes = result.quot;
cents = result.rem;
// Calculate the number of nickels to give the customer
result = div( cents, CENTS_PER_NICKEL );
int nickels = result.quot;
cents = result.rem;
// Calculate the number of pennies to give the customer
int pennies = cents;
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf( "%d\n", coins );
}
答案2
得分: 0
以下是您要翻译的内容:
"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:
The logic you have in your calculate functions are not correctly dividing the coins. The while loop you have set up will continue infinitely because the value of cents is never changing within the loop. Instead, you can simply use integer division to calculate the number of coins and return that value. The remainder of the division (the change left after giving the coins) will be handled in the main function as you have it set up already.
Here's the corrected code:
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
int get_cents(void)
{
int cents;
do{
cents = get_int("Change Owed: ");
}
while (cents<0);
return cents;
}
int calculate_quarters(int cents)
{
return cents / 25;
}
int calculate_dimes(int cents)
{
return cents / 10;
}
int calculate_nickels(int cents)
{
return cents / 5;
}
int calculate_pennies(int cents)
{
return cents / 1;
}
This program will correctly calculate the least number of coins to give as change. It first calculates the maximum number of quarters, then the maximum number of dimes from the remaining change, then nickels, and finally pennies."
希望这对您有帮助。
英文:
The logic you have in your calculate functions are not correctly dividing the coins. The while loop you have set up will continue infinitely because the value of cents is never changing within the loop. Instead, you can simply use integer division to calculate the number of coins and return that value. The remainder of the division (the change left after giving the coins) will be handled in the main function as you have it set up already.
Here's the corrected code:
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
int get_cents(void)
{
int cents;
do{
cents = get_int("Change Owed: ");
}
while (cents<0);
return cents;
}
int calculate_quarters(int cents)
{
return cents / 25;
}
int calculate_dimes(int cents)
{
return cents / 10;
}
int calculate_nickels(int cents)
{
return cents / 5;
}
int calculate_pennies(int cents)
{
return cents / 1;
}
This program will correctly calculate the least number of coins to give as change. It first calculates the maximum number of quarters, then the maximum number of dimes from the remaining change, then nickels, and finally pennies.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论