程序代写代做 algorithm Name:

Name:
• •
EAS 240: Introduction to Programming for Engineers Solution – Practice Final Exam
DO NOT TURN THIS PAGE OVER UNTIL YOU ARE TOLD TO DO SO
___ Solution ______
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. Which of the following will compile without error?
(a) doubler;
constexpr double a = 3.14 * r * r;
// Error: value of r not usable in constant expression. r must be declared a constexpr.
(b) const float pi;
// Error: must initialize a constant when it is defined
(c) float x {3.14};
doubley {x};
// OK: double has higher precision than float
(d) char x {300};
// Error: narrowing conversion (char can’t hold the number 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 // Correct (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.
// Correct

Page 3 EAS 240: Introduction to Programming for Engineers Practice Final Exam
4. Consider the overloaded addition operator that we defined for the 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; // Incorrect (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? A constructor is a function with the same name as the class. For example, class Foo has a constructor named Foo() (or Foo::Foo()). Constructors do not return anything. A constructor is called automatically when a Foo object is instantiated. 2. How do you define a destructor? When is a class' destructor called? A destructor is a function with the same name as the class that starts with a ~. For example, class Foo has a destructor named ~Foo() (or Foo::~Foo()). Constructors do not return anything. A destructor is called automatically when a Foo object leaves scope or is deleted. 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 blanks so that the square function swaps two ints passed by the caller. } 2. Fill in the blank so that m_count is an int that shares the same value across all instances of the class Foo: static int m_count; }; 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() { void square(int &x) { x *= x; class Foo { private: public: Foo(); string iname; int voltage, current; cout << "Enter input file name: "; cin >> iname;
ifstream ist(iname);
if (!ist.is_open()) {
}
cout << "ERROR: Can't open file " << iname << endl; return 0; while (!ist.eof()) ist >> voltage >> current
cout << voltage << "\t" << current << endl; return 0; } Page 5 EAS 240: Introduction to Programming for Engineers Practice Final Exam Problem 4: The Account class Recall the 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. See prob4.1.cpp. 2. Consider the following implementation of the Account::deposit function: void Account::deposit(int depositAmount) { } Rewrite this function using the this keyword. See Account.cpp. 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). // account holder's name int m_balance = 0; // account balance if (depositAmount > 0)
m_balance += depositAmount;
(a) acct.getBalance(); (b) acct.m_balance;
(c) acct.withdraw(10);
// OK: legal call to public member function
// Error: cannot access private member variable // OK: legal call to public member function

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); See Complex.cpp, Complex.h, and prob5.cpp.
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.
See prob6.cpp.

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 #?
See prob7.cpp.
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.
See prob8.1.cpp, prob8.2.cpp, and prob8.3.cpp.