CPSC1021 Lab 2- Vim, functions, memory allocation Solved

35.00 $

Category:

Description

Rate this product

During this lab you will:

  1. Practice using vim editor. We will use it every lab from now on.
  2. Write a program to calculate standard deviation.
  3. Compile and run a program that consists of several files.
  4. Create a zipped archive called “tarball” and submit it to canvas.

Part I – Learning how to use vim editor

Your TAs will conduct a short demo on how to use vim editor.  They will then assist you with it during the class. Here is a helpful tutorial: https://www.openvim.com/. And another useful link from Linux: https://www.linux.com/tutorials/vim101beginnersguidevim/.

 

Part II. Write a program to calculate standard deviation.

Creating necessary files  

Your program will consist of three files.

  1. Header file header file named h will contain your function prototype and the preprocessor       directives. These directives ensure the file does not get included twice by the       preprocessor.

 

#ifndef STDDEV_H

#define STDDEV_H

void stats (float *data, int n);

#endif

 

  1. Implementation file cpp file will contain function definition (implementation). This file will include your header file to let the compiler know where your function prototype can be found. The function will calculate the standard deviation and print the result. This is a void function and will not be returning any values to main.

 

Pseudocode for calculating standard deviation

  1. Find the mean (average) by dividing the sum of all elements of the array by the number (N) of elements in the array.

 

  1. Then for each of the N number in the array: subtract the mean from the number and square the result.

 

  1. Calculate the mean of the squared differences.

 

  1. Calculate the square root of the result from the previous computation.

 

Here is a link that explains how this works: https://www.mathsisfun.com/data/standarddeviationformulas.html  and the formula from the same website:

 

 

 

  1. Main driver the driver cpp will contain the main function. You will include your header file       here, but not the “stddev.cpp” file. Here you will do the following:

 

  • ask user to input the size of the array.

 

  • allocate space on the heap by using the C++ style operator new. (Do not forget to free up that memory at the end when you are done with it by using C++ operator delete. )

 

  • initialize the elements of the array to 4, 8, 12, 16, …. etc.

 

  • pass this array to the function stats that will calculate and print the standard deviation.

 

Compiling multiple files into an executable file To compile your program do the following:

g++ -c stddev.cpp -Wall -o stddev.o g++ -c main.cpp -Wall -o main.o g++ main.o stddev.o -Wall -o out

Please note that compiling with the flag -c tells the compiler to compile, but not link the files. This will create a binary object code. Object files have an extension .o. Flag -o allows you to rename the default filenames created by compiler. -Wall enables warnings for many common errors and should always be used.

 

Also, please note that the header file is not compiled separately. It will be included into the .cpp file by the preprocessor and compiled when you compile the .cpp file.

  • lab2-uclskc.zip