c problems!
Author Message
c problems!

C PROGRAMMING PROBLEMS
ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
APPRECIATED!!!

NOTES : None of these exercises use the float data type.

The required formats for user's input are as shown (user's input is shown in
bold type).

Pay attention to layout ? keep the columns in line.

1. Write a C program ~q produce ? the following output. The layout should
cope
with amounts up to 999 pounds and 99 pence.

Enter cost of 1st item > 1.45
Enter cost of 2nd item > 3.82

Total cost  5.27

money tendered >                10?00

Change given  4.73

Hints: Declare int variables pounds, pence, item1cost item2cost,
and tendered.

Read all input data into pounds and pence with scanf

Assign values to item1cost and item2cost in pence.
Display total cost as pounds and pence, using /, % and print f,
Assign value to tendered in pence.
Display change given as pounds and pence, using /, % and printf

#include<stdio.h>
void main (void)

{

int item1cost, item2cost, tendered, totalcost, changegiven, pounds, pence;

printf("enter item1cost:");
scanf ("%d.%d",&pounds,&pence);
item1cost=(pounds*100)+pence;

printf("enter item2cost:");
scanf ("%d.%d",&pounds,&pence);
item2cost=(pounds*100)+pence;

printf ("enter totalcost:");
scanf ("%d.%d"&pounds,&pence);
totalcost=(pounds*100)+pence;
totalcost=item1+item2;
printf("totalcost is %d.%d, &pounds, &pence");

tendered >10.00
printf (" enter amount tendered which must be greater than
10.00:");
scanf ("%d,%d", &pounds,&pence);
tendered=(pounds*100)+pence;

printf("your changegiven is %d.%d, &pounds, &pence);
scanf("%d.%d",&pounds, &pence);
changegiven=totalcost-tendered;

Quote:
}

2.

Write a C program to produce the following output. In Imperial units,
distance
is measured in yards, feet and inches. There are 12 inches in a foot and 3
feet in a yard

Enter distance from A to B 20yds 2ft 6ins
Enter distance from B to C 13yds 1ft  9ins

Total distance from A to C 34yds 1ft 3ins

Hints Declare int variables yards, feet, inches, distAB and distBC Hold
internal values and do all calculations in i

3. Write a C program to produce the following output. Times are given in the
format hh: mm: ss. Assume the use of a 24 hour clock.

Enter start time > 10:45:25
Enter finish time > 23:19:10

Elapsed time = 12 hours 33 minutes 45 seconds

Can your program cope with midnight ?

4. Write a C program to produce the following output.

Enter first person's age > 23yrs 10mths
Enter first person's height                          > 6ft 4ins
Enter second person's age > 27yrs 3m.ths
Enter second person's height > 5ft  10ins

Age difference = 3yrs 5mths
Height difference = Oft  6ins

Your program must cope with the fact that the first person may be older or
younger, shorter or taller than the second. See page 221 of Bronson for use
of abs () function. To use it you have to include < stdlib. h> in the same
way as you include <stdio .h> in order to use scarnf() and printf().

5. Write a C program to produce the following output.

Enter First Name > Tommy
Enter Family Name > Atkins
Enter House Number > 12
Enter Street Name > Acacia Avenue
Enter Town > Hove
Enter County > Sussex
Enter Postcode > BN3 7LS

Tommy Atkins, 12, Acacia Avenue, Hove, Sussex, BN3 7LS.

Assume one string each for First Name, Second Name, Town and County. Assume
two strings each for Street Name and Postcode. House Number is an int.
thank you.
--

Sun, 06 Apr 2003 03:00:00 GMT
c problems!

Quote:
>C PROGRAMMING PROBLEMS
>ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
>APPRECIATED!!!

We do not do other people's homework unless we are in a really bad mood
and want to sabotage your learning. Some of us have been known to note
the names of those that do not do their own work so that we can avoid
employing them in future.

Now if you write your own code and have problems with getting it to
compile or run correctly post the code and some of us may find the time
to give you some hints.

Alternatively change to a different degree.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--

Mon, 07 Apr 2003 03:00:00 GMT
c problems!

Quote:

> C PROGRAMMING PROBLEMS
> ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
> APPRECIATED!!!

> --

you've posted the same question to multiple NG's - perhaps that's why replies
are thin on the ground
check the faq  it will lead to gewtting help
--

RogerH
=============={
--

Mon, 07 Apr 2003 03:00:00 GMT
c problems!

Quote:

> 2.

> Write a C program to produce the following output. In Imperial units,
> distance
> is measured in yards, feet and inches. There are 12 inches in a foot and 3
> feet in a yard

> Enter distance from A to B 20yds 2ft 6ins
> Enter distance from B to C 13yds 1ft  9ins

> Total distance from A to C 34yds 1ft 3ins

> Hints Declare int variables yards, feet, inches, distAB and distBC Hold
> internal values and do all calculations in i

The implicit assumption that AC is coincident on both AB and BC is, in
my  opinion, completely untenable. ABC could easily form a triangle, for
instance, in which case (in Euclidean geometry) the length of AC is
clearly not equal to the simple sum of AB and BC. Therefore the problem
is incompletely specified and cannot be solved without further
clarification.

You may tell your instructor I said so.

Anton
--

Mon, 07 Apr 2003 03:00:00 GMT
c problems!

Quote:

> C PROGRAMMING PROBLEMS
> ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
> APPRECIATED!!!

I'm not going to make your homework. Nor is anybody else going to, in
this group. We can give you feedback on your efforts, so I'm going to
comment only exercise 1. If you want help on te other exercises, show
us you tried them yourself. And please, be specific in your questions.
"It doesn't work" is hardly a question.

Quote:
> The required formats for user's input are as shown (user's input is shown in
> bold type).
> 1. Write a C program ~q produce ? the following output. The layout should
> cope  with amounts up to 999 pounds and 99 pence.
> Enter cost of 1st item > 1.45
> Enter cost of 2nd item > 3.82
> Total cost  5.27
> money tendered >                10?00
> Change given  4.73
> Hints: Declare int variables pounds, pence, item1cost item2cost,
> and tendered.
> Read all input data into pounds and pence with scanf

Ok - if the teacher asks you to use scanf(), you should. For many problems
there are better ways.

Quote:
> Assign values to item1cost and item2cost in pence.
> Display total cost as pounds and pence, using /, % and print f,
> Assign value to tendered in pence.
> Display change given as pounds and pence, using /, % and printf
> #include<stdio.h>
> void main (void)

This is wrong. main returns int, not void. -> int main(void)

Quote:
> {
> int item1cost, item2cost, tendered, totalcost, changegiven, pounds, pence;

> printf("enter item1cost:");
> scanf ("%d.%d",&pounds,&pence);
> item1cost=(pounds*100)+pence;

And what if the user entered only a number of pounds? i.e. "7". You might want
to look at the return value of scanf. Another useful defensive strategy is to set
pounds and pence to a large known value (say INT_MAX) and check if they're altered,
but why reinvent the wheel?

[ snip item2 ]

Quote:
> printf ("enter totalcost:");
> scanf ("%d.%d"&pounds,&pence);
> totalcost=(pounds*100)+pence;

Why are you *asking* for the totalcost?

Quote:
> totalcost=item1+item2;

You're now overwriting the totalcost - the previous value you asked
for is overwritten.

Quote:
> printf("totalcost is %d.%d, &pounds, &pence");

pounds and pence are scratch variables. You must give them a value based
on totalcost. I.e. if totalcost=735 (pence) what should pound become?
what remains?

Quote:
> printf (" enter amount tendered which must be greater than
>             10.00:");

Why should it be >10.00 pounds?

Quote:
> scanf ("%d,%d", &pounds,&pence);
> tendered=(pounds*100)+pence;

Now you should calculate the changegiven, before you're
printing it. Again, you should translate the changegiven
in pounds and pence, that doesn't happen automatically.

Quote:
> printf("your changegiven is %d.%d, &pounds, &pence);
> scanf("%d.%d",&pounds, &pence);

What's this scanf() doing here?

Quote:
> changegiven=totalcost-tendered
> }
> 2.
> Write a C program to produce the following output...

*You* do that. *We* will criticize the result, not the
other way around.

Michiel Salters
--

Mon, 07 Apr 2003 03:00:00 GMT
c problems!

Quote:

> C PROGRAMMING PROBLEMS
> ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
> APPRECIATED!!!

>  NOTES : None of these exercises use the float data type.

> The required formats for user's input are as shown (user's input is shown in
> bold type).

> Pay attention to layout ? keep the columns in line.

> 1. Write a C program ~q produce ? the following output. The layout should
> cope
>  with amounts up to 999 pounds and 99 pence.

> Enter cost of 1st item > 1.45
> Enter cost of 2nd item > 3.82

> Total cost  5.27

> money tendered >                10?00

> Change given  4.73

> Hints: Declare int variables pounds, pence, item1cost item2cost,
> and tendered.

> Read all input data into pounds and pence with scanf

This leads to problems if you enter invalid data, but if that's the
assignment, so be it.

Quote:
> Assign values to item1cost and item2cost in pence.
> Display total cost as pounds and pence, using /, % and print f,
> Assign value to tendered in pence.
> Display change given as pounds and pence, using /, % and printf

> #include<stdio.h>
> void main (void)

NO!!!!!! It's:

int     main(void)

Quote:

> {

> int item1cost, item2cost, tendered, totalcost, changegiven, pounds, pence;

> printf("enter item1cost:");

fflush(stdout);

Quote:
> scanf ("%d.%d",&pounds,&pence);
> item1cost=(pounds*100)+pence;

> printf("enter item2cost:");

fflush(stdout);

Quote:
> scanf ("%d.%d",&pounds,&pence);
> item2cost=(pounds*100)+pence;

---
Quote:
> printf ("enter totalcost:");
> scanf ("%d.%d"&pounds,&pence);
> totalcost=(pounds*100)+pence;
> totalcost=item1+item2;

---

Er... it's your program that should count the total (item1cost +
item2cost), don't ask it from the user. Drop the lines between the ---
marks and calculate the totalcost:

totalcost = item1cost + item2cost;

Quote:
> printf("totalcost is %d.%d, &pounds, &pence");

printf needs integers, not addresses of integer variables (which is
what & gives). Just to clarify: scanf *does* need addresses. Replace
the above line with:

printf("totalcost is %d.%d", pounds, pence);

Before this, you should probably calculate how many pounds and pence
are in totalcost (hint: / divides a value, % gives the modulo). The
assignment was to use / and % with printf, though, so you would make
the calculations in place of 'pounds' and 'pence' in the above line.

Quote:
> tendered >10.00

The above line looks strange...

Quote:
> printf (" enter amount tendered which must be greater than
>             10.00:");
> scanf ("%d,%d", &pounds,&pence);
> tendered=(pounds*100)+pence;

Here, you should calculate the difference between the money tendered
and totalcost, then convert the result into pounds and pence
(actually, use / and % in printf arguments). The place you're doing
the calculation now (after printing) is wrong.

Quote:
> printf("your changegiven is %d.%d, &pounds, &pence);

The above line is missing a quotation mark (") and has two extraneous
ampersands (&).

Quote:
> scanf("%d.%d",&pounds, &pence);

The above line is unnecessary.

Quote:
> changegiven=totalcost-tendered;

Tell the system the program ran ok:

return 0;

Quote:

> }

> 2. Write a C program to produce the following output...
> 3. Write a C program to produce the following output...
> 4. Write a C program to produce the following output...
> 5. Write a C program to produce the following output...

DYOH.
AriL
--
Pain and disappointment are inevitable. Misery is optional.
Homepaged at http://www.angelfire.com/or/lukumies
--

Mon, 07 Apr 2003 03:00:00 GMT
c problems!

Quote:

> >C PROGRAMMING PROBLEMS
> >ANY HELP/ AND/OR C CODING  WITH THE BELOW PROBLEMS WOULD BE MUCH
> >APPRECIATED!!!

> We do not do other people's homework unless we are in a really bad
mood
> and want to sabotage your learning. Some of us have been known to note
> the names of those that do not do their own work so that we can avoid
> employing them in future.

I think it should be pointed out that this Mr. Stravoscoufis is
posting from a hotmail account.  Anyone could have registered an
account in Mr. Stravoscoufis' name as a prank or to cause trouble
for him with his instructors.

I doubt that this is what has happened here, but it is a possibility.
If you are a potential employer keeping lists, put a star by names
that post from these free accounts and try to determine in an
interview if this is the sort of person who would do this kind of
thing.

Quote:
> Now if you write your own code and have problems with getting it to
> compile or run correctly post the code and some of us may find the
time
> to give you some hints.

> Alternatively change to a different degree.

> Francis Glassborow      Association of C & C++ Users
> 64 Southfield Rd
> Oxford OX4 1PA          +44(0)1865 246490
> All opinions are mine and do not represent those of any organisation
> --

--
-Jordan Henderson

--

Thu, 10 Apr 2003 13:00:21 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages