473,789 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_outpu t;
printf("please enter a inches\n");
scanf("%d",&inc hes_to_input);
inches_to_outpu t = inches_to_input * centmeter;
printf("inches converted is %f\n",inches_to _output);
return 0;
}
Jan 21 '06 #1
17 3095
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_outpu t;
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",&inc hes_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_outpu t = 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_outpu t;
printf("please enter a inches\n");
scanf("%d",&inc hes_to_input);
inches_to_outpu t = 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_outpu t;
inches_to_outpu t = 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_outpu t = (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_outpu t;
Give these better names, like "inches" and "centimeter s".
printf("please enter a inches\n");
scanf("%d",&inc hes_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_outpu t = 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_inpu t))
{
operations and calcuations
}


Jan 21 '06 #9
"C_beginner " <no@no.com> wrote in message
news:43******** *************** @news.sunsite.d k...
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

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

Similar topics

5
3081
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 book which isn't aimed at people coming from C/Pascal/Java/Delpi/whatever... However, there seem to be plenty such books for all those other languages. Is there really no literature for people trying to learn programming by starting with C++? ...
8
2382
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
2945
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 in Nederlands, maar Engels is ook goed. Thnx, Rensjuh
27
4374
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 instead of a number, or give an error message? Also, how can I make the program verify there are two valid entries in txtBox1 and txtBox2 to then ENABLE the button operators (ie +, -, /, *).
18
2927
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? Are there better languages than c for a beginner? For instance visual basic or i should just keep the confidence of improving?
20
2298
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
2749
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
4472
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
2156
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 like python, ruby etc. I would like to know if there is a prerequisite to learning any computer language, is there something I have to learn before learning any computer language, like a basic or core?
22
18153
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.