Thursday, December 1, 2011

Thursday 12/1 online class

We are going to review the Chapter 14 using the following class material:

1. Video Clip on the Recursion of Chapter 14: http://www.youtube.com/watch?v=qyaVrP8y0RM
You may view this video in full screen HD (720) 

2. Audio Clips (reside in Course Outline) on Chapter 14 by Prof. Burns.

Thursday, November 10, 2011

Chap12.3 sample code

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

struct Student
{
  string name;
  int id;
  float gpa;
};
 
void printStudents(Student* student, int nStudents)
{
  int i;
  for (i = 0; i < nStudents; i++)
  {
    cout << "Name = " << left << setw(30) << student[i].name;
    cout.fill('0'); 
    cout << " ID = " << right << setw(7)
      << student[i].id << ", gpa = "
      << student[i].gpa << endl;
    cout.fill(' '); 
  }
}

int main()
{
  // open a file for input
  ifstream fin;
  fin.open("students.txt");
  if (!fin.good()) throw "I/O error";
 
  // create an empty list
  const int MAX_STUDENTS = 100; // capacity
  int nStudents = 0; // initially empty
  Student student[MAX_STUDENTS];
 
  // read and save the records
  while (fin.good())
  {
    // create a record and read it from file
    Student aStudent;
    getline(fin, aStudent.name);

    fin >> aStudent.id;
    fin.ignore(1000, 10);
 
    fin >> aStudent.gpa;
    fin.ignore(1000, 10);
 
    fin.ignore(1000, 10); // skip the ---------- separator

    // add record to list, if it's not full
    if (nStudents < MAX_STUDENTS)
      student[nStudents++] = aStudent;
  }
  fin.close();
 
  // sort the students by name
  for (int i = 0; i < nStudents; i++)
  {
    for (int j = i + 1; j < nStudents; j++)
    {
      if (student[i].name > student[j].name)
      {
        Student temp = student[i];
        student[i] = student[j];
        student[j] = temp;
      }
    }
  }

  printStudents(student, nStudents);
 
  return 0;
} // main

Thursday, November 3, 2011

Pointer Example 1

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

int main()
{

  int andy = 25;
  int fred = andy;
  int* ted = &andy;
  cout << andy << endl;
  cout << fred << endl;
  cout << *ted << endl;
  cout << ted << endl;

    // declare the variables:

    int nNumber;
    int *pPointer;

    // now, give a value to them:

    nNumber = 15;
    pPointer = &nNumber;

    cout << "nNumber is equal to :" << nNumber << endl;
    *pPointer = 25;

    cout << "nNumber is equal to :" << nNumber << endl;
  return 0;
}

Array of Struct Initialization

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


// A struct Definition
struct Student
{
  string name;
  int id;
  float gpa;
}; // Student

int main()
{
  Student a[2]={{"Tom",123455, 3.66},
                {"Tom",123455, 3.66}};
  a[0].name = "George Washington";
  a[0].gpa = 3.14;
  a[0].id = 123456;

  cout << "Name=" << a[0].name
       << ", ID=" << a[0].id
       << ", GPA=" << a[0].gpa << endl;

  cout << "Name=" << a[1].name
       << ", ID=" << a[1].id
       << ", GPA=" << a[1].gpa << endl;

  return 0;
}

Passing Struct to Function

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

struct Student
{
  string name;
  int id;
  float gpa;
}; // Student

Student getGeorge()
{
  Student george;
  george.name = "George";
  george.id = 123456;
  george.gpa = 3.4;
  return george;
} // getGeorge

void printRecord(Student&amp; x)
{
  cout &lt;&lt; "Name=" &lt;&lt; x.name;
  cout &lt;&lt; ", ID=" &lt;&lt; x.id;
  cout &lt;&lt; ", GPA=" &lt;&lt; x.gpa;
} // printRecord

int main()
{
  Student s = getGeorge();
  printRecord(s);
  return 0;
} // main

Passing Array to Function

#include
#include
using namespace std;

double getAverage(int* score, int n)
{
  int sum = 0;
  int i = 0;
  for (i = 0; i &lt; n; i++)
    sum += score[i];
  double average = double(sum) / n;
  return average;
} // getAverage

int main()
{
  int daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  cout &lt;&lt; "Average = " &lt;&lt; getAverage(daysPerMonth, 12) &lt;&lt; endl;
  return 0;
} // main

Tuesday, November 1, 2011

Struct Sample Code

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

  struct Course
  {
    string name;
    int session;
    float grade;
    string prereq;
    string teacher;
  }; // Student

int main()
{
/* how to demarc the structs and arrays
 * struc s[5] = { {s1, s2, s3, s4, s5},
 *                {s1, s2, s3, s4, s5},
 *                 ....}};
 */
  Course clist[5] = {
   {"COMSC110", 3131, 100.00, "COMSC100","Robert"},
   {"COMSC100", 1111, 100.00, "COMSC90","Tony"},
   {"COMSC110", 2131, 100.00, "COMSC100","Larry"},
   {"COMSC265", 3331, 100.00, "COMSC110","Joe"},
   {"COMSC110", 3431, 100.00, "COMSC100","Tom"}};
//  clist[0].session = 3131;
//  clist[0].teacher = "Tony";
//  clist[0].name = "COMSC110";

  string term;
  cout << "What Course are you looking for? ";
  getline(cin, term);

  // a way to querry the course, and print out only COMSC110
  for(int i =0; i <5; i++)
    if(clist[i].name == term)
    cout << clist[i].name << ": "
       << clist[i].session << " by "
       << clist[i].teacher << endl;

  return 0;
}

Thursday, October 27, 2011

Testing Scopes

#include <iostream>
using namespace std;

int main()
{
  int j = 1;
  cout << j << endl;

  // assigning the address of outer j to jptr
  int* jptr = &j;

  // creating j2ptr to hold the address of inner j
  int* j2ptr;
  cout << jptr
       << "____________________"  << endl;
  int i = 5;
  for (int j = 1; j<i ; j++)
  {
    // this for loop only operates on the
    // inner j
    j2ptr = &j;
    cout << "Rock and Roll!" << j << endl;
  }

  // the outer j is not affected by the
  // above for loop
  cout << j2ptr
       << "____________________"  << endl;
  cout << j << endl;
  return 0;
}

Pointers Reference

pointers explained
pointer examples

Tesing Array

#include <iostream>
using namespace std;

void enterArray(int* a, int size)
{
  // read and save in the array according the the size
  int i;
  for (i = 0; i < size; i++)
  {
    cout << "element " << i+1 << ": ";
    cin >> a[i];
  } // for

  // print out the content
  for (i = 0; i < size; i++)
    cout << a[i] << ' ';
  cout << endl;
}

int main()
{
  int size;
  while (size)
  {
    int* intPtr; // a pointer to the integer type
    cout << endl
         << "What is the size of the array: (0 to quit) ";
    cin >> size;
    int* score = new int[size];
    intPtr = score;
    cout << "array address: "<< *score << endl;

    enterArray(score, size);
    cout << "the pointed items: ";
    for (int i=0; i<size+4; i++)
      cout << *intPtr++ << ' ';
    delete [] score;
  }

  return 0;
} // main

Thursday, October 20, 2011

floating point data references

float internal 

floating point data representation

//
// This program is an illustration for potential floating point boundary error
// only the floating point number > 1 case is implemented.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  ofstream fout;
  fout.open("data.txt");
  if (!fout.good()) throw "I/O error";

  double x, mant;
  int exp = 0;
  cout << "Enter a postive x > 1: ";

  //Now find exponent and mantissa
  cin >> x;
  cin.ignore(1000,10);
  fout << x << endl;

  //
  // shifting the decimal point left
  // until the number is smaller than 1
  // to obtain the exponent
  mant = x;
  while (mant >= 1)
  {
    mant /= 2;
    exp++; // At this point x = mant * 2^(exp)
  }
  while (mant < 0.5)
  {
    mant *= 2;
    exp--;
  }
  cout << x <<" is stored in exponent and mantisa, \n"
       << "as 2^(eee) *.mmm\n"
       << x <<" = ";
  cout << "2^(" <<exp<<") *.";
  fout << x <<" is stored as mmm.mmm\n"
       << x <<" = ";
  //
  // shifting the decimal point right
  // to convert decimal fraction to binary mantisa
  int i=0;
  string s = "";   // mantisa
  string ss = "";  // binary float
  float bfrac = 0; // binary fraction
  float weight = 1;      // fraction bit weight
  while (mant > 0)
  {
    mant *= 2;
    if (mant >= 1)
    {
      s = s + '1';
      ss = ss + '1';
      mant -= 1;
      if(i >= exp) bfrac += weight = weight/2;
    }
    else
    {
      s = s + '0';
      ss = ss + '0';
      if(i >= exp) weight = weight/2;
    }
    if(++i == exp) ss = ss + '.';
    if(i >= 23) break;
  }
  cout << s << endl;
  cout << "or, presented as: " << ss << endl;
  fout << ss << endl;

  cout << "regenerated decimal fraction from binary fraction: \n"
       << bfrac << endl;
  fout << "regenerated decimal fraction from binary fraction: \n"
       << bfrac << endl;
  return 0;
}

floating point data range and precision

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

int main()
{
  cout << fixed << setprecision(25) << endl;
  cout << "--------- round-off error------------" << endl;
  double x = 3;
  cout << x << endl;
  cout << (x + 0.0000000000000001) << endl;
  cout << (x + 0.000000000000001) << endl << endl;

  cout << "--------- more round-off -------------" << endl;
  cout << scientific << setprecision(25) << endl;
  double d;
  for (d = 0; d != 2.0; d += 0.4)
    cout << d << endl;
  cout << d << endl << endl;

  float f;
  for (f = 0; f != 2.0; f += 0.4)
    cout << f << endl;
  cout << f << endl << endl;

  cout << "--------- boundary test -------------" << endl;
  f = 3.402823866e37;
  cout << f << endl;
  f = 3.402824036e37;
  cout << f << endl;
  f = 3.402824136e37;
  cout << f << endl << endl;

  f = 3.402823567e38;
  cout << f << endl;
  f = 3.402823568e38;
  cout << f << endl;
  f = 3.402823866e38;
  cout << f << endl << endl;

  return 0;
}