-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.

Raildex
  • 3,406
  • 1
  • 18
  • 42
Monk_1o1
  • 21
  • 2
  • You don't want two returns so you probably want to only return count. Use unsigned if don't expect negative values. Why are you using a loop count = cents / 25, right? – Allan Wind Apr 01 '22 at 08:27
  • simple math: to get the quarters needed, you divide by 25, to get the remainder, you use modulo 25. You can't return multiple values in C. You need to decide of what you want to return. Amount of Quarters or the remainer? – Raildex Apr 01 '22 at 08:29
  • `count` is declared inside the loop, so is unknown outside, as the error message tells you. –  Apr 01 '22 at 08:32

3 Answers3

2

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;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • 2
    Shouldn't it be `unsigned int` rather than `unsigned`? I know it's just a matter of style, but `unsigned` has an "old style C" smell to me. – Jabberwocky Apr 01 '22 at 08:43
  • https://stackoverflow.com/questions/7176657/difference-between-unsigned-and-unsigned-int-in-c ; if you care `uint32_t` (from stdint.h) might be a even better choice. – Allan Wind Apr 01 '22 at 08:43
2

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
-1

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.

  • Won't that code only return the remainder instead of the number of times it can divide? For that last case, should't it be this? cents = cents - 25; – Stephan ten Kate Oct 24 '22 at 13:03