Thursday, April 22, 2010

Programming Puzzle



1. Declare a double variable named Velocity and a pointer variable named PtrVelocity, and make PtrVelocity refer (point) to variable Velocity.

double Velocity;
double *PtrVelocity;
PtrVelocity = &Velocity;


2. Given this definition: int X_Array[] = {10, 20, 30, 40, 50, 60, 70, 80}; 
What is the meaning or, if applicable, the value of:

a. X_Array 
The address of the first element of the X_Array[] which holds 10 in our case.


b. X_Array + 2 
The address of the third element of the X_Array[] which holds 30 in our case.


c. *X_Array
Since an array is a pointer in itself, *X_Array gives us the value of the first element of the X_Array[] and the value is 10.



d. *X_Array + 2
Since an array is a pointer in itself, *X_Array + 2 adds the value of the first element of the X_Array[] + 2 Hence, the value is 12.



e. *(X_Array + 2)
Since an array is a pointer in itself, *(X_Array + 2) gives us the value of third element of the X_Array[] which is 30 because it is the value in the position that is two bytes ahead of where X_Array starts.





3. A program contains the following declarations and statements.

char u;
char v = 'A';
char* pu;
char* pv = &v;

*pv = v + 1;

u = *pv + 1;

pu = &u;

If the address of u is 5000 and the address of v is 5001, then (next page):


Columbia College - Aurora Campus
CISS 242 Midterm Exam

(a) [2] What value is stored at &v?        _5001 ________


(b) [2] What value is assigned to pv?      _5001_________


(c) [2] What value is represented by *pv?  _65  _________


(d) [2] What value is assigned to u?       _66___________


(e) [2] What value is stored at &u?        _5000_________


(f) [2] What value is assigned to pu?      _5000_________


(g) [2] What value is represented by *pu?  _66___________



4. Write a C-string function named strrev that uses exclusively pointer notation and reverses the elements of a given string. As is the convention of most C-string functions, have your function return the address of the given string.

int strrev (char*);

int strrev (char* str) {
   char* p = str + strlen (str) – 1;
   while (p >= str) {
      cout << *p;
      --p;
   }
   return &str;
}
  


CISS 242 Midterm Exam


5. Write a C-string function named strsum that uses exclusively pointer notation and calculates and returns the sum of the ASCII values of all characters in a given string.


int strsum(char*);

int strsum(char* str)
{
     int num = 0;
     int sum = 0;
     while (str[num] != '\0') {    
         sum += str[num];      
         ++num;
     }
     return sum;
}



6. Write a C-string function named strsumd that uses exclusively pointer notation and calculates and returns the sum of digits embedded within a given string. For example, if the given string were “ab2c38d4e27fg” then the sum would be 26 since 2 + 3 + 8 + 4 + 2 + 7 = 26. (Note: A more difficult function would be to calculate and return the sum of embedded numerical substrings; i.e., for the example given, the sum would be 71 since 2 + 38 + 4 + 27 = 71. Are there any takers for some extra credit?)


int strsumd (char*);

int strsumd (char* str) {
   int sum = 0;
   char* p = str;
   while (*p) {
      if (isdigit (*p)) {
         sum = sum + *p – ‘0’;
      }
   ++p;
   }
   return sum;


7. A challenging “fill-in-the-blanks” take-home problem (next/last page):
MEMORY LOCATIONS - Byte Addresses Shown: Hex Value (Decimal Value)

// Fill in the bits according to the declarations at the bottom.

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 1 0 1 1 1
0 (0)             1 (1)             2 (2)             3 (3)

1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 0 1 0 1 0 1 1
4 (4)             5 (5)             6 (6)             7 (7)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
8 (8)             9 (9)             A (10)            B (11)

0 1 0 0 0 0 1 0   1 0 0 0 0 0 1 0   1 1 1 1 0 1 0 1   1 1 0 0 0 0 1 1
C (12)            D (13)            E (14)            F (15)

1 1 0 0 0 0 0 0   0 0 1 1 1 0 1 1   0 1 1 0 0 0 1 1   1 1 0 1 0 1 1 1
10 (16)           11 (17)           12 (18)           13 (19)

0 0 0 0 1 0 1 0   0 0 1 1 1 1 0 1   0 1 1 1 0 0 0 0   1 0 1 0 0 0 1 1
14 (20)           15 (21)           16 (22)           17 (23)

0 0 1 1 1 1 1 1   0 1 1 1 0 0 1 0   0 1 0 1 0 0 1 0   0 1 0 1 0 0 1 0
18 (24)           19 (25)           1A (26)           1B (27)

0 1 1 0 1 1 1 1   0 1 1 0 1 1 1 0   0 0 0 0 0 0 0 0   0 1 0 0 1 0 0 0
1C (28)           1D (29)           1E (30)           1F (31)

0 1 1 0 0 0 0 1   0 1 1 1 0 0 1 0   0 1 1 1 0 1 0 0   0 0 0 0 0 0 0 0
20 (32)           21 (33)           22 (34)           23 (35)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 1 0 0
24 (36)           25 (37)           26 (38)           27 (39)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 1 1 0 0
28 (40)           29 (41)           2A (42)           2B (43)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 1 1 1 1 1
2C (44)           2D (45)           2E (46)           2F (47)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 1 0 1 1 0 0
30 (48)           31 (49)           32 (50)           33 (51)

_ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _
34 (52)           35 (53)           36 (54)           37 (55)

      int Index = 23, Value = -85; // sizeof(int) is 4; 2s-complement for
      long FlightSeconds = 0L;     //                   negative integers.   
      float Velocity = 65.48; // sizeof(float) is 4; this already done.
      double Temp = -27.39; // sizeof(double) is 8; this already done too.
      char Ch = '?', Letter1 = 'r', Letter2 = 'R';
      char FirstName[] = "Ron";   // How many bytes?
      char LastName[]  = "Hart";  // How many bytes?
      int* Valueptr = &Value; // Use 4 bytes for pointer variables.
      float* VelocityPoint = &Velocity;
      char* ByteAddr = LastName;
      char** PointToPoint = &ByteAddr;

Monday, April 12, 2010

Pointer Puzzle



//----------------------
// Variable Definitions
//----------------------

int* pi;
int* pj;
int* pk;

int t;

double* pd;

//-----------------
// Some Statements
//-----------------

*pd += static_cast(*pi);

pi = &t;

*pi = *pk;

pj = pi;

*pj /= 3;

++pj;

++*pj;

//-----------------------------------------------------------------------------
// Assuming the following initial configuration of memory (on the left side),
// what is the resulting configuration of memory (on the right side) after the
// above statements execute?
//-----------------------------------------------------------------------------

                   BEFORE             AFTER

                 __________         __________
                |                        |       |                    |
       1100 |     9                 |       |          9        |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
pi     1300|   1100           |        |    1350          |
                |__________|        |__________|

                 __________         __________
                |                       |       |                    |
t      1350 |    14              |       |       2            |
                |__________|       |__________|
                |                      |       |                     |
                |    20              |       |   21              |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
pj     1380|   1100           |        |   1354           |
                |__________|       |__________|

                 __________         __________
                |                      |         |                      |
pk    1400|   1410           |         | 1410             |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
       1410 |              7      |        |     7               |
                |__________|       |__________|

                 ____________________         ___________________
                |                                                |      |                                        |
       1430 |        0.0                                   |      |     9.0                              |
                |____________________|       |___________________|

                 __________         __________
                |                      |         |                      |
pd    1440|   1430           |         |1430              |
                |__________|        |__________|






Sunday, April 11, 2010

Movie Statistics

// Programming Environment: Bloodshed
// Professor: Ronald J Hart
// Program Description: This program can be used to gather the statical data
// about the number of movies college students see in a month. The program
// asks the user on how many students were survayed and then taking that integer
// value, it dynamically allocates memory as directed by user as an array of
// that many elements (integers). It also validates the user input limiting
// them to enter negative values. It uses a seperate function to display what
// the user has input for each student's movie watch behaviour. Then, it uses
// a seperate function (with pointer as an argument to it) to calculate the
// average, median, and mode of the values entered.
// Date/Time: 4/2/2010  6:51:27 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Function Prototypes
double Get_Average(int*, int);
void Display_Array(int*, int);
double Get_Median (int*, int);
void Sort_Array (int*, int);
int Get_Mode (int*, int);
//---------------------------------------------------------------------------

bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {

    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);
        
    do {
  
    int num_students = 0;  
    while (num_students <= 0) {
    cout << endl << "Enter the number of students survayed: ";
    cin >> num_students;
    }
    // Dynamically allocating  pointer named student_array that holds
    // num_students.
    int* student_array = new int[num_students];  
    for(int i = 0; i < num_students; ++i) {
         // Now storing the values in the student_array.
         cout << endl << "Enter the number of movies seen by student " << i+1
                      << " : ";
         cin >> student_array[i];
         while (student_array[i] < 0) {
         // Validating the user's input to student array for non-zero entries.
         cout << endl << "Invalid Entry!!"
                      << "Re-enter the number of movies seen by student " << i+1
                      << " : ";
         cin >> student_array[i];
         }        
    }
    cout << endl;
  
    Display_Array(student_array, num_students);
    // This function displays the contents of the student_array.  
    double average = Get_Average(student_array, num_students);
    // Average variable gets average from Get_Average function.
    cout << endl << "The average number of movies college students see per month: "
         << average; // Displays the Average.      
      
    Sort_Array(student_array, num_students);
    // Sorting the elements of student_array as we need the sorted array to
    // calculate the median of the of the elements in the array.
    cout << endl << endl << "The number of movies seen by each students (Sorted): ";
    for (int i = 0; i < num_students; ++i) {
        // Displaying the sorted array elements just for the shake of
        // information to the users.
        cout << student_array[i] << " ";
    }
    // The median variable gets the median from Get_Median().  
    double median = Get_Median(student_array, num_students);
    cout << endl << "The median of number of movies college students see per month: "
         << median; // displays the median.
  
    // The variable mode gets the mode from Get_Mode().
    int Mode = Get_Mode (student_array, num_students);
    // If it finds the mode then it will display the mode else it would disply
    // that the mode doesn't exists.
if (Mode > 0) {
    cout << endl << "The Mode of number of movies college students see per month: " << Mode;
} else {
         cout << endl << "No Mode Exists";
}

    } while (Do_Again ());
    return 0;
}
//------------------------------------------------------------------------
// Function Definitions

double Get_Average(int* array_num_movies, int student_num) {
       // This function calculates the average number of movies that the students
       // saw by returning a double to main which is the average number of movies
       // that the student who were survayed and stored in array saw.
       double average_num_movies = 0.0;
       for (int i = 0; i < student_num; ++i) {
           average_num_movies += array_num_movies[i];    
       }
       average_num_movies /=  student_num;
       return average_num_movies;
       }

void Display_Array(int* dynamic_array, int num_elements) {
     // This function uses the definations to display the contents of the array
     // loaded dynamically.
     cout << endl << "The number of movies seen by each students: ";
     for(int i = 0; i < num_elements; ++i) {
            cout << dynamic_array[i] << " ";
    }
}
 
double Get_Median (int* in_array, int num_elements) {
    // This function definations returns the median to main. If the num_elements is greater
    // than 1 then it sorts the array so that it could give median for odd as well
    // as even num_elements.
    double median;

if (num_elements > 1) {
Sort_Array (in_array, num_elements);
}
if (num_elements % 2 == 1) {
        // Odd number of elements.
median = static_cast(in_array[num_elements/2]);
} else {
// Even number of elements.
median = (in_array[num_elements/2 - 1] +
in_array[num_elements/2]) / 2.0;
}
    return median;
}

void Sort_Array (int* in_array, int num_elements) {
     // This function definations would sort the array for further calculations
     int temp;
     bool swapped = true;
     while(swapped) {
          swapped = false;
          for (int i = 0; i < num_elements; ++i) {
              if (in_array[i] > in_array[i + 1]) {
                   temp = in_array[i];
                   in_array[i] = in_array[i + 1];
                   in_array[i + 1] = temp;
                   swapped = true;                
              }
          }  
     }
}

int Get_Mode (int* in_array, int num_elements) {

// Function receives two parameters: first is the address
// on an integer array; second is the number of elements
// in the given array. This function returns the statistical
// mode of the given array of integers. (Note: We assume
// either one mode value exists or not; i.e., bi-modal and
// multi-modal situations are not considered.)

   int frequency;
   int greatest_frequency = 0;
   int mode;

   for (int i = 0; i < num_elements; ++i) { // For each array element...
    frequency = 0;
  for (int j = 0; j < num_elements; ++j) {
  if (in_array[i] == in_array[j]) {
               ++frequency;
  }
  }
  if (frequency > greatest_frequency) {
  greatest_frequency = frequency;
  mode = in_array[i];
  }
   }
   if (greatest_frequency == 1) {
       mode = -1;
   }
   return mode;
}

//------------------------------------------------------------------------
bool Do_Again (void) {

    (void) fflush (stdin);
    cout << endl << endl << "\aDo Again? (y/n): ";
    return static_cast(tolower (_getch ())) == 'y';
}