Practice Programming Exercises and Solutions Winter 2015

First-page document preview for Practice Programming Exercises and Solutions Winter 2015 in CPS 125: Digital Computation and Programming at Toronto Metropolitan University (TMU). Inspect the first page below or request the complete PDF study document for free.

Independent study archive Learn more

Materials are uploaded by users for self-study purposes. PwnHero is an independent platform, not affiliated with, sponsored by, or endorsed by any university or institution.

📄 Page 1 Preview
First page preview of Practice Programming Exercises and Solutions Winter 2015 for Digital Computation and Programming at Toronto Metropolitan University (TMU)

Page 1 Key Topics & Excerpt

🔍 Text Transcript

First page excerpt for quick study reference. Complete formulas, diagrams, and subsequent pages are available in the full document.

PwnHero CPS125‐P15‐Practice More programs! 1‐Write a complete C program consisting only of a main program that will read from a file (named pairs.txt) pairs of numbers: a double followed by an integer. We do not know how many pairs there are but all pairs are complete. Your program must calculate pair by pair the division of the double by the integer and print on a separate line each quotient with three decimals precision. Finally your program will display on a separate line the number of pairs followed by the word “pairs.”. For example if the file contains 4.5 3 6.6 2, your program will display 1.500 3.300 2 pairs. #include <stdio.h> int main (void) { int npairs=0, integer; double doubl; FILE *in; in = fopen ("pairs.txt", "r"); while (fscanf(in, "%lf %d", &doubl, &integer)!=EOF) { npairs = npairs + 1; printf ("%.3lf\n", doubl/integer); } fclose (in); return (0); } 2‐Complete the function only (no main program) that will take two matrices (up to 5x5 in size) and give their multiplication. Assume that the matrices coming from the main program are already compatible for multiplication. void matmult (int m1[][5], int m2[][5], int m3[][5]) { int i,j,k; for (i=0; i<5; ++i) for (j=0; j<5; ++j) { m3[i][j]=0.0; for (k=0; k<5; ++k) m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];} 3‐Complete the function only (no main program) that will take in an array of double numbers and will “return” a different array containing the inverted values (1/n). For example if the original array contains 1.0, 2.0, 3.0..., the inverted array PwnHero.com

Related CPS Courses at Toronto Metropolitan University (TMU)