practice-final-w05s

practice-final-w05s - A text file called accidents.bin...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Questions from the CPS125 Final Exam - Winter 2005 Question 1 (10 marks) Write a function only that takes a 2D array of integers and returns another 2D array of integers with its corner values swapped (the top-left with bottom-right and the top-right with bottom-left). In addition, the function will return the sum of all four corner values. Do not scan nor print anything in the function. Everything comes from and goes back to the main program. The maximum possible array size is 50x50 but in can be as small as 2x2. See the example with a 3x3 array. Ex: if the array is 1 2 3 then the new array will be 9 2 7 4 5 6 4 5 6 7 8 9 3 8 1 and the sum of the corners will be 20. int swapcorners (int ar1[][50], int ar2[][50], int nrows, int ncols) { int sumcorners, temp; ar2[nros-1][ncols-1] = ar1[0][0]; ar2[0][0] = ar1[nros-1][ncols-1]; ar2[nrows-1][0] = ar1[0][ncols-1]; ar2[0][ncols-1] = ar1[nrows-1][0]; sumcorners = ar1[0] + ar1[nros-1][ncols-1] + ar1[0] [ncols-1] + ar1[nrows-1][0]; return (sumcorners); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Question 2 (10 marks)
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A text file called accidents.bin contains accident numbers on the main highways of Ontario for a typical year. Write a complete program (main only) that will read data from the file (we don't know how many highways in the file but there is only one year) and calculate the total number of accidents in the year. Print out the year and the total number of accidents. You must use the user-defined structure type accdata in your program to store the data. Ex: if the file contains 2004 401 400 402 70 403 80 your report will print 550 accidents in 2004 . #include <stdio.h> typedef struct { int highwayno; /* highway number */ int nacc; /* number of accidents */ } accdata; int main (void) { FILE *in; int totacc=0, year; accdata data; in = fopen ("accidents.bin", "r"); fscanf (in, "%d", &year); while (fscanf (in, "%d%d", &data.highwayno, &data.nacc)! =EOF) { totacc = totacc + data.nacc; } printf (%d accidents in %d\n", totacc, year); fclose (in); return (0); }...
View Full Document

Page1 / 2

practice-final-w05s - A text file called accidents.bin...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online