pointers - c with the wrong answer -
write program dispense change. user enters amount paid , amount due. program determines how many dollars, quarters, dimes, nickels, , pennies should given change. ask user 2 inputs (amount due , amount paid) in main() , send these along pointers 5 parameters (dollars, quarters, dimes, nickels, pennies) function called change(), calculate number of each give out. print results main().
so program compiling getting wrong answer. doing wrong.
#include <stdio.h> #include <conio.h> //function prototype void change( int *d, int *q, int *di, int *n, int *p, int paid, int due ); int main() { //variables int paid; int due; int dollars; int quarters; int dimes; int nickels; int pennies; //reference variables int *d; int *q; int *di; int *n; int *p; printf( "enter amount due: \n" ); scanf( "%d", &due ); printf( "enter amount paid: \n" ); scanf( "%d", &paid ); change( &dollars, &quarters, &dimes, &nickels, &pennies, paid, due ); printf( "dollars = %d\nquarters = %d\ndimes =%d\nnickels = %d\npennies = %d", dollars, quarters, dimes, nickels, pennies ); getch(); return 0; } void change(int *d,int *q,int *di, int *n, int *p, int paid, int due ) { int dollarchange; int quarterchange; int dimechange; int nickelchange; int pennychange; int modu; int modu2; int modu3; int total; int penniestotal; total = due - paid; penniestotal = total * 100; dollarchange = penniestotal / 100; modu = penniestotal % 100; quarterchange = modu / 25; modu2 = modu % 25; dimechange = modu2 / 10; modu3 = modu2 % 10; nickelchange = modu3 / 5; pennychange = modu3 % 5; }
change()
takes lots of pointers arguments, pointers never used in function. there's no return value.
which means nothing in check()
matters.
you're reading due
, paid
input, call function doesn't matter, , print lots of uninitialized variables output.
postscriptum:
you've asked 5 questions far, each of them following similar pattern: explaining program should do, line of "it not work", , code dump.
that not how stackoverflow, or other q&a site, works.
your problem here, example, not "calculating change", it's "parameter passing". other questions posted, conceivably expressed in ten lines of code, maximum. dumped change calculation on instead shows lack of effort on part, why keep getting downvotes.
postscriptum 2:
i visited other questions. 4 out of 5 questions identical: have not understood how pointers , parameter passing work. since questions nebulous, answers on place, isn't helpful. went trial & error without understanding you're doing, , programs got worse each try...
so, consideration, program pointing-to , parameter passing, right, 1 parameter @ time, , quite verbose doing. study it, answers last 4 questions in 1 go:
#include <stdio.h> void function( int * parameter ) { int local = 987; printf( "address of 'local' is: %p\n", &local ); printf( "value of 'local' is: %d\n\n", local ); printf( "address 'parameter' points is: %p\n", parameter ); printf( "value @ address is: %d\n\n", *parameter ); puts( "switching 'parameter' point 'local'...\n" ); int * copy_of_parameter = parameter; parameter = &local; printf( "address 'parameter' points is: %p\n", parameter ); printf( "value @ address is: %d\n\n", *parameter ); puts( "changing value of 'local' through 'parameter'...\n" ); *parameter = 0; printf( "address 'parameter' points is: %p\n", parameter ); printf( "value @ address is: %d\n\n", *parameter ); puts( "making 'parameter' point @ old address again...\n" ); parameter = copy_of_parameter; *parameter = 666; printf( "address 'parameter' points is: %p\n", parameter ); printf( "value @ address is: %d\n\n", *parameter ); return; } int main() { int in_main = 123; printf( "address of 'in_main' is: %p\n", &in_main ); printf( "value @ address is: %d\n\n", in_main ); puts( "calling function()...\n" ); function( &in_main ); puts( "returned function()...\n" ); printf( "address of 'in_main' is: %p\n", &in_main ); printf( "value @ address is: %d\n\n", in_main ); return 0; }
Comments
Post a Comment