[SOLVED] ENSF337 -  Programming Fundamentals for Software and Computer  - Lab 1

30.00 $

Category:

Description

5/5 - (5 votes)

Objectives:

 

This is much smaller lab assignment than most of the future labs. It contains several simple exercises, that are designed to help you to:

  • Get familiar with the C program development environment, compiling running a C program Review some of the very basic programming features in C.

 

Please heed this advice:

.

  • When writing code (C or C++) make sure the following information appears at the top of your code:
    • File Name
    • Assignment and exercise number o Lab section  o   Your name o

 

Here is an example:

/*

  • File Name: lab1exe_B.c
  • Assignment: Lab 1 Exercise B
  • Lab section: Your Lab Section (B01, B02, B03, or B04)
  • Completed by: Your Name (plus your partner(s) name for group exercises)

*/

 

  • Some exercises in this lab and future labs will not be marked. Please do not skip them, because these exercises are as important as the others in learning the course material.
  • In courses like ENSF 337 some students skip directly to the exercises that involve writing code, skipping the sections such as “Read This First”, or postponing the diagram-drawing until later. That’s a bad idea for several reasons:
    • Read This First” sections normally explain some of technical or syntax details that may help you to solve the problem or may provide you with some hints.
    • Drawing diagrams is an important part of learning how to visualize memory used in C and C++ programs. If you do diagram-drawing exercises at the last minute, you won’t learn the material very well. If you do the diagrams first, you may find it easier to understand the code-writing exercises, so you may be able to finish them more quickly.

 

 

Exercise A: Creating a C source file, building and running an executable

 

Read This First:

 

There are no marks for this exercise, but if this is your first attempt to develop a C program, please do it so that you start to become comfortable with program development in C and particularly using your favorite development environment such as (Cygwin + Notepad++), Geany, or Xcode. Before starting this exercise, please read the slides on “Getting Started”.

 

What to Do:

  • 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: c
  • In Cygwin Terminal (or mac terminal) navigate to your working directory (the same directory that you saved your 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

./a.exe

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 out instead of a.exe.

 

Exercise B – Variables and Basic Arithmetic (4 marks)

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.

What to Do:

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 (6 marks)

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.

  1. z = x + n * y – (x + n) * y;
  2. z = m / n + m % n;
  3. z = n / m + n % m;
  4. z = 5 * x – n / 5;
  5. z = 1 – ( 1 – ( 1 – ( 1 – ( 1 – n))));
  6. z = sqrt(sqrt((double)n);

 

 

Exercise D: Mathematical Expressions (5 marks)

 

For this task, 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

  1. 0 radians (0 degrees)
  2. 5 radians (approximately 28.65 degrees)
  3. 0 radians (approximately 57.30 degrees)
  4. 5 radians (approximately 143.24 degrees)

 

Exercise E: Problem Solving (10 marks)

Problem 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 equation as