C: Assignments Day 3

Today we have three problems for you to tackle.

Problem 1: Lets switch to C++ syntax

Navigate to /code/c++/ExerciseDay3/ex1/ to find the code for this C++ exerise. This code is the solution to yesterdays last assignment. It compiles with both a C and a C++ compiler. The task is to replace the standard C lib functions for memory and output, e.g functions like malloc(), with the standard C++ functions, e.g. new().

 1
 2// program to read values from a file, each file a csv list of int and two double
 3// written: fmk
 4//
 5// code is written using C language, rewrite using C++ language
 6//
 7
 8#include <stdio.h>
 9#include <stdlib.h>
10
11int main(int argc, char **argv) {
12
13  if (argc != 2) {
14    fprintf(stdout, "ERROR correct usage appName inputFile\n");
15    return -1;
16  }
17  
18  FILE *filePtr = fopen(argv[1],"r"); 
19
20  int i = 0;
21  float float1, float2;
22  int maxVectorSize = 100;
23  double *vector1 = (double *)malloc(maxVectorSize*sizeof(double));
24  double *vector2 = (double *)malloc(maxVectorSize*sizeof(double));  
25  int vectorSize = 0;
26  
27  while (fscanf(filePtr,"%d, %f, %f\n", &i, &float1, &float2) != EOF) {
28    vector1[vectorSize] = float1;
29    vector2[vectorSize] = float2;
30    printf("%d, %f, %f\n",i, vector2[i], vector1[i]);
31    vectorSize++;
32
33    if (vectorSize == maxVectorSize) {
34      double *tmp = (double *)malloc(maxVectorSize*2*sizeof(double));
35      for (int i=0; i<maxVectorSize; i++)
36         tmp[i] = vector1[i];
37      free(vector1);
38      vector1 = tmp;
39
40      tmp = (double *)malloc(maxVectorSize*2*sizeof(double));
41      for (int i=0; i<maxVectorSize; i++)
42         tmp[i] = vector2[i];
43      free(vector2);
44      vector2 =  tmp;      
45
46      maxVectorSize *= 2;
47    }
48  }
49
50  fclose(filePtr);  
51}

Note

There is no CmakeLists.txt file. To compile you must invoke the C++ compiler directly, which is invoked from the terminal using the following to invoke the Intel C++ compiler:

$ icpc file3.cpp

or the following to invoke the GNU C++ compiler

$ g++ file3.cpp

Problem 2: Adding a ColoredShape

Navigate to /code/c++/ExerciseDay3/ex2/ to find the code for the C++ example presented in todays video. You are asked to create another shape of your choosing and add to the code base. A similar example was perfomed during the video.

Hint

When starting a new C++ class it is usual for developers to start by copying the .h and .cpp file of an existing working class that is similar to have the name of the new class they want. Then they open the new class and make isssue a global replacement to replace old class name with new name. This way they start with a working class. Then they would go into new class and start making modifications.

$ cp workingClass.h newClass.h
$ cp workingClass.cpp newClass.cpp
$ emacs newClass.h
emacs> <esc>%
emacs> (asks for string to replace:>)  oldClass <enter>
emacs> (asks for replacement string:>)  newClass <enter>
emacs> !        <-- to replace all quickly
emacs> // make other changes as needed
emacs> <CTRL> XS
emacs> <CTRL> XF
emacs> newClass.cpp
emacs> <esc>%
$ (asks for string to replace:>)  oldClass <enter>
$ (asks for replacement string:>)  newClass <enter>
$ !        <-- to replace all quickly
$ // make other changes as needed
emacs> <CTRL> XF
emacs> <CTRL> XC

Note

A CMakeLists.txt file has been provided. Add your file to it when created. The cmake command will check your system for compilers and other development tool and create a Makefile in each source folder. The make will build the executable(s). For subsequent builds, you only have to call cmake again if you change anything in the CMakeFilesList.txt file. Placing the compile files into a build folder makes cleanup easier: simply delete the entire build folder when done. It can be regenerated easily using the procedure below.

The build process again is:

$ mkdir build
$ cd build
$ cmake ..
$ make

Problem 3: An engineering Vector Class requires you to finish off

Navigate to /code/c++/ExerciseDay3/ex3/ to find the partially completed code for a C++ Vector class. You are to finish it and test it. The directory contains a header file Vector.h, an implementation file Vector.cpp, a main.cpp to test with and a CMakeFilesList.txt file for building and compiling.

The header file Vector.h:

 1#ifndef _VECTOR
 2#define _VECTOR
 3
 4class Vector 
 5{
 6 public:
 7  Vector(int size); 
 8  ~Vector();
 9
10  // some methods
11  int Size(void) const;
12  void zero(void);
13  double norm(void) const;
14  double dot(const Vector &other) const;
15  void print(void);
16
17  // overload some operators to look Matlabish
18  Vector operator+(const Vector &other) const;
19  void operator=(const Vector &other);
20  void operator+=(double val);
21  void operator+=(const Vector &other);
22
23  double operator()(int x) const;
24  double &operator()(int x);
25
26 private:
27  double *data;
28  int size;
29};
30
31
32#endif

The implementation file Vector.cpp:

  1#include <iostream>
  2#include <cmath>
  3#include "Vector.h"
  4
  5Vector::Vector(int sz)
  6{
  7  size = 0;
  8  data = 0; // PROVIDE CODE
  9}
 10
 11Vector::~Vector()
 12{
 13  if (data != 0)
 14    ; // PROVIDE CODE
 15}
 16
 17// some methods
 18int 
 19Vector::Size(void) const
 20{
 21  return size;
 22}
 23
 24void 
 25Vector::zero(void)
 26{
 27  for (int i=0; i<size; i++)
 28    data[i] = 0;
 29}
 30
 31double 
 32Vector::norm(void) const
 33{
 34  // PROVIDE CODE
 35}
 36
 37double 
 38Vector::dot(const Vector &other) const
 39{
 40  double result = 0;
 41  
 42  if (other.size != size) {
 43    std::cerr << "Vector::dot ERROR vectors not of same size, returning 0\n";
 44    return result;
 45  }
 46  
 47  // PROVIDE CODE
 48
 49  
 50  return result;
 51}
 52
 53void 
 54Vector::print(void)
 55{
 56  for (int i=0; i<size; i++)
 57    std::cerr << " " << data[i];
 58  std::cerr << "\n";
 59}
 60
 61// overload some operators to look Matlabish
 62Vector 
 63Vector::operator+(const Vector &other) const
 64{
 65  Vector result(size);
 66
 67  if (other.size != size) {
 68    std::cerr << "Vector::operator ERROR vectors not of same size, returning empty vector\n";
 69    return result;
 70  }
 71
 72  // PROVIDE CODE
 73
 74  return result;
 75}
 76
 77void 
 78Vector::operator=(const Vector &other)
 79{
 80  // PROVIDE CODE
 81}
 82
 83void 
 84Vector::operator+=(double val)
 85{
 86  // PROVIDE CODE
 87}
 88
 89void 
 90Vector::operator+=(const Vector &other)
 91{
 92  // PROVIDE CODE
 93}
 94
 95double 
 96Vector::operator()(int x) const
 97{
 98  // PROVIDE CODE
 99}
100
101double &
102Vector::operator()(int x)
103{
104  if (x < 0 || x >= size) {
105    static double errorResult = 0;
106    std::cerr << "Vector::operator() " << x << " outside range 0 through " << size-1 << "\n";
107    return errorResult;
108  }
109  // PROVIDE CODE
110}
111

Hint

Wherever you see a comment //PROVIDE CODE is a place you need to edit, e.g. line 8 requires some assignment using a new double[some value].