Seneca College
SENECA COLLEGE OF APPLIED ARTS AND TECHNOLOGY
SCHOOL OF INFORMATION AND COMMUNICATIONS TECHNOLOGY  SY

                FINAL EXAMINATION

SEMESTER        SUBJECT NAME                            SUBJECT CODE

Fall 2014       Introduction To Programming Using C     IPC144

    NAME:                              

    STUDENT NUMBER:                      

    SECTION:              PROFESSOR:                          

DATE and TIME:    Monday, December 8, 2014  2:00 P.M. to 4:00 P.M.
LOCATION:         GYM
TIME ALLOWED:     2 Hours

QUESTIONS:     4 QUESTIONS ON 5 PAGES (80 MARKS TOTAL)
QUESTION #1    Walkthrough    20     MARKS
QUESTION #2    Programming    20     MARKS
QUESTION #3    Programming    20     MARKS
QUESTION #4    Walkthrough    20     MARKS
-----------    -----------    --     -----
TOTAL                         80     MARKS
PROFESSOR(S) / SECTION(S):

SPECIAL INSTRUCTIONS:
Any C programming textbook AND a single reference sheet (double sided)
is permitted and must be submitted with the exam.  Answer all questions
in the examination booklet(s) provided.
Ensure that all questions are clearly identified in the
examination booklet(s).
At the end of the examination, insert this examination paper in
your examination booklet(s),
and hand in all material. Show all rough work.
Marks for each question are provided in (brackets), so plan your
time accordingly.
This exam includes a cover page, plus 4 pages of questions. 


SENECA'S ACADEMIC HONESTY POLICY:
As a Seneca student, you must conduct yourself in an honest and 
trustworthy manner in all aspects of your academic career. 
A dishonest attempt to obtain an academic advantage is considered
an offense, and will not be tolerated by the College.

APPROVED BY:
Mary Lynn Manton, Chair, School of ICT


/*    QUESTION #1 WALKTHROUGH (20 Marks).
      Determine the exact output of the following C program.
      You must show your rough work to receive full or partial marks.
*/

#include <stdio.h>

#define MATRIX 6

int main( ) {
   int i, j, sum,
       m1[MATRIX] = { 1, 2, 3, 4, 5, 6 },
       m2[MATRIX] = { 7, 8, 9, 2, 8, 7 },
       m3[MATRIX / 3];

   for(i = 0; i < 2; i++) {
      sum = 0;
      for(j = 0; j < 3; j++) {
         sum += (m1[3 * i + j] * m2[i + 2 * j]);
         printf("i: %d j: %d sum: %d m1: %d m2: %d\n",
                     i, j, sum, 3 * i + j, i + 2 * j);
      }
      m3[i] = sum;
      printf(">>>m3[%d] = %d<<<\n", i, m3[i]);
   }
   return 0;
}


QUESTION #2 PROGRAMMING (20 Marks).

Write a solution to the following problem using the C programming language.

Given the following structure declaration that contains information
about a book:

struct Book {
   char name[31];
   char author[60];
   int publishYear;
};


Part A: Write the following function (5 marks).

   int isEqual(struct Book bookOne, struct Book bookTwo);

This function accepts two Books and determines if they are equal.
Books are considered equal if they have same name and the same
author. The function returns a true value if the Books are equal 
or a false value otherwise.

Part B: Write the following function (15 marks).

   int searchRecords(struct Book item, const char *fileName);

This function accepts a Book and the address of a string that holds the name of
a file containing data records in the following format:

Persuasion:Jane Austen:1818
Les Miserables:Victor Hugo:1862
Emma:Jane Austen:1815
Frankenstein:Mary Shelley:1818

where each record contains the name of a book, its author, and the year it
was published (all fields separated by the ':' character and followed by a newline).

This function reads through the file one line at a time and for
each line, prints out the name of the book only if the year matches
the value of 'publishYear' in the struct 'item'. The function
returns a count of all books found with that year.
For example if the value of 'publishYear' within the Book 'item' is 1818,
then the function displays:

Persuassion
Frankenstein

and returns a value of 2.


QUESTION #3 PROGRAMMING (20 Marks).
Write a solution to the following problem using the C programming language.

Write the code for a function with the following prototype:

void graphCurrency(double currencyValues[ ], int nValues, int *largestDecrease);

where:
currencyValues contains monthly currency values of 1 Canadian dollar,
nValues holds the number of doubles in the currencyValues array, and
largestDecrease must be updated to hold the largest monthly change in the currency.

The function displays a horizontal graph (using the * character) of
the monthly changes in the value of the Canadian dollar. The change in
currency is calculated by subtracting consequtive pairs of array values
and multiplying the difference by 1000. Consequtive pairs of array elements are those that
follow each other (eg.  array[0]  array[1],  array[2]  array[3],  array[4]  array[5],  etc.).
This value is then converted to a whole number and used to display
the graph for that calculation, but only if the whole number
value is greater than 0.

So, for example, if the first array value is 0.9542 and the second
array value is 0.9398, then the change in monthly currency
is (0.9542  0.9398) * 1000 = 14.4.
This value (converted to 14), is then displayed on the screen followed
by a space and a row of
14 asterisks (*) followed by a newline. This is repeated for all monthly changes.

So, for example, the following program:

int main( ) {
   double cdn[12] = {  0.9542, 0.9398, 0.9176, 0.9041, 0.8999, 0.9089, 
                       0.9176, 0.9232, 0.9325, 0.9152, 0.9092, 0.8912 };
   int largestDecrease;
   printf("CDN $ DECREASES FROM NOV. 2013 TO OCT. 2014\n");
   
   graphCurrency(cdn, 12, &largestDecrease);

   printf("The largest decrease is: %d\n", largestDecrease);
   return 0;
}

Displays:

CDN $ DECREASES FROM NOV. 2013 TO OCT. 2014
14 **************
22 **********************
13 *************
 4 ****
-9  
-8  
-5  
-9  
17 *****************
 6 ******
18 ******************
The largest decrease is: 22


/*  QUESTION #4 WALKTHROUGH (20 Marks).
    Determine the exact output of the following C program.
    You must show your rough work to receive full or partial marks.
    NOTE: Subtracting or adding an integer to or from a character
          produces a different character.
          For example: 'C' + 1 = 'D',   '8' - 2 = '6',   '5' + 2 = '7',   'B' - 1 = 'A', etc.
          The ASCII chart is NOT required, however the alphabet is listed below
          for your convenience.
    Alphabet:        A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
*/

#include <stdio.h>
#include <string.h>

int proc(int *, char [ ], int);  /* function prototype */

int main( ) {
   int num=0, index=-1, rc;
   char word[5] = "0763";
   
   do {
      index++;
      rc = proc(&num, word, index); /* calling the function */
      printf("main>>> num: %d word: '%s' rc: %d\n", num, word, rc);
   } while(rc != 0);
   return 0;
}

int proc(int *p, char str[ ], int i) {
   int x = i + 1;
   *p += 1;

   printf("%s\n", *p == 1 ? "first time" : "next");

   if(str[i] >= '0' && str[i] <= '7') {
      str[i] = str[i] + x;  /* adding an integer to a char */
   }
   else {
      str[i] -= (i + 2);    /* subtracting an integer from a char */
   }
   printf("proc>>> pntr: %d str: '%s' i: %d str[%d]: '%c'\n",
                                *p, str, i, i, str[i]);
   return x - strlen(str);
}

Any Browser!