I am trying to complete some math tutorials, and wanted to write an
answer generator in C.
The type of question is;
"A doll costs $20, a toy car costs $6, a yoyo costs $1.
What combination of toys will allow you to by 100 toys for $200?"
So far I have:
#include <stdio.h>
#define DOLL 20
#define CAR 6
#define YOYO 1
#define TOTALCASH 200
#define TOTALNUM 100
void main (void)
{
int cost;
int numb_tops;
int numb_dolls;
int numb_cars;
printf("A doll costs $%i.00, a car costs $%i.00, and a yoyo costs
$%i.00\n", DOLL, CAR, YOYO);
printf("With $%i.00 dollars you can buy %i toys, \nif you buy the
following;\n", TOTALC, TOTALN);
for(cost = 0; numbert < TOTALN & cost < TOTALC; )
}
But I cannot figure out the loops needed to compute the answer.
I am looking to make this into a function to be called when a system
of equations needs to be solved.
Thanks 9 1442
"claymic" <cl*****@cmccommunications.net> wrote in message
news:26**************************@posting.google.c om... I am trying to complete some math tutorials, and wanted to write an answer generator in C.
The type of question is;
"A doll costs $20, a toy car costs $6, a yoyo costs $1. What combination of toys will allow you to by 100 toys for $200?"
So far I have:
#include <stdio.h> #define DOLL 20 #define CAR 6 #define YOYO 1 #define TOTALCASH 200 #define TOTALNUM 100 void main (void) { int cost; int numb_tops; int numb_dolls; int numb_cars;
printf("A doll costs $%i.00, a car costs $%i.00, and a yoyo costs $%i.00\n", DOLL, CAR, YOYO); printf("With $%i.00 dollars you can buy %i toys, \nif you buy the following;\n", TOTALC, TOTALN); for(cost = 0; numbert < TOTALN & cost < TOTALC; ) }
But I cannot figure out the loops needed to compute the answer.
I am looking to make this into a function to be called when a system of equations needs to be solved.
Thanks
Any program does what you tell it to do... so as a programmer...
you gotta figure out how the problem is solved in your head first...
so i suggest you take out a piece of paper...
do this as a math word problem... then write the program according to what
you have done on the paper...
this is called "design" or "planning"...
:)

{ Kelvin@!!! }
claymic <cl*****@cmccommunications.net> wrote:
: I am trying to complete some math tutorials, and wanted to write an
: answer generator in C.
: The type of question is;
: "A doll costs $20, a toy car costs $6, a yoyo costs $1.
: What combination of toys will allow you to by 100 toys for $200?"
: So far I have:
: #include <stdio.h>
: #define DOLL 20
: #define CAR 6
: #define YOYO 1
: #define TOTALCASH 200
: #define TOTALNUM 100
: void main (void)
: {
: int cost;
: int numb_tops;
: int numb_dolls;
: int numb_cars;
:
: printf("A doll costs $%i.00, a car costs $%i.00, and a yoyo costs
: $%i.00\n", DOLL, CAR, YOYO);
: printf("With $%i.00 dollars you can buy %i toys, \nif you buy the
: following;\n", TOTALC, TOTALN);
: for(cost = 0; numbert < TOTALN & cost < TOTALC; )
: }
Since this is a _math_ problem, try couching it in mathematical terms.
E.g.:
find nonnegative integers m,n,p to satisfy:
m*DOLL + n*CAR + p*YOYO = 200 dollars,
subject to the constraint that: m + n + p = 100 toys.
That should get you started.
Wendy E. McCaughrin writes: claymic <cl*****@cmccommunications.net> wrote: : I am trying to complete some math tutorials, and wanted to write an : answer generator in C.
: The type of question is;
: "A doll costs $20, a toy car costs $6, a yoyo costs $1. : What combination of toys will allow you to by 100 toys for $200?"
: So far I have:
: #include <stdio.h> : #define DOLL 20 : #define CAR 6 : #define YOYO 1 : #define TOTALCASH 200 : #define TOTALNUM 100 : void main (void) : { : int cost; : int numb_tops; : int numb_dolls; : int numb_cars; : : printf("A doll costs $%i.00, a car costs $%i.00, and a yoyo
costs : $%i.00\n", DOLL, CAR, YOYO); : printf("With $%i.00 dollars you can buy %i toys, \nif you buy
the : following;\n", TOTALC, TOTALN); : for(cost = 0; numbert < TOTALN & cost < TOTALC; ) : }
Since this is a _math_ problem, try couching it in mathematical terms. E.g.: find nonnegative integers m,n,p to satisfy: m*DOLL + n*CAR + p*YOYO = 200 dollars, subject to the constraint that: m + n + p = 100 toys. That should get you started.
Get you started on what? I see two equations and three unknowns.
I suggest three nested for loops. As a wild guess, 0 dolls, 20 cars and 80
yoyo's might work. Also 5, 1, and 94.
But that is the easy part. The "system of equations" part sounds more
difficult. I don't know what you want there but it sounds like a pretty
impressive end product.
claymic wrote: "A doll costs $20, a toy car costs $6, a yoyo costs $1. What combination of toys will allow you to by 100 toys for $200?"
dolls + cars + yoyos = 100 ==> yoyos = 100  dolls  cars
$200 = $20*dolls + $6*cars + $1*yoyos
= $20*dolls + $6*cars + $1*(100  dolls  cars)
= $19*dolls + $5*cars + $100
$100 = $19*dolls + $5*cars
so you can buy 5 dolls, 1 car and 94 yoyos.
E. Robert Tisdale wrote: claymic wrote:
"A doll costs $20, a toy car costs $6, a yoyo costs $1. What combination of toys will allow you to by 100 toys for $200?"
dolls + cars + yoyos = 100 ==> yoyos = 100  dolls  cars
$200 = $20*dolls + $6*cars + $1*yoyos = $20*dolls + $6*cars + $1*(100  dolls  cars) = $19*dolls + $5*cars + $100
$100 = $19*dolls + $5*cars
so you can buy 5 dolls, 1 car and 94 yoyos.
Oops!! It's been a while since I was in school.
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<40**************@jpl.nasa.gov>... claymic wrote:
"A doll costs $20, a toy car costs $6, a yoyo costs $1. What combination of toys will allow you to by 100 toys for $200?"
dolls + cars + yoyos = 100 ==> yoyos = 100  dolls  cars
$200 = $20*dolls + $6*cars + $1*yoyos = $20*dolls + $6*cars + $1*(100  dolls  cars) = $19*dolls + $5*cars + $100
$100 = $19*dolls + $5*cars
so you can buy 5 dolls, 1 car and 94 yoyos.
or... no dolls, 20 cars and 80 yoyos.

Peter
here is a start:
int main()
{
int ndoll = 0;
int ncar = 0;
int nyoyo = 0;
int tcash = 200;
int ttoys = 100;
int pdoll = 20;
int pcar = 6;
int pyoyo = 1;
printf("A doll costs $%i.00,
a car costs $%i.00,
and a yoyo costs $%i.00\n", pdoll, pcar, pyoyo);
while(ttoys <= 100 && tcash <= 200){
ttoys = ndoll + ncar + nyoyo;
tcash =(pdoll *ndoll) + (pcar *ncar) + (pdoll *nyoyo);
}
printf("With $%i.00 dollars you can buy %i toys,
\nif you buy the following;\n", TOTALC, TOTALN);
return(0);
}
On 10 May 2004 16:32:55 0700, cl*****@cmccommunications.net (claymic)
wrote: I am trying to complete some math tutorials, and wanted to write an answer generator in C.
The type of question is;
"A doll costs $20, a toy car costs $6, a yoyo costs $1. What combination of toys will allow you to by 100 toys for $200?"
x=doll y=toy_car z=yoyo
/
x + y + z = 100
20*x + 6*y + z= 200
\
z = 100  x  y
20*x + 6*y + 100  x  y = 200
19x + 5y = 100
19x = 100  5y
x, y unsigned
y=0 => no
y=1 => 100  5 = 95 = 5*19 #ok# => y=1, x=5, z=10051=94
#include <stdio.h>
int main(void)
{unsigned x, y, z, t;
y=0;
while((t = 5*y)<=100)
{if((100  t)%19 == 0)
{x = (100  t) / 19; z = 100  x  y;
printf("\aHo trovato la soluzione: "
"doll=%u toy_car=%u yoyo=%u\n", x, y, z);
}
++y;
}
printf("y==%u\n", y);
return 0;
}
So far I have:
#include <stdio.h> #define DOLL 20 #define CAR 6 #define YOYO 1 #define TOTALCASH 200 #define TOTALNUM 100 void main (void) { int cost; int numb_tops; int numb_dolls; int numb_cars;
printf("A doll costs $%i.00, a car costs $%i.00, and a yoyo costs $%i.00\n", DOLL, CAR, YOYO); printf("With $%i.00 dollars you can buy %i toys, \nif you buy the following;\n", TOTALC, TOTALN); for(cost = 0; numbert < TOTALN & cost < TOTALC; ) }
But I cannot figure out the loops needed to compute the answer.
I am looking to make this into a function to be called when a system of equations needs to be solved.
Thanks This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by BVM 
last post: by

3 posts
views
Thread by Alex 
last post: by

7 posts
views
Thread by Shapper 
last post: by

6 posts
views
Thread by Federico 
last post: by

reply
views
Thread by Jitesh 
last post: by

27 posts
views
Thread by John Salerno 
last post: by

8 posts
views
Thread by vj 
last post: by

1 post
views
Thread by arun 
last post: by

17 posts
views
Thread by Michael Reichenbach 
last post: by
            