PullMonkey Blog


14 Apr

ECE231 – Spring 2009 – Programming Assignment 4


This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are only going to be doing input and output. So the only header file we need is the iostream library.

1
2
3
4

#include <iostream>
using namespace std;

Defining the Node Class

For the node class we need 5 private variables: type, value, node1, node2, next, a constructor, 5 member functions, and a friend class.

Because most of the functions only return a single variable it would be a waste to have the functions outside the class so I put them all inside the class.

So here is what your class should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

class node
{
  protected:
    int type;     // type of component
    double value; // value of the component
    int node1;    // first node of the component
    int node2;    // second node of the component
    node *next;   // pointer to the next node in list
  public:
    node(int a, double b, int c, int d)
    {
      type  = a;
      value = b;
      node1 = c;
      node2 = d;
    }

    int getType() { return type; }
    double getValue() { return value; }
    int getNode1() { return node1; }
    int getNode2() { return node2; }
    node *getNext() { return next; }

    friend class circuit;
};

Defining the Circuit Class

For the circuit class we need 2 private variables: first and last, a constructor, a copy constructor, 6 member functions, and a friend function.

For these I will put most of the functions on the outside of the class.

So here is what your class should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class circuit
{
  protected:
    node *first; // pointer to first node in list
    node *last;  // pointer to last node in list
    node *getNode(int & , double & , int & , int & );
  public:
    circuit();
    circuit(const circuit & );
    ~circuit();

    void insert(int , double , int , int );
    void insertInFront(node * );
    void insertInBack(node * , node * );
    void insertInMiddle(node * , node * , node * );
    bool remove(int , int );
    bool isEmpty();

    friend ostream & operator << (ostream & , circuit );
};

Writing the Actual Functions for the Circuit Class

First, we have the private function getNode. This function creates a new member of the node class and returns the address to it.

1
2
3
4
5
6
7
8

node* circuit::getNode(int &t, double &v, int &n1, int &n2)
{
  node *temp = new node(t, v, n1, n2);
  assert( temp != 0 );
  return temp;
}

Second, we have the a constructor, a copy constructor, and a class destructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

circuit::circuit()
{
  first = last = 0;
}
circuit::circuit(const circuit &c)
{
  first = last = 0;
  node *nc = c.first;
  while(nc != 0)
  {
    insert(nc->type, nc->value, nc->node1, nc->node2);
    nc = nc->next;
  }
}
circuit::~circuit()
{
  if( !isEmpty() )
  {
    node *nc = first;
    node *temp;
    while(nc != 0)
    {
      temp = nc;
      nc = nc->next;
      delete temp;
    }
  }
}

Next, we have the 6 member functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

void circuit::insert(int t, double v, int n1, int n2)
{
  node *newNode = getNode(t, v, n1, n2);
  node *nc = first;
  node *nc_next = first;
  if(isEmpty())
  {
    first = last = newNode;
    newNode->next = 0;
  }
  else if(first == last)
  {
    if(newNode->node1 < first->node1)
      insertInFront(newNode);
    else if(newNode->node1 > first->node1)
      insertInBack(newNode, first);
    else // if newNode->node1 == first->node1
    {
      if(newNode->node2 < first->node2)
        insertInFront(newNode);
      else
        insertInBack(newNode, first);
    }
  }
  else
  {
    while(nc->next != 0)
    {
      if(newNode->node1 < nc_next->node1)
      {
        if(nc_next == first)
          insertInFront(newNode);
        else
          insertInMiddle(newNode, nc, nc_next);
        break;
      }
      if(newNode->node1 == nc_next->node1)
      {
        if(newNode->node2 <= nc_next->node2)
        {
          if(nc_next == first)
            insertInFront(newNode);
          else
            insertInMiddle(newNode, nc, nc_next);
          break;
        }
      }
      if(newNode->node1 >= nc_next->node1 and nc_next == last)
      {
        insertInBack(newNode, nc_next);
        break;
      }
      nc = nc_next;
      nc_next = nc->next;
    }
  }
}
void circuit::insertInFront(node *n)
{
  n->next = first;
  first = n;
}
void circuit::insertInBack(node *n, node *p)
{
  p->next = last = n;
  n->next = 0;
}
void circuit::insertInMiddle(node *cn, node *p , node *n)
{
  p->next = cn;
  cn->next = n;
}
bool circuit::remove(int n1, int n2)
{
  node *nc = first;
  node *nc_next = first;
  node *temp;
  if( isEmpty() )
    return false;
  while (nc_next != 0)
  {
    if(nc_next != 0 and nc_next->node1 == n1 and nc_next->node2 == n2)
    {
      temp = nc_next;
      nc->next = nc_next->next;
      if(first == last)
        first = last = 0;
      if(nc_next == first)
        first = temp->next;
      if (nc_next == last)
        last = nc;
      delete nc_next;
      return true;
    }
    nc = nc_next;
    nc_next = nc->next;
  }
  return false;
}
bool circuit::isEmpty()
{
  return ( first == 0 and last == 0 );
}

Last, we have a friend function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

ostream & operator << (ostream &os, circuit c)
{
  node *nc = c.first; // node count variable used for looping through list
  int lcv = 1; // loop control variable
  cout << "First: " << c.first << "  Last: " << c.last << endl;
  while (nc != 0)
  {
    os << lcv << ". "
       << "node1: "   << nc->getNode1() 
       << "  node2: " << nc->getNode2()
       << "  type: "  << nc->getType() 
       << "  value: " << nc->getValue() 
       << "  next: "  << nc->getNext() 
       << endl;
    nc = nc->getNext();
    lcv++;
  }
  if(c.first == 0)
    os << "List is empty" << endl;
  return os;
}

Testing your program

Our teacher gave us a test program, however I expanded it a little bit to test a bit more fully.

So here is my test program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#include <iostream>
#include "yourprogramname.h"
using namespace std;

const int M=7;
const int R=2;

int main()
{
  circuit c, c2, c3;
  int d[M*3] = {1,1,4, 1,1,3, 2,2,3, 1,1,2, 1,5,6, 1,2,5, 3,1,3};
  double v[M] = {10.5, 30.7, 100.4, 20.6, 35.5, 45.9, 60.8};
  int d1[R*8] = {1,2, 5,6, 3,6, 1,4, 1,3, 2,5, 2,3, 1,3};
  int i, j;

  for( i=0,j=0; i<M; i++,j=j+3 )
  {
    cout << i+1 << endl;
    cout << "Inserting new node into list with:" << endl;
    cout << "node1: "   << d[j+1]
         << "  node2: " << d[j+2]
         << "  type: "  << d[j]
         << "  value: " << v[i]
         << endl;
    c.insert( d[j], v[i], d[j+1], d[j+2] );
    cout << "List: " << endl;
    cout << c << endl << endl;
  }
  for( i=0; i<R*8; i=i+2 )
  {
    cout << "Removing node with:" << endl;
    cout << "node1: "   << d1[i]
         << "  node2: " << d1[i+1]
         << endl;
    if( c.remove( d1[i], d1[i+1] ) == false )
      cout << "Node not found" << endl;
    cout << "List: " << endl;
    cout << c << endl << endl;
  }

  cout << "nnMaking List #2:" << endl;
  int d2[M*3] = {2,2,4, 1,1,3, 2,2,3, 1,1,2, 1,5,6, 1,2,5, 3,1,3};
  for( i=0,j=0; i<M; i++,j=j+3 )
  {
    cout << i+1 << endl;
    cout << "Inserting new node into list with:" << endl;
    cout << "node1: "   << d2[j+1]
         << "  node2: " << d2[j+2]
         << "  type: "  << d2[j]
         << "  value: " << v[i]
         << endl;
    c2.insert( d2[j], v[i], d2[j+1], d2[j+2] );
    cout << "List: " << endl;
    cout << c2 << endl << endl;
  }

  cout << "nnMaking List #3:" << endl;
  int d3[M*3] = {2,2,4, 3,3,3, 2,2,3, 1,1,2, 1,5,6, 1,2,5, 3,1,3};
  for( i=0,j=0; i<M; i++,j=j+3 )
  {
    cout << i+1 << endl;
    cout << "Inserting new node into list with:" << endl;
    cout << "node1: "   << d3[j+1]
         << "  node2: " << d3[j+2]
         << "  type: "  << d3[j]
         << "  value: " << v[i]
         << endl;
    c3.insert( d3[j], v[i], d3[j+1], d3[j+2] );
    cout << "List: " << endl;
    cout << c3 << endl << endl;
  }

  return 0;
}


Comments Off on ECE231 – Spring 2009 – Programming Assignment 4 Filed under: C / C++, development, Home, projects, tutorials Tags: , ,
26 Mar

ECE231 – Spring 2009 – Programming Assignment 3


This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.

1
2
3
4
5
6
7
8
9

#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.141592654
or
const double PI = 3.141592654;

Defining the Complexn Class

For the complexn class we need 2 private variables r and i, 3 constructors, a copy constructor, 4 member functions, 9 overloaded opperators, and 2 friend functions.

Like the previous program we will define everything with in the class and have the actual functions at the bottom.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

class complexn
{
  private:
    double r; // real part of the complex number
    double i; // imaginary part of the complex number
  public:
    complexn();
    complexn(double );
    complexn(double , double );
    complexn(const complexn & );

    double complexabs();
    double complexangle();
    complexn complexconj();
    double distance(const complexn & );

    complexn operator + (const complexn & );
    complexn operator - (const complexn & );
    complexn operator * (const complexn & );
    complexn operator / (const complexn & );
    complexn operator = (const complexn & );

    complexn operator ++ ();
    complexn operator ++ (int );
    complexn operator -- ();
    complexn operator -- (int );

    friend ostream & operator << (ostream & , const complexn & );
    friend istream & operator >> (istream & , complexn & );
};

Writing the Actual Functions for the Complexn Class

First we have the 3 constructors and the copy constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// takes no arguments and sets r and i to 0.0
complexn::complexn()
{
  r = i = 0.0;
}
// takes 1 argument and sets r to the argument and i to 0.0
complexn::complexn(double real)
{
  r = real;
  i = 0.0;
}
// takes 2 arguments and sets r to the first and i to the second
complexn::complexn(double real, double imag)
{
  r = real;
  i = imag;
}
// this is a copy constructor that dereferences the complexn variable if it is referenced
complexn::complexn(const complexn &c)
{
  r = c.r;
  i = c.i;
}

Next, we have the 3 member functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// takes no arguments and returns the distance from the 0 as a double
double complexn::complexabs()
{
  return sqrt(pow(r,2.0) + pow(i,2.0));
}
// takes no arguments and returns the angle of the complex point as a double in radians
double complexn::complexangle()
{
  double angle; // angle of coordinate from positive x axis
  angle = atan(r / i);
  if(r < 0 and i >= 0)
    return PI - abs(angle);
  if(r < 0 and i < 0)
    return -(PI - abs(angle));
  return angle;
}
// takes no arguments and returns the conjugate of the complex number as a complexn class
complexn complexn::complexconj()
{
  complexn temp = *this; // complex number used for calculations
  temp.i *= -1;
  return temp;
}
// takes one complexn type argument and returns the distance between the argument and the current instance
double complexn::distance(const complexn &temp)
{
  return sqrt(pow(temp.r -r, 2.0) + pow(temp.i - i, 2.0));
}

Next, we have the 9 overloaded operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

complexn complexn::operator + (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r + temp.r;
  cn.i = i + temp.i;
  return cn;
}
complexn complexn::operator - (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r - temp.r;
  cn.i = i - temp.i;
  return cn;
}
complexn complexn::operator * (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r * temp.r - i * temp.i;
  cn.i = r * temp.i + i * temp.r;
  return cn;
}
complexn complexn::operator / (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = (r * temp.r + i * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  cn.i = (i * temp.r - r * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  return cn;
}
complexn complexn::operator = (const complexn &temp)
{
  r = temp.r;
  i = temp.i;
  return *this;
}
complexn complexn::operator ++ ()
{
  r += 1;
  return *this;
}
complexn complexn::operator ++ (int x)
{
  complexn temp = *this;
  r += 1;
  return temp;
}
complexn complexn::operator -- ()
{
  r -= 1;
  return *this;
}
complexn complexn::operator -- (int x)
{
  complexn temp = *this;
  r -= 1;
  return temp;
}

Last, we have the 2 friend functions:

1
2
3
4
5
6
7
8
9
10
11
12

ostream & operator << (ostream &os, const complexn &temp)
{
  os << temp.r << " + " << temp.i << "i";
  return os;
}
istream & operator >> (istream &is, complexn &temp)
{
  is >> temp.r >> temp.i;
  return is;
}

Testing Your program

I wrote a fairly good program for testing this complexn class.

To test your program copy your program to [name].h file and in the main.cpp file change the header file at the top to match your file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#include <iostream>
#include "yourprogram.h" // the name of your comlexn header file goes here

using namespace std;

int main()
{
  complexn c1;
  complexn c2(1);
  complexn c3(3, -5);
  complexn c4, c5, c6;

  cout << "Complex Numbers: " << endl;
  cout << "  C1: " << c1 << endl;
  cout << "  C2: " << c2 << endl;
  cout << "  C3: " << c3 << endl;

  cout << "nEnter values for C1: ";
  cin >> c1;
  cout << "  You enetered: " << c1 << endl;
  cout << "Enter values for C2: ";
  cin >> c2;
  cout << "  You enetered: " << c2 << endl;
  cout << "Enter values for C3: ";
  cin >> c3;
  cout << "  You enetered: " << c3 << endl;

  cout << "nDistances from origin: " << endl;
  cout << "  C1(" << c1 << "): " << c1.complexabs() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexabs() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexabs() << endl;

  cout << "nConjugates:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexconj() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexconj() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexconj() << endl;

  cout << "nDistances between:" << endl;
  cout << "  C1(" << c1 << ") and C2(" << c2 << "): " << c1.distance(c2) << endl;
  cout << "  C1(" << c1 << ") and C3(" << c3 << "): " << c1.distance(c3) << endl;
  cout << "  C2(" << c2 << ") and C3(" << c3 << "): " << c2.distance(c3) << endl;

  cout << "nAddition:" << endl;
  cout << "  C1(" << c1 << ") + C2(" << c2 << "): " << c1 + c2 << endl;
  cout << "  C1(" << c1 << ") + C3(" << c3 << "): " << c1 + c3 << endl;
  cout << "  C2(" << c2 << ") + C3(" << c3 << "): " << c2 + c3 << endl;

  cout << "nSubtraction:" << endl;
  cout << "  C1(" << c1 << ") - C2(" << c2 << "): " << c1 - c2 << endl;
  cout << "  C1(" << c1 << ") - C3(" << c3 << "): " << c1 - c3 << endl;
  cout << "  C2(" << c2 << ") - C3(" << c3 << "): " << c2 - c3 << endl;

  cout << "nMultiplication:" << endl;
  cout << "  C1(" << c1 << ") * C2(" << c2 << "): " << c1 * c2 << endl;
  cout << "  C1(" << c1 << ") * C3(" << c3 << "): " << c1 * c3 << endl;
  cout << "  C2(" << c2 << ") * C3(" << c3 << "): " << c2 * c3 << endl;

  cout << "nDivision:" << endl;
  cout << "  C1(" << c1 << ") / C2(" << c2 << "): " << c1 / c2 << endl;
  cout << "  C1(" << c1 << ") / C3(" << c3 << "): " << c1 / c3 << endl;
  cout << "  C2(" << c2 << ") / C3(" << c3 << "): " << c2 / c3 << endl;

  cout << "nIncrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1++: " << c1++ << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  ++C1: " << ++c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2++: " << c2++ << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  ++C2: " << ++c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3++: " << c3++ << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  ++C3: " << ++c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  cout << "nDecrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1--: " << c1-- << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  --C1: " << --c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2--: " << c2-- << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  --C2: " << --c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3--: " << c3-- << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  --C3: " << --c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  c4 = c1;
  c5 = c2;
  c6 = c3;

  cout << "nEquals:" << endl;
  cout << "  C4 = C1(" << c1 << "): " << c4 << endl;
  cout << "  C5 = C2(" << c2 << "): " << c5 << endl;
  cout << "  C6 = C3(" << c3 << "): " << c6 << endl;

  c1 = complexn ( 1, 1);
  c2 = complexn (-1, 1);
  c3 = complexn (-1,-1);
  c4 = complexn ( 1,-1);
  c5 = complexn ( 0, 1);
  c6 = complexn (-1, 0);
  cout << "nAngles:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexangle() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexangle() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexangle() << endl;
  cout << "  C4(" << c4 << "): " << c4.complexangle() << endl;
  cout << "  C5(" << c5 << "): " << c5.complexangle() << endl;
  cout << "  C6(" << c6 << "): " << c6.complexangle() << endl;

  return 0;
}


23 Feb

ECE231 – Spring 2009 – Programming Assignment 2


This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.

1
2
3
4
5
6
7
8

#include <iostream>
#include <cmath>

#define PI 3.14159
or
const double PI = 3.14159;

Defining the Coordinate Class

For the coordinate class we need 2 private variables x and y, 3 constructors, and 11 member functions.

The way you do the constructors and member functions can be done 2 different ways. You can either do them inside the class, or you can define them inside the class, like you do prototypes, and then have the actual functions at the bottom of your code. I like to define them and then put the functions at the bottom, making the code a little bit easier to read so that is the way that I'll show you, but if you want to do it the other way then just do it how you normally would with a regular function.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class coordinate
{
  private:
    double x;  // x value of the coordinate point
    double y;  // y value of the coordinate point
  public:
    coordinate(); // 1st constructor requiring that no arguments are passed
    coordinate(double ); // 2nd constructor requiring that only 1 argument is passed
    coordinate(double , double ); // 3rd constructor requiring that 2 arguments are passed

    void set(double , double ); // sets both values and requires 2 arguments to be passed
    void setx(double ); // sets x value and requires 1 argument to be passed
    void sety(double ); // sets y value and requires 1 argument to be passed
    void read(); // allows user to input both x and y values 
    void print(); // prints out the coordinate point in "(x, y)" form
    double distancezero(); // calculates distance of point from zero and returns the value
    double distancetwo(coordinate ); // calculates distance between 2 points (current instance and coordinate value passed) and returns the value
    double ranglezero(); // calculates the angle of the coordinate in radians and returns the value
    double danglezero(); // calculates the angle of the coordinate in degrees and returns the value
    int quadrant(); // find what quadrant the coordinate is in and returns the value
    void midpoint(coordinate ); // calculates the midpoint between 2 points (current instance and coordinate value passed) and prints the value as a coordinate
};

Writing the Actual Functions for the Coordinate Class

Since we are not actually writing the functions in the same place we are doing the class we will write them at the bottom after the main function. However, to do this we will need a little more than what we would with regular functions. For member functions we have to define what class the function is actually a member of. To do this we have to do coordinate::[function]().

For example, here are the 3 constructors for the coordinate class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// takes no arguments and sets x and y to 0
coordinate::coordinate()
{
  x = y = 0;
} 
// takes 1 argument and sets x and y to that value
coordinate::coordinate(double a)
{
  x = y = a;
} 
// takes 2 arguments and sets the corresponding x and y to those values
coordinate::coordinate(double a, double b)
{
  x = a;
  y = b;
}

Next, we have the member functions. The first 3 are the set functions. These allow the user to set the values of either the x or the y or both together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// take 2 arguments and sets the corresponding x and y to those values
void coordinate::set(double a, double b)
{
  x = a;
  y = b;
}
// takes 1 argument and sets the x to that value
void coordinate::setx(double a)
{
  x = a;
}
// takes 1 argument and sets the y to that value
void coordinate::sety(double b)
{
  y = b;
}

Next, is the read and print functions. All we want the read to do is to do a cin of the 2 values (x and y). The print is just the opposite. All it does is do a cout of the x and y values in the (x, y) format.

1
2
3
4
5
6
7
8
9
10
11
12

// takes no arguments and reads in the x and y values from the screen
void coordinate::read()
{
  cin >> x >> y;
}
// takes no arguments and prints the x and y values in (x, y) format
void coordinate::print()
{
  cout << "(" << x << ", " << y << ")";
}

Next, we have the distance functions. distanczero calculates the distance of the current instance coordinate from 0. distancetwo calculates the distance between 2 points (the current instance and the coordinate passed in).

1
2
3
4
5
6
7
8
9
10
11
12

// takes no arguments and returns the distance of the current instance from zero
double coordinate::distancezero()
{
  return sqrt(x * x + y * y);
}
// take 1 argument and calculates the distance between them
double coordinate::distancetwo(coordinate pt)
{
  return sqrt(pow(x - pt.x, 2.0) + pow(y - pt.y, 2.0));
}

Next, we have the angle functions. ranglezero uses the function in the math library atan()(arc tangent) to find the angle in radians from the positive x axis. danglezero does the same thing but converts the value from radians to degrees. To convert from radians to degrees you multiply the value by 180 / pi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

// takes no arguments and returns the angle of the coordinate from the positive x axis in radians
double coordinate::ranglezero()
{
  double angle; // angle of coordinate from positive x axis
  if(x == 0) // if x == 0 then you will get a domain error so compute angle manually
  {
    if(y > 0) return PI / 2;
    if(y < 0) return -PI / 2;
    if(y == 0) return 0;
  }
  angle = atan(y / x);
  if(x < 0 and y >= 0)
    return PI - abs(angle);
  if(x < 0 and y < 0)
    return -(PI - abs(angle));
  return angle;
}
// takes no arguments and returns the angle of the coordinate from the positive x axis in degrees
double coordinate::danglezero()
{
  double angle; // angle of coordinate from positive x axis
  if(x == 0) // if x == 0 then you will get a domain error so compute angle manually
  {
    if(y > 0) return 90;
    if(y < 0) return -90;
    if(y == 0) return 0;
  }
  angle = atan(y / x) * 180 / PI;
  if(x < 0 and y >= 0)
    return 180 - abs(angle);
  if(x < 0 and y < 0)
    return -(180 - abs(angle));
  return angle;
}

Next, we have the quadrant function. This looks at whether the x and y values are positive or negative to determine which quadrant the coordinate is in. It will return the number of the quadrant as an integer 1-4 and 0 if the coordinate is (0, 0)

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int coordinate::quadrant()
{
  if(x > 0 && y >= 0)
    return 1; // return quadrant 1
  if(x <= 0 && y > 0)
    return 2; // return quadrant 2
  if(x < 0 && y <= 0)
    return 3; // return quadrant 3
  if(x >= 0 && y < 0)
    return 4; // return quadrant 4
  return 0;   // return 0 if point is (0, 0)
}

Last, we have the midpoint function. you will need to pass this function a coordinate instance and it will calculate the midpoint between the current instance and the passed instance and print the coordinate instance.

1
2
3
4
5
6
7
8
9

void coordinate::midpoint(coordinate pt)
{
  coordinate np; // new coordinate point
  np.x = (x + pt.x) / 2.0;
  np.y = (y + pt.y) / 2.0;
  np.print();
}

Now all you have to do is test each constructor and member function to make sure that everything is working properly.


Comments Off on ECE231 – Spring 2009 – Programming Assignment 2 Filed under: C / C++, development, Home, projects, tutorials Tags: , ,