new to C, how do I solve this problem?

C

claymic

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
 
K

Kelvin@!!!

claymic said:
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"...
:)
 
W

Wendy E. McCaughrin

: 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.
 
E

E. Robert Tisdale

claymic said:
"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.
 
O

osmium

Wendy said:
: 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.
 
O

osmium

E. Robert Tisdale said:
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.
 
J

Jack Klein

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.
 
P

Peter Nilsson

E. Robert Tisdale said:
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.
 
S

s.korbyn

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);
}
 
R

RoSsIaCrIiLoIA

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top