
<p> MECH 415 Assignment #2: Review</p><p>Question #1</p><p>Write a program that calculates x-- (first initialize x to 0) and prints it out to the screen, when x belongs to the following variable types. From these results indicate the maximum value that these variable types can store.</p><p> a) unsigned char b) unsigned int c) unsigned short int d) unsigned __64int</p><p>Question #2</p><p>Write a program that uses an infinite loop to repeatedly calculate and print out (followed with a getch() to slow the loop down) the expression x = x * 10.0, where x is a double initialized to 1.0. Run the program and use the result to determine the largest order of magnitude (i.e. exponent) the variable type can store.</p><p>Question #3</p><p>The simulation of a 2nd order differential equation given by ˙x˙ x x˙ sin(t) can be approximated using the Euler simulation method given by x x f dt , t t dt where </p><p>x1 x2 x , f x2 x1 x2 sin(t)</p><p>Note x1 x is the position and x2 v is the velocity. Evaluate the Euler simulation equation indicated above with the initial conditions x1 1, x2 0 at t 0 and dt 0.001 repeatedly using an infinite while loop and suitable arrays to represent the vectors. Print the variables x1,x2,t to the screen each time through the loop followed by a new line. Use a break statement to exit the loop when the ‘right arrow’ key is pressed (hint: use a cast to determine the integer value of that key). </p><p>For each question below (if applicable) illustrate the function usage with an appropriate main that calls the function and prints out the values (including call-by-reference parameters) that are returned.</p><p>Question #4</p><p>Write a function compute_array(M,nz,n7) that initializes (from the keyboard) and computes various statistics for a 5x5 matrix M. The function computes the number of elements equal to zero (nz) and the number of elements greater than 7 (n7). Indicate which function parameters should be call-by-value or call-by-reference. Print out an error message (and return 1) if at least one matrix element is greater than 10 AND at least one element is less than -10. Question #5</p><p>Write a function save_array(M,file_name) that saves a 5x5 matrix (like the one calculated in question #4) to a file, where the name of the file is stored in a string variable file_name. The format of the file has one matrix row on each line of the file.</p><p>Question #6</p><p>Write a function load_array(M,file_name) that reads a 5x5 matrix from a file with the same format as question #5.</p><p>Question #7</p><p>Write a function counter1() that counts and returns the number of times it has been called without using a function parameter or a global variable. When the count reaches 100 it should stop counting and print out “count is complete”.</p><p>Question #8</p><p>Write a program with your matrix functions written in #4, #5, and #6 placed into a file called “matrix.cpp” and header file “matrix.h”. The header file will also define the constants PI and e to 16 significant figures (i.e. calculate them using <cmath> functions). Illustrate your program using a suitable main function defined in a second file “program.cpp”.</p><p>Question #9</p><p>Write a function load_table(x,y,n) that reads a table from a file and stores the values in dynamic 1D arrays x and y with a size of n. The format of the file is as follows: n x1 y1 x2 y2 … … xn yn</p><p>Hint: The pointers x and y must be call by reference in order to update/change them with new inside the function.</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-