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

Info icon This 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); }
Image of page 1

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

View Full Document Right Arrow Icon
Question 2 (10 marks)
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern