有什么问题吗,我在此C代码中正在做吗?

发布于 2025-01-18 13:46:36 字数 571 浏览 3 评论 0原文

我想在 cents 参数中输入参数,这样如果参数(cents)大于 25,它将进入 for 循环并除以 25 后给出余数。现在,这个余数被保存为分的新值,如果它仍然大于 25,它将进入循环并发生相同的过程,直到我们得到最后的分值,该值小于 25。

沿着这个,我运行这个计数标识符,以便每次循环运行时它都会累加并告诉我循环运行了多少次。

int calculate_quarters(int cents)
{
    // TODO

    for(int count=0; cents>=25; count++)
    {
        cents = cents%25;
    }

    return cents;
    return count;
}

我想最后得到 cent 的值,它小于 25 并计算循环运行的次数。但是,每次运行它时它都会给我这个错误

undeclared use of identifier 'count'

我想做的假设是,我给出了参数 55到函数calculate_quarters_,所以在循环内运行后,它应该给我值5分,最终计数为2。

I wanted to enter the argument in cents parameter so that if the argument(cents) is greater than 25, it will go inside the for loop and after dividing by 25 give the remainder. Now, this remainder is saved as the cent's new value, and if it is still greater than 25 it will go inside the loop and same process happen untill we get the last value of cents, which is less than 25.

Along this, I run this count identifier so that every time the loop run it adds up and and tell me how many times the loop runned.

int calculate_quarters(int cents)
{
    // TODO

    for(int count=0; cents>=25; count++)
    {
        cents = cents%25;
    }

    return cents;
    return count;
}

I want to get the value of cent in the end , which is less than 25 and count of how many times loop run.But, every time i run it it gave me this error

undeclared use of identifier 'count'

What I wanted to do suppose, I gave the argument 55 to the function calculate_quarters_, so after running inside the loop, it should give me the value of cents 5 and count is 2 in the end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心在旅行 2025-01-25 13:46:36

这是一个工作示例,演示如何通过(OUT)指针返回两个值:

#include <stdio.h>

void calculate_quarters(unsigned cents, unsigned *count, unsigned *remainder) {
    *count = cents / 25;
    *remainder = cents % 25;
}

int main() {
    unsigned cents = 54;
    unsigned count;
    unsigned remainder;
    calculate_quarters(cents, &count, &remainder);
    printf("%u cents is %u quarters and %u remainding cents\n", cents, count, remainder);
    return 0;
}

Here is a working example demonstrating how to return two values via (out) pointers:

#include <stdio.h>

void calculate_quarters(unsigned cents, unsigned *count, unsigned *remainder) {
    *count = cents / 25;
    *remainder = cents % 25;
}

int main() {
    unsigned cents = 54;
    unsigned count;
    unsigned remainder;
    calculate_quarters(cents, &count, &remainder);
    printf("%u cents is %u quarters and %u remainding cents\n", cents, count, remainder);
    return 0;
}
旧情勿念 2025-01-25 13:46:36

在C中,这不是返回语句的工作方式,如果要返回多个值,则必须使用结构。另外,您无需使用循环来解决此问题。

我会这样解决:

如果您正在学习CI推荐 http://beej.us/guide/bgc /

#include <stdio.h>

struct quarter {
  int remainder;
  int count;
};

struct quarter calculate_quarters(int cents) {
  int count = cents / 25;
  int remainder = cents % 25;
  struct quarter q = {.remainder = remainder, .count = count};
  return q;
}

int main(void) {
  int cents = 55;
  struct quarter q = calculate_quarters(cents);
  printf("For %d cents, you have %d quarters and %d cents left over\n", cents,
         q.count, q.remainder);

  return 0;
}
$ gcc quarter.c -Wall -o quarter
$ ./quarter
For 55 cents, you have 2 quarters and 5 cents left over

In C that's not how the return statement works, if you want to return multiple values you must use a struct. Also, you don't need to use a loop to solve this problem.

I would solve it like this:

If you're learning C I recommend http://beej.us/guide/bgc/

#include <stdio.h>

struct quarter {
  int remainder;
  int count;
};

struct quarter calculate_quarters(int cents) {
  int count = cents / 25;
  int remainder = cents % 25;
  struct quarter q = {.remainder = remainder, .count = count};
  return q;
}

int main(void) {
  int cents = 55;
  struct quarter q = calculate_quarters(cents);
  printf("For %d cents, you have %d quarters and %d cents left over\n", cents,
         q.count, q.remainder);

  return 0;
}
$ gcc quarter.c -Wall -o quarter
$ ./quarter
For 55 cents, you have 2 quarters and 5 cents left over
So尛奶瓶 2025-01-25 13:46:36

我认为你的问题是你想计算可以除以 25 的数字,多少次?这是我的建议。

int calculate_quarters(int cents)
{
    int count = 0;
    while (cents >= 25)
    {
        count++;
        cents %= 25;
    }
    return count;
}

如果你想在for循环中执行:

int calculate_quarters(int cents)
{
    int count;
    for (count = 0; cents >= 25; count++)
    {
        cents = cents % 25;
    }
    return count;
}

注意:如果你想返回count,你可以在开始时初始化它。

I think your problem is you want to count number which can divide 25, how many times ? Here is my suggestion.

int calculate_quarters(int cents)
{
    int count = 0;
    while (cents >= 25)
    {
        count++;
        cents %= 25;
    }
    return count;
}

if you want to do in for loop:

int calculate_quarters(int cents)
{
    int count;
    for (count = 0; cents >= 25; count++)
    {
        cents = cents % 25;
    }
    return count;
}

Note: if you want to return count you can initialize it in the beginnning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文