3:08AM Perth time.  It's UWAweek 22

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying selected article
Showing 1 of 1168 articles.
Currently no other people reading this forum.




Login to reply

👍?
helpful
9:11pm Wed 9th Aug, Jie W.

"Christopher McDonald" chris.mcdonald@uwa.edu.au wrote:

"Jie Wei" 21022228@student.uwa.edu.au wrote:

Hi Sir Experienced weird result for the lab 1 first task (coin change).

Did you make any progress in solving this problem, Jie?

Hi Professor thanks for checking with me, as I'm a FIFO engineer, i just back to perth tonight. here's the code for this refresh task. the rest of the tasks are all good, just this first one gave me a funny result. thank you so much for your support.

#include <stdio.h>

//define the coin denominations
#define C50 50
#define C20 20
#define C10 10
#define C5  5
#define C2  2
#define C1  1

//define the maximum value that can be purchased
#define MAX_VALUE 100

int main(void)
{
	// declare a variable to store the purchase value in cents
    int value;

    // prompt the user to enter the purchase value in cents
    printf("Enter the purchase value in cents (1 to %d): ", MAX_VALUE);
    scanf("%d", &value);

    // check if the value is valid
    if(value < 1 || value > MAX_VALUE){
        //print an error message and exit program
        printf("Invalid value.\n");
        return 1;
    }

    //calculate the change in cents
    int change = MAX_VALUE - value;

    //print the change amount
    printf("The change is %d cents.\n", change);

    //declare variables to store the number of coins for each denomination
    int n50 = 0, n20 = 0, n10 = 0, n5 = 0, n2 = 0, n1 = 0;

    //calculate the number of coins for each denomination using integer division and modulo operator
    //change %= C50 -> change = change % C50
    n50 = change / C50;
    change %= C50;
    n20 = change / C20;
    change %= C20;
    n10 = change / C10;
    change %= C10;
    n5 = change / C5;
    change %= C5;
    n2 = change / C2;
    change %= C2;
    n1 = change / C1;
    change %= C1;

    //print the number of coins for each denomination if it is positive
    if(n50 > 0){
        printf("%d x 50c.\n", n50);
    }
    if(n20 > 0){
        printf("%d x 20c.\n", n20);
    }
    if(n10 > 0){
        printf("%d x 10c.\n", n10);
    }
    if(n5 > 0){
        printf("%d x 5c.\n", n5);
    }
    if(n2 > 0){
        printf("%d x 2c.\n", n2);
    }
    if(n1 > 0){
        printf("%d x 1c.\n", n1);
    }

    return 0;
}

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by Chris.McDonald@uwa.edu.au
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy