Starting from:

$30

ENSF337-Lab 1 Solved


•       If you haven't already done so, make a folder called ENSF337 on your computer.  

•       Within your ENSF337 folder, make another folder called lab1.

•       Start up your favorite text editor, and type in all the following C code:

•       Now save your file as: lab1exe_A.c

•       In Cygwin Terminal (or mac terminal) navigate to your working directory (the same directory that you saved your lab1exe_A.c),then enter the command:
gcc -Wall lab1exe_A.c 

 

If the command succeeds, an executable file called a.exe will have been created. If the command fails -- which will be indicated by one or more error messages--go back to your editor and fix the code that you have typed incorrectly and try gcc again.

• Once you have an executable, run it a few times by using the command

over and over. Try different inputs each time; see what happens if you enter letters or punctuation instead of numbers.

 Notes:   

•       You don't need to type ./a.exe over and over! You can use the up-arrow key on your keyboard to retrieve previously entered commands.

•       For those who use a Mac computer terminal, the default executable file created automatically is called a.out instead of a.exe.

 Exercise B - Variables and Basic Arithmetic 
Recall that variables are used to store values in a program.  However, before variables can be used, they must first be created (declared/defined).  This exercise will get you exposed to variable creation as well as basic arithmetic.


Copy the following incomplete C program into your favourite text editor or IDE:

#include <stdio.h> int main() { 

   double num1 = -34.5;    double num2 = 98.7; 

   double sum;          // sum of num1 and num2    double sumSquared;   // the square of num2 plus num2 

   // 1) Add the two numbers and store the result in the variable 'sum'  

   // 2) Compute the square of the sum and store the result in the variable 'sumSquared' 

   //    Use the variable 'sum' (computed above) for this computation 
    
   printf( "The sum squared is: %f \n", sumSquared); 

   // 3) Now double the sum squared value and store the result in 'sumSquared' 

  printf( "The sum squared is now: %f \n",  sumSquared);     return 0; 

This code contains most of a program to perform various numerical calculations. What you need to do is:  
•       At the point labelled 1), write the code to add the values of num1 and num2 and to store the result in the variable sum.

•       At the point labelled 2), write the code to compute the square of the sum of num1 and num2 and to store the result in the variable sumSquared.  Use the value of the variable sum computed in the previous step.

•       At the point labelled 3), double the value of the sumSquared variable and store the result in sumSquared.

•       Compile your program.

•       Run the program and record the output.

 Exercise C. Operator Precedence 
In the lectures, you were introduced to operator precedence in C. In this Task, you are asked to predict the value of the following expressions by using the proper operator precedence.  First, assume that:

 double z = 0; double x = 2.5; double y = -1.5; int m = 18; int n = 4; 

Now, what are the values of z in the following expressions? Show your work.

a)    z = x + n * y – (x + n) * y; 

b)    z = m / n + m % n; 

c)    z = n / m + n % m; 

d)    z = 5 * x – n / 5; 

e)    z = 1 – ( 1 – ( 1 – ( 1 – ( 1 – n)))); 

f)     z = sqrt(sqrt((double)n);  

Exercise D: Mathematical Expressions 
 write a program to read in an angle in units of radians and compute the sine of the angle.  However, in addition to using the built-in sine function, you will also compute the Taylor series approximation, which is given by

x3 x5 x7 x9

                                                                  sin(x)= − + − + −x          

                                                                                                       3!     5!     7!     9!
The pseudo-code for your program is as follows:
•       Prompt for input angle in units of radians.

•       Read input angle in units of radians.

•       Compute and output the sine of the input angle using built-in trigonometric functions.

•       Compute and output the Taylor series approximation of sin(x) including terms up to order seven (i.e., x7 ).   

Write your source code in a file called lab1_exe_D.c and compile it into a program called sine.  Remember, in order to use the built-in sin(x) or pow x y( , ) functions, you must include the math.h file.  To get you started you can use the following template

 #include <stdio.h> 

#include <math.h> 

  int main() 

   // Develop the body of the program here 

  return 0; 

Once you have written and compiled your code, test your program by running it and recording the outputs for following input values

a.     0 radians (0 degrees)

b.     0.5 radians (approximately 28.65 degrees)

c.     1.0 radians (approximately 57.30 degrees)

d.     2.5 radians (approximately 143.24 degrees)

 Exercise E: Problem Solving
Probem Statement 
 You have been given the problem of writing an algorithm to solve quadratic equations.  The input to the algorithm is the three coefficients,         ,            and       of the quadratic equation to be solved

 Then, the solutions to the quadratic equations are given by

The algorithm should be able to output both real and complex results (i.e., those results which require taking the square root of negative number;  ).  As an example of a complex result, consider the following equation:

                                                                          x2 + 2x+1= 0
Which its solutions are:
Note, however, that a computer cannot compute the square root of a negative number.  If this is required, your algorithm should compute the square root of the absolute value of the number and prefix the output (scaled by the denominator) with an “ ”.  Mathematically, this is equivalent to writing the solution to the quadratic


Write your source code in a file called lab1_exe_E.c and compile it into a program called quadratic.  
Remember, in order to use the built-in math functions, you must include the math.h file.  To get you started you can use the following template

#include <stdio.h> 

#include <math.h>

int main() 

   // Develop the body of the program here 

return 

More products