468,760 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,760 developers. It's quick & easy.

new to C, how do I solve this problem?

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
Nov 14 '05 #1
9 1376
"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@!!! }
Nov 14 '05 #2
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 non-negative 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.
Nov 14 '05 #3
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 non-negative 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.
Nov 14 '05 #4
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.

Nov 14 '05 #5
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.
Nov 14 '05 #6
On 10 May 2004 16:32:55 -0700, cl*****@cmccommunications.net (claymic)
wrote in comp.lang.c:
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)


[snip]

This might actually be a C program if main() were defined legally.

main() returns int. ALWAYS.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
"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
Nov 14 '05 #8
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);
}
Nov 14 '05 #9
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=100-5-1=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


Nov 14 '05 #10

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
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
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.