Lab #1 .ca



MECH 415 Assignment #1: Review

Question #1

Consider a 2D array A with 3 rows and 3 columns which will be used to represent a 3x3 matrix. A 1D array v will be used to represent a 3 component vector. Use for loops while answering the questions below.

a) Declare the arrays A and v in the main function.

b) Develop a function set_Av(A,v) that inputs the elements of A and v from the keyboard. The function should also print out the result to the screen.

c) Develop a function norm(v) which calculates the magnitude of the vector v. The function should also print out the result to the screen.

d) Develop a function multiply(A,v,b) that calculates b = A*v where * represents matrix-vector multiplication. The function should also print out the result to the screen.

Question #2

Develop a program (you can put this in the main) that inputs an integer n and a double x from the keyboard.

Using while, if, else, and if-else ladders write a program that performs the following tasks.

a) If n5 print out an error message and repeatedly request an input until there is no error.

b) Using an if-else ladder, perform the following tasks depending on the value of n

i) For n = 1 calculate and print out sin(x).

ii) For n = 2 calculate and print out the absolute value of x.

iii) For n = 3 calculate and print out exp(x).

iv) For n = 4 calculate and print out log base 10 of x (check x for valid values).

v) For n = 5 save the value of x into a file called “x-file.txt”.

c) Declare and set the elements of a 1D array B to B[i] = x^i, where i varies from 1 to n. The symbol ^ represents the power operator (e.g. x^2 = x*x).

Question #3

Write a program (put into the main function) that performs the following tasks:

a) Declares and initializes two string variables (i.e. character arrays that end with ‘\0’) s1 and s2, where s1 = “applepeachorange” and s2 = “mangopear”.

b) Calculates the lengths (given by n1, n2) (not including the termination character) of s1 and s2, respectively.

c) Calculates a string s3, using s1 and s2, formed by appending s2 on to the end of string s1 (i.e. s3 = “applepeachorangemangopear”). Print s1, s2, and s3 out to the screen.

d) Calculates a string s4 formed by alternating the characters of s1 and s2 (i.e. s4 = “ampapnlgeoppeeaacrhorange”). Print s4 to the screen.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download