Copy the following code to a file on your Omega account. Compile the code. There
ID: 3666619 • Letter: C
Question
Copy the following code to a file on your Omega account. Compile the code. There are error(s). What are they? (record the command line error message) Perform the preprocess step of compilation by executing the following command. (Replace p1. with the file name you used.) Page down until the end of the file where the main function is. Record the main function in your submission. What is different from the original code? When you review the updated code before it is sent to the next step of the compiler, can you find the syntax errors in the last printf statement? If so, what are they? What does that tell you about the define directives associated with the constants? In the original .c file, correct the error and compile and execute the code. Record the corrected code and the execution test case per submission guidelines. What are the two types of preprocessor directives and what are they used for? What are the four compiler steps (see Compiler section in C_notcs.docx)? Create a program to define constant that converts velocity in m/s to ft/s. Use the ratio I m/s = 3.280839895013 ft/s. Call the constant SItoEnglish. Create a second constant to convert m/s to miles per hour. Call this constant SItoMPH. Use the constants in a program to convert the following values from metric to English units. Print each to the screen in the following format,Explanation / Answer
i) Writing code in a editor
//Modified code
#include<stdio.h>
#include<conio.h>
#define CONV 2.54
#define MY_VALUE 123.12345678901234567899012345
//Remove the assignment operator
#define PI 3.1459
#define PIby2 PI/2
int main()
{
//declare variables
float inches=10;
float cm;
//perform output
printf("%f inches is %f centimeters. ",inches,inches*CONV);
printf("MY_VALU = %f ",MY_VALUE);
//another example
printf("%f divided by %d is : %f ",PI,2,PIby2/2);
getch();
return 0;
}
------------------------------------------------------------------------------------------
ii) The errors that found during the compilation of program
'=' : unexpected in macro definition
The macro's
#define PI=3.1459
#define PIby2 =PI/2
contains errors the assignment operator '=' followed by constat
name is causing error.
------------------------------------------------------------------------------------------
iii) , v) are not done since ours is windows platform
Run the programes in Linux/Unix platform shell.
------------------------------------------------------------------------------------------
vi)Remove the assignment operators from
the macor defitions of PI and PIby2
#define PI 3.1459
#define PIby2 PI/2
------------------------------------------------------------------------------------------
vii) Corrected code
//Modified code
#include<stdio.h>
#include<conio.h>
#define CONV 2.54
#define MY_VALUE 123.12345678901234567899012345
//Remove the assignment operator
#define PI 3.1459
#define PIby2 PI/2
int main()
{
//declare variables
float inches=10;
float cm;
//perform output
printf("%f inches is %f centimeters. ",inches,inches*CONV);
printf("MY_VALU = %f ",MY_VALUE);
//another example
printf("%f divided by %d is : %f ",PI,2,PIby2/2);
getch();
return 0;
}
sample output:
10.000000 inches is 25.400000 centimeters.
MY_VALU = 123.123457
3.145900 divided by 2 is : 0.786475
------------------------------------------------------------------------------------------
b.
The preprocessor directives are executed and set values before
start of the main method .
The most useful preprocessor directive is #include directive
This preprocessor directive is used to incllude the header file
into the current the program .
#include<stdio.h>
The statement when compilation the program includes all methods
into the current program .
Other preprocessor directive is #define.
The #define preprocessor directive is used to set constant values
in the program to access in the entire program.
For example,
#difine PI 3.1459
------------------------------------------------------------------------------------------
c. The four phases of the c -program compilation
1. Preprocessing
2.Compilation
3.Assembly
4.Linking
------------------------------------------------------------------------------------------
d.
//C program that converts meter per second to feet per second and miles per hour
#include<stdio.h>
#include<conio.h>
//set constant to convert m/s to feet/sec
#define SItoEnglish 3.280839895013
//set constant to convert m/s to Miles per hour
#define SItoMPH 2.23694
int main()
{
//declare variables
//set velocity in m/s
float velocity=5;
//perform output
printf("%6.2f m/s %6.2f ft/s %6.2f MPH ",velocity,velocity*SItoEnglish,velocity*SItoMPH);
getch();
return 0;
}
sample output:
5.00 m/s 16.40 ft/s 11.18 MPH