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 = i + 1) {
      sum = 0;
      for(j = 0; j < 3; j = j + 1) {
         sum = 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 problems using the C programming language.

Given the following string that contains information about a book:
where the name of the book, the author’s name, and the year it was
published (in that order) appear in the string and where each field is
separated by the semi-colon ‘;’ character. 

char bookInfo[121] = “Emma;Jane Austen;1815”;

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

   int isEqual(char bookInfo[ ], char author[ ]);

This function accepts a bookInfo string (see above) and a string author
and determines if the author is present within bookInfo. The function
returns a true value if the author is found or a false value otherwise.

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

Given the following string that contains information about ANY NUMBER of
books: where the name of the book, the author’s name, and the year it was
published (in that order) appear in the string and where each field is
separated by the semi-colon ‘;’ character and each record (different books)
is separated by the ‘@’ symbol.

char bookData[121] =
   “Persuasion:Jane Austen:1818@Emma:Jane Austen:1815@”
   “Les Miserables:Victor Hugo:1862@Frankenstein:Mary Shelley:1818”;

   int searchRecords(char bookData[ ], int findYear);

This function accepts a bookData string and for each book, prints out
the name of the book only if the integer value of the book’s year is
equal to the value stored in the integer parameter findYear.
The function returns a count of all books found with that year.

For example if the value of 'findYear' 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:

double graphCurrency(double currencyValues[ ], int nValues);

where:
currencyValues contains monthly currency values of 1 Canadian dollar and
nValues holds the number of doubles in the currencyValues array.

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[1] – array[2],  array[2] – array[3],  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.
The function must return the largest monthly decrease as an integer.

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");
   
   largestDecrease = graphCurrency(cdn, 12);

   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>

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

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

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

   for(n = 0; str[n] != ‘\0’; n = n + 1)
      ;

   if(i == 1)
      printf("%s\n", "first time");
   else
      printf("%s\n", "next");

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

   return x - n;
}

Any Browser!