简体   繁体   中英

Passing Argument into function

Write a C program to read a rupee amount (integer value) and break it up into the smallest possible number of bank notes. Assume bank notes are in the denominations 2000, 500, 200, 100, 50, 20 and 10.

I am trying to pass amount into the denCAL() function and want to update it everytime the function is called but the value of amount remains same.

Please provide the solution for my problem and a better approach for the solution and also do le me know some good programming practises which are missing here.

#include <stdio.h>
int amount, note, den;
int denCAL(amount, den){
note = amount/den;
amount -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}
int notes(){
printf("Enter the amount in rupees: ");
scanf("%d", &amount);
if(amount >= 2000){
    denCAL(amount, 2000);
}
if(amount >= 500){
    denCAL(amount, 1000);
}
if(amount >= 200){
    denCAL(amount, 500);
}
if(amount >= 100){
    denCAL(amount, 100);
}
if(amount >= 50){
    denCAL(amount, 50);
}
if(amount >= 20){
    denCAL(amount, 20);
}
if(amount >= 10){
    denCAL(amount, 10);
}
}
int main(){
 notes();
}

OUTPUT

Enter the amount in rupees: 30020
Number of 2000 notes are: 15
Number of 1000 notes are: 30
Number of 500 notes are: 60
Number of 100 notes are: 300
Number of 50 notes are: 600
Number of 20 notes are: 1501
Number of 10 notes are: 3002
int nominals[] = {2000, 500, 200, 100, 50, 20, 10, 0};

void getNominals(unsigned money, int *result)
{
    int *nm = nominals;
    while(*nm && money)
    {
        *result++ = money / *nm;
        money %= *nm++;
    }
}

int main(void)
{
    int result[sizeof(nominals) / sizeof(nominals[0])] = {0};

    getNominals(30020, result);

    for(size_t index = 0; nominals[index]; index++)
    {
        printf("%d = %d\n", nominals[index], result[index]);
    }
}

But in your code you need:

add the return statement:

int denCAL(amount, den)
{
    int note = amount/den;
    amount -= note*den;
    printf("Number of %d notes are:%d\n",     den, note);
    return amount;
}

and then in every if change denCAL(amount, ...); to amount = denCAL(amount, ...);

The reason why you're not updating your amount variable, even considering it's a global, it's because in your function denCAL() , by passing amount as a parameter, you're accessing a copy of it. Also you need to specify the type of the input parameters in your function prototype. You should change the code like this:

void denCAL(int den){ // amount is global, no need to pass it
note = amount/den;
amount -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(2000);
    }
    /* rest of code */
}

Another approach you could use, instead of using a global variable, is to use pointers for example. You could define amount inside notes() , and remove the global you've already declared.


void denCAL(int* amount, int den){ // passing amount by ADDRESS (so it gets modified)
note = (*amount)/den;
(*amount) -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    int amount;
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(&amount, 2000);
    }
    /* rest of code */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM