Beginner excersice #1

C

C_beginner

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

Kies Lee

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?
 
F

Flash Gordon

Kies said:
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
 
F

Flash Gordon

C_beginner said:
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 said:
#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/
 
T

thomas.mertes

C_beginner said:
#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
 
N

nobody

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
 
M

Malcolm

C_beginner said:
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
 
C

C_beginner

[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
}
 
C

C_beginner

Thanks for all the help. I learned little bit about promotion rank,
conversion operator.
 
W

William J. Leary Jr.

C_beginner said:
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
 
E

Emmanuel Delahaye

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

CBFalconer

C_beginner said:
[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/>
 
R

Randy Howard

C_beginner wrote
(in article said:
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".
 
M

Malcolm

Randy Howard said:
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.
 
F

Flash Gordon

Randy said:
C_beginner wrote

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

Flash Gordon

Malcolm said:
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.
 
R

Randy Howard

Malcolm wrote
(in article
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.
 
R

Randy Howard

Flash Gordon wrote
(in article said:
<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".
 

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
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top