473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Beginner excersice #1

Am I did the following program corectly according to the question?

Question:

/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the European
market and wants a program that converts inches to
centimeters (1 inch = 2.54 cm). The company wants the
program set up so that it prompts the user to enter an inch
value. Your assignment is to define the program objectives and
to design the program (steps 1 and 2 of the programming process).
*/

solution:

#include<stdio.h>

#define centmeter 2.54
int main(void)
{
int inches_to_input;
float inches_to_output;
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;
}
Jan 21 '06 #1
17 3024
I think your program is correct except the int type of inches_to_input.
Why don't you set the type of inches_to_input to float?

Jan 21 '06 #2
Kies Lee wrote:
I think your program is correct except the int type of inches_to_input.
Why don't you set the type of inches_to_input to float?


Please provide context when replying, there is no guarantee that others
have (or ever will) see the post you are replying to. See
http://cfaj.freeshell.org/google/ for details on how to provide proper
context and other useful information.
You may also find this URL useful http://clc-wiki.net/wiki/Intro_to_clc
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 21 '06 #3
C_beginner wrote:
Am I did the following program corectly according to the question?
It looks to me like it should basically work, although there are some
issues and style matters you should consider.
Question:

/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the European
market and wants a program that converts inches to
centimeters (1 inch = 2.54 cm). The company wants the
program set up so that it prompts the user to enter an inch
value. Your assignment is to define the program objectives and
to design the program (steps 1 and 2 of the programming process).
*/

solution:

#include<stdio.h>
This would be easier for a human to read with an extra space.

#include <stdio.h>
#define centmeter 2.54
int main(void)
{
int inches_to_input;
Why use an int? It is common for people to deal with fractional inches.
Obviously if you change it you will have to change the scanf format
specifier.
float inches_to_output;
Why use a float rather than a double? Most of the time when you use
floats they get immediately promoted to double for the calculation
anyway. In this case, of course, you could also not use the variable at
all and print the result directly.
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
You should check the return value of scanf to find out if it succeeded.
If it fails because the user entered "one" then inches_to_input would
not be initialised so the program could well output some random value.
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;
}


Generally a better first attempt than many we see here from beginners.

In the spirit of being helpful, I would also like to point out the
comp.lang.c FAQ to you which contains a lot of useful information and
help with questions you are likely to ask as you progress http://c-faq.com/
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 21 '06 #4
"C_beginner" <n...@no.com> wrote:
#define centmeter 2.54
int main(void)
{
int inches_to_input;
float inches_to_output;
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;


I would suggest using better names like:

#define CM_PER_INCH 2.54

int main(void)
{
float inches;
float centimeters;

printf("please enter inches ");
scanf("%f", &inches);
centimeters = inches * CM_PER_INCH;
printf("%1.2f inches converted are %f1.2 centimeters\n",
inches, centimeters);
return 0;
}

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7

Jan 21 '06 #5
On Sat, 21 Jan 2006 13:51:23 +0530, C_beginner wrote:
Am I did the following program corectly according to the question? int inches_to_input;
float inches_to_output;
inches_to_output = inches_to_input * centmeter;

inches_to_input is an integer while inches to output is float. While
the integer will be automatically become a float during the
multiplication with a float(centmeter) I would write this code
differently.

Option1 : Why not float inches_to_input?
Option2 : If you want inches_to_input to be an integer you could
do -> inches_to_output = (float) inches_to_input * centmeter;
This is called casting

Jan 21 '06 #6

"C_beginner" <no@no.com> wrote
Am I did the following program corectly according to the question?

Question:

/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the European
market and wants a program that converts inches to
centimeters (1 inch = 2.54 cm). The company wants the
program set up so that it prompts the user to enter an inch
value. Your assignment is to define the program objectives and
to design the program (steps 1 and 2 of the programming process).
*/

solution:

#include<stdio.h>

#define centmeter 2.54
int main(void)
{
int inches_to_input;
float inches_to_output;
Give these better names, like "inches" and "centimeters".
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
check the return from scanf(). If it returns 1, the user has entered an
integer correctly. If it doesn't, something has gone wrong, so print out an
error message.
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;
}


Program seems otherwise OK to me
Jan 21 '06 #7
Thanks for all the help. I learned little bit about promotion rank,
conversion operator.
Jan 21 '06 #8
[help sniped...]

You should check the return value of scanf to find out if it succeeded.
If it fails because the user entered "one" then inches_to_input would
not be initialised so the program could well output some random value.


Also told by Malcolm, is this the way that it has to be done?

if(!scanf("%d",&inches_to_input))
{
operations and calcuations
}


Jan 21 '06 #9
"C_beginner" <no@no.com> wrote in message
news:43***********************@news.sunsite.dk...
Am I did the following program corectly according to the question?

Question:

/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the European
market and wants a program that converts inches to
centimeters (1 inch = 2.54 cm). The company wants the
program set up so that it prompts the user to enter an inch
value. Your assignment is to define the program objectives and
to design the program (steps 1 and 2 of the programming process).
*/


I'm probably being overly literal here, but I note that the "question" doesn't
actually tell you to write or implement anything.

It asks you to

1. Define the program objectives
2. Design the program

In the business world (at least the several I'm familiar with) neither of these
involves actually writing any code. That would usually be:

3. Implement the design

- Bill
Jan 21 '06 #10
C_beginner a écrit :
#include<stdio.h>

#define centmeter 2.54
int main(void)
{
int inches_to_input;
float inches_to_output;
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
Don't use scanf() unless you are a level 3 C-guru...
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;
}


Do you have any reasons to obfuscate the code with such a lame naming ?

#include<stdio.h>
#include<stdlib.h>

#define CM_PER_IN 2.54
int main(void)
{
double inches;
double centimeters;
printf("please enter a number of inches:\n");
{
char s[16];
fgets(s, sizeof s, stdin);
inches = strtod(s, NULL);
}
centimeters = inches * CM_PER_IN;
printf("%.2f inches are converted into %.2f cm\n", inches, centimeters);
return 0;
}

A source program should have been read like a book...

--
A+

Emmanuel Delahaye
Jan 21 '06 #11
C_beginner wrote:

[help sniped...]
You should check the return value of scanf to find out if it
succeeded. If it fails because the user entered "one" then
inches_to_input would not be initialised so the program could
well output some random value.


Also told by Malcolm, is this the way that it has to be done?

if (!scanf("%d", &inches_to_input)) {
operations and calcuations
}


Readability blanks inserted in above quote.

No. Read the documentation for scanf. In this case the test would
be:

if (1 == scanf("%d", &inches_to_input)) {
operations and calcuations
}
else {
do something about bad input
}

See <http://www.dinkumware.com/refxc.html>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 21 '06 #12
C_beginner wrote
(in article <43***********************@news.sunsite.dk>):
Am I did the following program corectly according to the question?

Question:

/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the European
market and wants a program that converts inches to
centimeters (1 inch = 2.54 cm). The company wants the
program set up so that it prompts the user to enter an inch
value. Your assignment is to define the program objectives and
to design the program (steps 1 and 2 of the programming process).
*/
You do realize that posting the question in its entirety,
including the made up company name info, means that even a
moderately competent professor can detect your cheating in short
order? Or, does he/she allow you to get help from outside
people for your assignments?

Oh yes, that's probably why you are using the fake name. Well,
that can be tracked down too.

solution:

#include<stdio.h>

#define centmeter 2.54
int main(void)
{
int inches_to_input;
float inches_to_output;
printf("please enter a inches\n");
scanf("%d",&inches_to_input);
inches_to_output = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to_output);
return 0;
}


What happens when you run this program:

$ ./myprog
please enter a inches
I think my answer will be forty-two inches.

????

Also, what kind of language allows a phrase like "please enter a
inches" to make sense?

Also, you failed the easiest part of the assignment, to "define
the program objectives".
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw
How 'bout them Horns?

Jan 21 '06 #13
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote
Also, you failed the easiest part of the assignment, to "define
the program objectives".

That's the sort of thing that makes sense for real world programs, but not
really for little toy exercises.

Defining what the program is to do is often quite hard, and a bad
specification is a major cause of problems.
Jan 21 '06 #14
Randy Howard wrote:
C_beginner wrote
(in article <43***********************@news.sunsite.dk>):
Am I did the following program corectly according to the question?

Question:

<snip>
You do realize that posting the question in its entirety,
including the made up company name info, means that even a
moderately competent professor can detect your cheating in short
order? Or, does he/she allow you to get help from outside
people for your assignments?

Oh yes, that's probably why you are using the fake name. Well,
that can be tracked down too.


<snip>

I think you are being a bit hard on the OP. After all s/he had made a
serious attempt at the assignment and then asked if it was correct. It's
not like the times where someone just asks for the solution.

Also, back when I was in education you were allowed to ask for
assistance with your homework/assignments, and at university there were
even people around specifically to help.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 21 '06 #15
Malcolm wrote:
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote
Also, you failed the easiest part of the assignment, to "define
the program objectives".

That's the sort of thing that makes sense for real world programs, but not
really for little toy exercises.

Defining what the program is to do is often quite hard, and a bad
specification is a major cause of problems.


However, looking back, it is what the assignment asked for, so the OP
should attempt to right up the objectives and design, so Randy was
correct in pointing that out. Although assistance with those parts of
the assignment would not be topical here since they are generic software
development rather than C specific.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 21 '06 #16
Malcolm wrote
(in article
<dq**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com>):
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote
Also, you failed the easiest part of the assignment, to "define
the program objectives".
That's the sort of thing that makes sense for real world programs, but not
really for little toy exercises.


I suspect the prof asked for it to make a point. Perhaps about
understanding the objectives leads to better code, such as
allowing floating point input instead of integers only.
Defining what the program is to do is often quite hard, and a bad
specification is a major cause of problems.


Feeping Creaturism is much worse.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw
How 'bout them Horns?

Jan 22 '06 #17
Flash Gordon wrote
(in article <qe************@news.flash-gordon.me.uk>):
Randy Howard wrote:
C_beginner wrote
(in article <43***********************@news.sunsite.dk>):
Am I did the following program corectly according to the question?

Question:


<snip>
You do realize that posting the question in its entirety,
including the made up company name info, means that even a
moderately competent professor can detect your cheating in short
order? Or, does he/she allow you to get help from outside
people for your assignments?

Oh yes, that's probably why you are using the fake name. Well,
that can be tracked down too.


<snip>

I think you are being a bit hard on the OP. After all s/he had made a
serious attempt at the assignment and then asked if it was correct. It's
not like the times where someone just asks for the solution.


I explicitly asked if the guidelines allowed asking for outside
help. Yet, you are right, at least some code was offered up,
instead of "can you email the solution".
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw
How 'bout them Horns?

Jan 22 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
10
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.