Function: convert to notes

This is a discussion on Function: convert to notes within the C Programming forums, part of the General Programming Boards category; The function need to cal. the no. of bank notes given a certain amount of money, it will calculate the ...

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Function: convert to notes

    The function need to cal. the no. of bank notes given a certain amount of money, it will calculate the number of hundred-dollar notes and twenty-dollar notes, given a certain amount of money, and also return the remaining amount of money not enough for a twenty-dollar note.

    for example: $1278
    the function should return 12 hundred-dollar notes
    ,3 twenty-dollar notes, and then 18 as the remaining amount.

    Can someone look my code and check my syntax error, thk a lot.
    Code:
    struct notes { 
      int oneHundred;
      int twenty;
      int remainder;
    };
    
    
    void convertToNotes2(int money, struct notes* notes) {
    
     (*notes).oneHundred=money/100;                      /* 1278 / 100 = 12 */
     (*notes).twenty=(money-oneHundred*100)/20;          /* (1278 - 12 x 100) / 20 = 3 */
     (*notes).remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
    
    }

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hello alice,

    I might be able to help. First of all your syntax problem has to do with (*notes). You see when using a struct all you have to do is notes.twenty insteald of (*notes).twenty since you already have struct *notes notes in the function call.

    What I will do is put the function convertToNotes2() in your struct, also making the three variables private so no other functions outside of the struct can touch/modify the values.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct notes {
    public: // Outside functions can call on this
    	void convertToNotes2(int money);
    private: // Outside functions can't touch these
    	int oneHundred;
    	int twenty;
    	int remainder;
    };
    
    int main() {
    	int amount;
    	struct notes NOTES;
    
    	printf("Enter your amount: ");
    	scanf("%i", &amount);
    
    	NOTES.convertToNotes2(amount);
    
    	return 0;
    }
    
    /*
    ** This function is inside struct notes
    */
    void notes::convertToNotes2(int money) {
    	oneHundred = money / 100;				/* 1278 / 100 = 12 */
    	twenty = (money-oneHundred * 100) / 20;			/* (1278 - 12 x 100) / 20 = 3 */
    	remainder = (money-oneHundred * 100) - (twenty * 20);	/* 1278 - 12 x 100 - 3 x 20 = 18 */
    
    	printf("$%i - %i (One hundred dollar bills)\n", (oneHundred*100), oneHundred);
    	printf("$%i - %i (Twenty dollar bills)\n", (twenty*20), twenty);
    	printf("$%i (Remainder)\n", remainder);
    }
    Most of that should make sense, I just initialized the struct in main() and called on the function convertToNotes() which automatically sent retrieved the data and sent the text to the screen. Also the reason why you can modify the variables remainder, oneHundred, and twenty in the function convertToNotes() is because that function is within the struct speaking it can touch private variables.

    Now of course if you still want the three integer variables in the public category in case you need to get the values feel free to move them however you see fit.


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    Hi

    Thank for your reply.

    I want to use
    Code:
    void convertToNotes2(int money, struct notes* notes)
    as the function prototype. and
    Code:
    struct notes { 
      int oneHundred;
      int twenty;
      int remainder;
    };
    as the structure, is it correct if I change '(*notes).' to 'notes.' ?
    the function only need to cal. the hundred,twenty and the remaining notes.it does not need to print the value out.

    Thank a lot.


    Code:
    struct notes { 
      int oneHundred;
      int twenty;
      int remainder;
    };
    
    
    void convertToNotes2(int money, struct notes* notes) {
    
     notes.oneHundred=money/100;                      /* 1278 / 100 = 12 */
     notes.twenty=(money-oneHundred*100)/20;          /* (1278 - 12 x 100) / 20 = 3 */
     notes.remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
    
    }

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hello alice,

    Glad to be of help, and yes, to run your current code all you have to do is change (*notes). to notes.. Example:

    notes.oneHundred = (money / 100)

    Will work.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    Hi

    I need to change the function to another prototype (no struct use),
    Code:
    int convertToNotes(int money, int* oneHundred, int* twenty);
    Did my code write correctly? please specify what I make wrong .
    Thank a lot.

    Code:
    int convertToNotes(int money, int* oneHundred, int* twenty) {
    
    int remainder;
    
    oneHundred=money/100;      /* 1278 / 100 = 12 */
    twenty=(money-oneHundred*100)/20; /* (1278 - 12 x 100) / 20 = 3 */
    remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
    
    }

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hello alice,

    In fact one thing I learned in C is its not easy to pass an integer thru a function, then to write it and send it back. What I would do instead is write the data to your struct, and then read it from then on, example:

    Code:
    struct notes {
    	int oneHundred;
    	int twenty;
    	int remainder;
    };
    
    void convertToNotes(int money, struct notes *NOTES);
    
    int main() {
    	int amount;
    	struct notes *NOTES = (notes*) malloc (sizeof(notes));	// Allocated memory to get data
    
    	printf("Enter your amount: ");
    	scanf("%i", &amount);
    
    	convertToNotes(amount, NOTES);							// Send data to function
    
    	printf("%i\n%i\n%i\n", NOTES->oneHundred, NOTES->twenty, NOTES->remainder);
    
    	free(NOTES);											// Free memory
    
    	return 0;
    }
    
    void convertToNotes(int money, struct notes *NOTES) {
    	// Use '->' if using a pointer for struct else use '.'
    	NOTES->oneHundred=money/100;      /* 1278 / 100 = 12 */
    	NOTES->twenty=(money-NOTES->oneHundred*100)/20; /* (1278 - 12 x 100) / 20 = 3 */
    	NOTES->remainder=money-NOTES->oneHundred*100-NOTES->twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
    }
    I changed some things around here. What this does now is the program now calls the function directly then pointing to your structure notes. Then it sets the data. Of course we allocated a slot of memory for the structure which we free later.

    Why we allocate memory is because when you use 'struct *' call you have to set the pointer to read something, and it can't read empty memory so once you allocate memory for it you're all set. Also you don't need to make convertToNotes an int call because you shouldnt have to return a value when the function is over. Example:

    Code:
    int myFunc(void);
    
    int main() {
    	int test;
    
    	test = myFunc();
    }
    
    int myFunc(void) {
    	return (255);
    }
    Then 'test' would equal 255. Unless you need that, you wont need the int call for your functions.


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    Hi

    Thank for your detail reply.but I am sorry to let you know that I have not state clearly in my previous reply that I want to use another function ,

    Code:
    int convertToNotes(int money, int* oneHundred, int* twenty); {
    because I want to learn using different function to make the program work.

    Rgds

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To dereference(retrieve or set the value it points to) a pointer, you use an asterisk.
    Code:
    int convertToNotes(int money, int* oneHundred, int* twenty)
    {
        int remainder;
    
        *oneHundred = money / 100;      /* 1278 / 100 = 12 */
        *twenty = (money - *oneHundred * 100) / 20; /* (1278 - 12 x 100) / 20 = 3 */
        remainder = money - *oneHundred * 100 - *twenty * 20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
    }
    You may want to look into the modulus % operator. This returns the remainder of a division:
    Code:
    int convertToNotes(int money, int* oneHundred, int* twenty)
    {
        int remainder;
    
        *oneHundred = money / 100;
        *twenty = (money % 100) / 20; /* 1278 % 100 == 78; 78 / 20 == 3 */
        remainder = money % 20;       /* 1278 % 20 == 18; */
    }
    Last edited by anonytmouse; 05-03-2004 at 02:34 AM.

  9. #9
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    hi,

    many thank of your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21