程序代写代做 algorithm Name:

Name:
• •
EAS 240: Introduction to Programming for Engineers Practice Final Exam
DO NOT TURN THIS PAGE OVER UNTIL YOU ARE TOLD TO DO SO
___________________________
This exam has X problems, worth a total of Y points.
You are allowed one two-sided, handwritten 8.5” by 11” formula sheet.
Question
Score
Out of
Your Grade
100

Page 2 EAS 240: Introduction to Programming for Engineers Practice Final Exam
Problem 1: Multiple choice
Each of the following multiple choice questions have only one correct answer.
1. IGNORE THIS (NOT COVERED THIS YEAR) Which of the following will compile without error?
(a) doubler;
constexpr double a = 3.14 * r * r;
(b) const float pi;
(c) float x {3.14};
doubley {x};
(d) char x {300};
2. Consider the following statements:
string str1 = “Hello”, str2 = “world”; string str3 = str1 + str2;
int len = str3.size();
What is the value of len? (a) 10
(b) 11
(c) 12
(d) The above code will not compile.
3. Function overloading is a feature of C++. Which one of the following pairs of function prototypes will compile without error?
(a) intfunc1();
int func1(float x);
(b) void func2(float x); void func2(double x);
(c) char func3(double x);
char func3(double x, int i);
(d) double func4(int x); int func4();
(e) All of the above are correct applications of function overloading.

Page 3 EAS 240: Introduction to Programming for Engineers Practice Final Exam
4. Consider the overloaded addition operator defined for a Signal class:
class Signal { // …
public:
// …
Signal operator+(int offset); // …
};
Signal Signal::operator+(int offset) { vector tmp;
}
for (int i = 0; i < length(); i++) { tmp.push_back(m_samples[i] + offset); } return Signal(tmp); Assume that sig1 and sig2 are both Signal objects. Which of the following statements is an incorrect use of the addition operator as it is defined above? (a) sig2 = sig1 + 1; (b) sig2 = 1 + sig1; (c) sig2 = sig1.operator+(1); (d) All of the above are correct. (e) None of the above are correct. Problem 2: Class constructors and destructors 1. How do you define a constructor? When is a class' constructor called? 2. How do you define a destructor? When is a class' destructor called? Page 4 EAS 240: Introduction to Programming for Engineers Practice Final Exam Problem 3: Fill in the blank Fill in the blanks in the following code blocks to achieve the specified functionality. 1. Fill in the blank so that the square function modifies the caller's argument. } 2. IGNORE THIS (NOT COVERED THIS YEAR) Fill in the blank so that m_count is an int that shares the same value across all instances of the class Foo: ______ m_count; public: Foo(); }; 3. Fill in the blanks to read in a file with two columns of data (voltage and current) and print the data to the terminal. int main() { (________); if (!_______________) { } } void square(______ x) { x *= x; class Foo { private: string iname; int voltage, current; ___ << "Enter input file name: "; ___ >> iname;
ifstream ist
cout << "ERROR: Can't open file " __ iname __ endl; return 0; } while (________________________){ ist >> _________ >> _________
___ << voltage << "\t" << current << endl; return 0; Page 5 EAS 240: Introduction to Programming for Engineers Practice Final Exam Problem 4: The Account class Consider the below Account class: class Account { public: // constructor initializes name and balance Account(std::string accountName, int initialBalance); // sets/gets account name void setName(std::string accountName); std::string getName(); // get account balance int getBalance(); // withdraw/deposit valid amount from/to balance int withdraw(int withdrawAmount); void deposit(int depositAmount); private: std::string m_name; 1. Write a main() function that opens an account in your name with an initial balance of $1000, withdraws $250, and then deposits $100. 2. Consider the following implementation of the Account::deposit function: void Account::deposit(int depositAmount) { } Rewrite this function using the this keyword. 3. Suppose that acct has been defined as an Account object in main(). Which of the following are legal statements (circle all of the correct answers). (a) acct.getBalance(); (b) acct.m_balance; (c) acct.withdraw(10); // account holder's name int m_balance = 0; // account balance }; if (depositAmount > 0)
m_balance += depositAmount;

Page 6 EAS 240: Introduction to Programming for Engineers Practice Final Exam
Problem 5: Complex numbers
Define a complex number class (called Complex) with private member variables representing the real and imaginary parts of a complex number. As part of the class, define the following:
1. Define a constructor to initialize the real and imaginary parts of the complex number using a member initializer list.
2. Define a public member function to calculate the magnitude of the complex number.
3. Define an overloaded addition operator to add two complex numbers and an overloaded multiplication operator to multiply two complex numbers.
Hint 1: Consider the complex numbers  =  +  and  = + . The magnitude of  can be calculatedas√ +;thesum+= + + + ;andtheproduct⋅=  −   +  +  . Y ou may use the sqrt() function to help you calculate the magnitude.
Hint 2: You may use the following function prototypes for the overloaded addition and multiplication operators:
Complex operator+(const Complex& c); Complex operator*(const Complex& c);
Problem 6: Dynamic memory allocation
Consider the complex number class from problem 5. Create a vector of pointers to complex numbers that are positioned on the unit circle at angles  =  degrees (or  radians) for  =

0,1, … ,  − 1. First, use new to allocate memory to each of the complex numbers. Second, print
the numbers to a file, with one complex number on each row. Lastly, use delete to deallocate the memory.
Hint: Given  you can determine the real and imaginary parts of a complex number  =  +  on the unit circle as: =cos and =sin. You may use thecos()andsin() functions, which take the angle in radians as their arguments. You may assume that # = 3.14.

Page 7 EAS 240: Introduction to Programming for Engineers Practice Final Exam
Problem 7: Random numbers
1. Write a statement to generate a random number in the set {1, 2, 3, 4, 5, 6}.
2. Write a statement to generate a random number in the interval [-1, 1].
3. Write a for-loop in which you generate a pair of random numbers , , each in the interval [−1,1]. If the pair of random numbers falls inside the unit circle (i.e., the circle centered at (0,0) with radius 1), then increment a counter.
4. BONUS: How can you use the algorithm in problem 4.3 to estimate the value of #?
Problem 8: Strings
1. Write a program that checks if two files contain identical text by comparing them word- by-word.
2. Write a program that counts the number of times that a specific word appears in a file, the number of words in the file that come alphabetically before the specific word, and the number of words that come alphabetically after the specific word. Assume that the word you are checking is read as input from the terminal/console.
3. BONUS: Write a program that counts the number of repetitions of each word in a file.