CS 215 Spring 2018 Lab Exam 1 Review Material: - All material for the course up through the Arrays I slides - Nothing from the slides on Functions, Array Arguments, or Implementing Functions Format: - You will be given 4-5 programming problems to solve. - All solutions will be written in one.cpp file in one main() function. - One.cpp file will be submitted in Canvas - You will have 1 hour and 30 minutes to complete the exam. Points will be deducted per minute late submitted in Canvas. Practice Exam: Below is an exam from last semester. - Your exam will be slightly shorter - Your exam will not include material on this exam that is not indicated above. - Your exam will include material indicated above that is not on this exam. Dropping the write a function requirement, you could basically copy/paste the answers given at the end into your main(). Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.
CS 215 Fall 2017 Lab Exam 1 Sec 006 Exam Rules: - You may use any notes/books, including those posted on the course website. - You may not use any other website. - You may not use any other devices (turn off cell phones, no calculators, etc). - You may use the lab machine, your laptop, or your VM. - You must use MS Visual Studio (it will be graded using MSVS, just like projects). - You may not communicate with any person other than the TA in any manner during the exam. Doing so will be considered cheating. - Timed: the exam is timed: o you will have one hour and 20 minutes to post your.cpp file in Canvas. o The TA will give you a 10 minute warning and a 5 minute warning. o Any exam submitted late may receive a 1% Penalty per minute late Getting help: You may ask only your TA for help. If it is something simple, such as a clarification (ex. what does this mean? ), the TA can help you. If you ask for help with errors, or with solving the problem, the TA may say: - I can t help you with that (especially for major solutions to a problem). Or - I will sell you a hint for a certain number of points. If you agree to the purchase, the TA will deduct those points from your final grade on the exam, and will type something for you on the keyboard. The answer is not guaranteed to solve all your problems for you. Specifications: - Write and execute a complete C++ program in MS Visual Studio according to the following specifications. - Programs that do not compile and execute may not receive a passing grade (-40%) - You may leave some code in comments (along with an explanation to the grader), if needed, for partial credit. - You will not be graded on Style, with the exception of: o Using constants where instructed. You may use constants elsewhere, but not required. o Make a comment box at the top with your name and CS 215 Lab Exam 1 1. Write a function that is given a chemical symbol (a string) and alters an element name and atomic number. Lookup and assign the name and atomic number from the table to the right. When the symbol given is not on the table, set the name to Error and the atomic number to -1. Examples: Given: O Alter name= Carbon, atomicnum=6 Given: Na Alter name= Sodium, atomicnum=11 Given: Br Alter name= Error, atomicnum=-1
2. Write a function that is given the length and width of a rectangle. These values may contain decimal points. Calculate and return the area of the rectangle. Formula: area = length X width Example: given 2.5 and 4, returns 10.0 3. In main(), test the first function: USING A LOOP, repeat the following test 4 times: Ask the user to enter a chemical symbol. Invoke the function, giving it the symbol entered, and print the name and atomic number set by the function. Declare and use a constant for the number of tests! (4) Example execution: Enter symbol: O Main: name=oxygen num=8 Enter symbol: Na Main: name=sodium num=11 Enter symbol: Br Main: name=error num=-1 Enter symbol: Fe Main: name=iron num=26 4. In main(), test the second function: Ask the user to enter a length and width. Invoke the function, giving it the length and width entered by the user. Print the area returned. Then ask the user if they want to test again (Y/N). Repeat the test as long as the user responds with Y. Note: there will always be at least one test. When done testing, print the average of all areas calculated. Example execution: Enter length and width: 2.5 4 Main: Area = 10 Test again? (Y/N): Y Enter length and width: 4 5 Main: Area = 20 Test again? (Y/N): Y Enter length and width: 3 10 Main: Area = 30 Test again? (Y/N): N Average area = 20 (10 + 20 + 30) / 3
5. In main(), ask the user to enter a chemical symbol. Invoke the first function, giving it the symbol entered. Validate the user s input by repeating the question until the user enters a known symbol (one on the chart in question 1). Note: you should not directly check if the user entered C, H, O, Na, etc! Instead, use the information set by the function! (the function already does the work of checking if it s a valid symbol it sets the atomic number to -1 for an unknown symbol). Once a known symbol is entered, print the name and atomic number. Example execution: Enter symbol: xx Unknown symbol! Enter a known value! Enter symbol: Br Unknown symbol! Enter a known value! Enter symbol: He Unknown symbol! Enter a known value! Enter symbol: O Main: name=oxygen num=8 End the execution with the normal PAUSE. Overall sample execution:
Overall sample execution: Solution/Key: // -------------------------------------------------------------------------- // CS 215 Lab Exam 1 (Sec 006 & 007) // <student's name> //--------------------------------------------------------------------------- // 4 points for comment box - must have "CS 215 Lab Exam 1" and student name #include <iostream> #include <string> using namespace std; const int NUM_TESTS = 4; // 3 pts - must be global // ----------------------------- QUESTION 1 --------------------------- // 2 points: void return type // 1 point: a good function name // 2 points: string arg // 2 points: string & arg // 2 points: int & arg void chemlookup(string sym, string & name, int & anum) { // 15 points: sets name and anum correctly if (sym == "C") { name = "Carbon"; anum = 6; else if (sym == "H") { name = "Hydrogen"; anum = 1; else if (sym == "O") { name = "Oxygen"; anum = 8; else if (sym == "Na") { name = "Sodium"; anum = 11; else if (sym == "Cl") { name = "Chlorine"; anum = 17; else if (sym == "Fe") { name = "Iron"; anum = 26; else { name = "Error"; anum = -1;
//------------------------------- QUESTION 2 --------------------------- // 2 points: return type double or float // 1 points: good function name // 2 points: double/float length // 2 points: double/float width double rectarea(double length, double width) { // 4 points: calculates and returns area correctly return (length * width); int main() { // 1 point //------------------------------- QUESTION 3 --------------------------- string symbol, name; int atnum; // 4 pts: must use a LOOP to repeat 4 times // 2 pts: must use a CONSTANT for the 4 for (int i = 1; i <= NUM_TESTS; i++) { cout << "Enter symbol: "; s cin >> symbol; s chemlookup(symbol, name, atnum); // 4 pts cout << endl; cout << "Main: name=" << name << " num=" << atnum << endl; // 4 pts //------------------------------- QUESTION 4 --------------------------- string ans; double area, length, width, avg; double sum = 0; // 2 pts: init sum = 0 int count = 0; // 2 pts: init count = 0 do { // or WHILE that repeats at least once cout << "Enter length and width: "; s cin >> length >> width; s area = rectarea(length, width); // 3 pts sum += area; // 2 pts count++; // 2 pts cout << "Main: Area = " << area << endl; cout << "Test again? (Y/N): "; cin >> ans; while (ans == "Y"); // 3 pts avg = sum / count; s cout << "Average area = " << avg << endl << endl; // 2 pts //------------------------------- QUESTION 5 --------------------------- cout << "Enter symbol: "; cin >> symbol; chemlookup(symbol, name, atnum); // 3 pts while (atnum == -1) { // 3 pts cout << "Unknown symbol! Enter a known value! \n"; s cout << "Enter symbol: "; cin >> symbol; chemlookup(symbol, name, atnum); cout << "Main: name=" << name << " num=" << atnum << endl; // 2 pts system("pause"); return 0;