this is a c++ book example.. can anyone solve it??

4. Write a program that generates a random integer number between 1 and 1000 representing the amount of money the user has (in a fraction of a KD). The program passes the amount to a function coin_changer that calculates the minimum number of coins that the amount represents in 100, 50, 20, 10, and 5 files. The function then passes back the data to the main program which outputs the results. The function should not perform any I/O operations



Sample output
Enter the amount of money you have: 725 files.
The amount contains:
7 coins of 100 files.
0 coins of 50 files.
1 coins of 20 files.
0 coins of 10 files.
1 coins of 5 files.




wish you luck!!:)


hint( use division and modulus)
Last edited on
This is not a difficult task. I assume this is an assignment you've been given?
Yeah.. Modulus problem. :) Pretty easy logic for this one. :D
yeah .. i love how modulus works with divisions in c++:)
jemeripol



naah.. don`t worry!! i`m studying for my midterm this coming Monday!!:)

i`ve submitted my homeworks yesterday..:D

zhuge
for ones who want the answer...

here`s the code..

it`s a shame no one participated in this (u_ u)..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cmath> 

using namespace std;

#include <cstdlib>
#include <ctime>

using namespace std;



void coin_changer (int,int &, int &, int &, int &, int &); //function prototype


int main()
{

	int files;
	int hund;
	int fift;
	int twent;
	int tens;
	int five;

	unsigned seed=time(0);
	srand(seed);
	files=1+rand()%1000;

	coin_changer(files,hund,fift,twent,tens,five);


cout<<"enter the amount of money you have: "<< files <<"files"<<endl;

cout<<" \n";

coin_changer(files,hund, fift, twent, tens, five);

cout<< "the amount contains: \n";

cout<< hund <<" coins  of  100 files.\n";
cout<< fift <<" coins  of  50  files.\n";
cout<< twent <<" coins  of  20  files.\n";
cout<< tens <<" coins  of  10  files.\n";
cout<< five <<" coins  of  5  files.\n";
cout<< endl;

return 0;
}


void coin_changer(int files, int &hund, int &fift, int &twent, int &tens, int &five )
{


	hund=files/100;
	files=files%100;
	fift=files/50;
	files=files%50;

	twent=files/20;
	files=files%20;
	tens=files/10;
	files=files%10;

	five=files/5;


}

/*
enter the amount of money you have: 135files

the amount contains:
1 coins  of  100 files.
0 coins  of  50  files.
1 coins  of  20  files.
1 coins  of  10  files.
1 coins  of  5  files.

Press any key to continue
*/
/*
enter the amount of money you have: 288files

the amount contains:
2 coins  of  100 files.
1 coins  of  50  files.
1 coins  of  20  files.
1 coins  of  10  files.
1 coins  of  5  files.

Press any key to continue
*/
Last edited on
Suggestion to simplify coin_changer function:

1
2
3
4
5
hund = files / 100;
files = files - hund;
fift = files / 50;
files = files - fift;
....

Also there is no need to call coin_changer twice.
yeah,, but my instructor wants it like this...

he hates simplifying :`c
zoran404
That doesn't sound like a very good instructor/teacher..
I guess he wants to know if we really understand and to show him that we know what are we doing.?? Or just to make sure we're doing our work on our own ,,,
zoran404
Good job. :)
Thank yew(⌒▽⌒)
jemeripol
Topic archived. No new replies allowed.