CS 1410 Midterm

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How would you generate and print a number between n and m?

#include <random> using namespace std; int n = 75; int m = 100; default_random_engine en; uniform_int_distribution<> dist{n,m}; cout << dist(en);

Given the following variables: string name ="CS1410"; string lang ="C++"; int n =30; double g =92.35; Use a stringstream object that uses the above variables to build the following string: Class CS1410 has 30 students with an avg. grade of 92.35. It teaches C++ programming.

#include <sstream> string name = "CS1410"; string lang = "C++"; int n = 30; double g = 92.35; stringstream ss; ss << "Class " << name << " has " << n << " students with an avg. grade of " << g << ".\n" << "It teaches " << lang << " programming." << endl;

What is the difference between a prefix++x and postfixx++? Use an example statement to demonstrate the difference.

++x means increment comes before x is used; x++ means increment happens after x is used. For example: int x = 6; int y = 10; cout << ++x; // prints 7 cout << y; // prints 10

Give examples of single- and multi-line comments.

// single-line comment /*multiple line comment*/

What would the following code snippets printout? 1. cout <<"The motto is\nGo for it!"; 2. string name ="Keanu"; string greeting ="Hi "+ name; cout << greeting; 3. int balance = 600; if(balance >=0){ balance = balance + (0.02* balance) /12; }else{ balance = balance −15.0; } cout << balance; 4. int nums[] = {2,3,5,7}; for(int n =3; n >=0; n--){ cout << nums[n]; } 5. const char* sptr ="Greetings"; while(*sptr){ cout << sptr++ << endl; }

1. cout <<"The motto is\nGo for it!"; The motto is Go for it! 2. string name ="Keanu"; string greeting ="Hi "+ name; cout << greeting; Hi Keanu 3. int balance = 600; if(balance >=0){ balance = balance + (0.02* balance) /12; }else{ balance = balance −15.0; } cout << balance; 601 4. int nums[] = {2,3,5,7}; for(int n =3; n >=0; n--){ cout << nums[n]; } 7532 5. const char* sptr ="Greetings"; while(*sptr){ cout << sptr++ << endl; } Greetings reetings eetings etings tings ings ngs gs s

How can you add default arguments to a function? Where will the default values be specified? Give an example.

Add the default parameters at the end of the prototype parameter list: For example: void increment(int& x, int by=1); // prototype void increment(int& x, int by){ // definition x = x + by;}

Given the following variables, What is the value of each of these expressions? int x = 5, y = 10, z = -1; int& a = x;

Expression Value x + 2 * 5 + 1 16 (a + 2 ) * 5 + 1 36 (x + 2 ) * (5 + 1) 42 y + a - 16 - z 0 !(z+1) 1 -(x * z) % 2 + z 0 -(a * z) % 2 - z 2 !x || true true a && y && z true x && (y - 10) false a * z / y 0 z <= -x || z >= -y true !(z+1) && 2 * x <= y true ((a && (!z)) || y) true

What is the difference between variables defined in the stack and variables defined in the heap? Give an example of each

Stack variables are managed by C++ and their size must be known at compile time. Heap variables can be of any size. For example: int x = 100; double numbers [] = {3, 4, 6, 7, 8}; They are requested at run time but must be managed by you, the programmer. For example: Time *t = new Time;

What is included in a function's signature? What is the signature of the following function: void mysteryFunction(int& x, int& y) { cout <<"Enter x:\n"; cin >> x; cout <<"Enter y:\n"; cin >> y; cout <<"x + y is "<< x + y << endl; }

The signature of a function contains anything from the function name to the { of its definition. The signature of the function above is: mysteryFunction(int& x, int& y) which does not include the return type.

In the table below, list the primitive data types that C++ supports and define an example variable and an example constant of each type;

Type Example variable Example Constant short short num; const short MAX = 100; char char c = 'A'; const char AT = '@'; int int n = 16; const int M = 100; long long nn = 8L; const long NN = 100L; float float frac = 8.7f; const float FRAC = 3.7F; double double num = 9.12; const double PI = 3.14; bool bool active = true; const bool married = false; string string msg = "Hello!\n" const string alph = "ABC";

Convert the following if-else statement to a switch statement. char choice; cout <<"Enter a choice: "; cin >> choice; if(choice =='a'|| choice =='A'){ cout <<"Add action"; }else if(choice =='r'|| choice =='R'){ cout <<"Remove action"; }else{ cout <<"Invalid action"; }

char choice; cout << "Enter a choice: "; cin >> choice; switch(choice){ case 'a': case 'A': cout << "Add action"; break; case 'r': case 'R': cout << "Remove action"; break; default: cout << "Invalid action"; }

Write an equivalent for loop to the following while loop: unsigned int n = 7; unsigned long factorial =1; while(n >=1){ factorial *= n; n--; } Do the same using an equivalent do-while loop.

for loop: unsigned long factorial = 1; for (unsigned int n = 7; n >=1; n--){ factorial *= n; } do- while-loop: unsigned int n = 7; unsigned long factorial = 1; do { factorial *= n; n--; }while(n >= 1);

Write a loop that iterates over the characters of the following quote and prints them one character per line. string quote ="To be or not to be: that is the question!";

for(char c : quote){ cout << c << endl; }

What are the differences between a pointer and a reference? Give examples of each. A reference is another name of a variable. For example:

int x = 10; int& xRef = x; // xRef is another name for x A pointer is variable whose values must be memory addresses: int x = 10; int* xPtr = &x; // xPtr stores the address of x

Write a function named dateString() that takes a Date argument (see the structure below) and returns a string representation of the given date in the format: MON/DAY/YEAR. (forexample3/12/2018) struct Date { int day; int month; int year; };

string dateString(Date d) { return to_string(d.month) + "/" + to_string(d.day) +"/" + to_string(d.year); }

Convert the following if-else statement to a conditional expression (? :). string m; cout <<"Enter a month: "; cin >> m; string msg; if(m =="June"|| m =="July"|| m =="August"){ msg ="A summer month"; }else{ msg ="Not a summer month"; }

string m; cout << "Enter a month: "; cin >> m; string msg = (m == "June" || m == "July" || m == "August") ? "A summer month" : "Not a summer month";

●Define a double constant named PI and initialize it to3.14159. What happens if you try to change the value of PI? ●Write a prototype for and define a void-returning function named toRadians that takes a double reference angle in degrees and converts to radians using the formula: Radians = Degrees * PI / 180 ●Define a double variable named area. ●Define a double pointer and assign it the memory address of area.

●Define a double constant named PI and initialize it to3.14159. What happens if you try to change the value of PI? const double PI = 3.14159; The compiler won't let you do that. ●Write a prototype for and define a void-returning function named toRadians that takes a double reference angle in degrees and converts to radians using the formula: Radians = Degrees * PI / 180 Prototype: void toRadians(double&); Definition: void toRadians(double& d){ return (d * 3.14159)/180; } ●Define a double variable named area. double area; ●Define a double pointer and assign it the memory address of area. double* dptr = &area;

Given the string "It is a wonderful life!": ●How would you find how many characters it contains? ●How would you convert it to uppercase? What about to lowercase? ●How would you write an empty string

●How would you find how many characters it contains? string s = "It is a wonderful life!"; cout << s.size(); ●How would you convert it to uppercase? What about to lowercase? #include <cstring> string s = "It is a wonderful life!" for(char c : s) { cout << toupper(c); cout << tolower(c); } ●How would you write an empty string ""

A computer can perform the following six kinds of statements/instructions: ●Input: ●Output: ●Arithmetic's: ●Conditionals: ●Loops: ●Jumps: Give an example C++ statement of each kind.

●Input: cin >> x; ●Output: cout << "Hi"; ●Arithmetics: int y =(x + 6) / 7; ●Conditionals: if(x == 1) cout << 1; ●Loops: for(int i = 0; i < 10; i++) cout<< 0; ●Jumps: break; or return;

Answer the following regarding the switch statement: ●What data types can the variable/expression be of? ●What does default do? ●Can curly braces be used inside the cases? ●Do you have to include the break statement? What happens when you don't? ●Do you need a break statement in the default case? Why?

●What data types can the variable/expression be of? int, short, long, char ●What does default do? It runs if no case is matched. ●Can curly braces be used inside the cases? Can but shouldn't ●Do you have to include the break statement? What happens when you don't? In most cases you have to. If you don't, then control bleeds from one case to the next. In other words, the code of more than once case runs. ●Do you need a break statement in the default case? Why? No; you are at the bottom.

Use the structure : enum class CardSuit { CLUBS, DIAMONDS, HEARTS,SPADES }; struct Card { int face; CardSuit suit; }; Notice that a card face can take any value between 1 and 13 with 11 for Jack, 12 for Queen, and 13 for King. ●dynamically create an Ace Of Spades card (using the new operator). Use the->operator to access and print its members to the console. ●statically (in the stack) create a Queen of Diamonds card. Use the dot operator to access and print its members to the console.

●dynamically create an Ace Of Spades card (using the new operator). Use the->operator to access and print its members to the console. Card* ace = new Card {1, CardSuit::SPADES}; cout << ace-> face; if(ace->suit == CardSuit::SPADES) cout << "Spades"; ●statically (in the stack) create a Queen of Diamonds card. Use the dot operator to access and print its members to the console. Card queen{12, CardSuit::DIAMONDS}; cout << queen.face; if(queen->suit == CardSuit::DIAMONDS) cout << "Diamonds";

What C++ function/class/object/utility causes us to use the following header files: ●iosteam ●iomanip ●string ●vector ●cmath ●cstring ●array ●cctype

●iosteam cout ●iomanip setw ●string string ●vector vector ●cmath cos ●cstring strlen ●array array ●cctype isupper

List all the identifiers in the above program. Also which of the following is an invalid C++ Identifier and why? ●x_ ●true ●False ●Two numbers ●*y ●return ●1two ●_num ●this&that

●x_Valid ●true Invalid: keyword ●False Valid ●Two numbers Invalid: space not allowed ●*y Invalid: * is not allowed ●return Invalid: keyword ●1two Invalid: starts with a number ●_num Valid ●this&that Invalid: & not allowed

C++ supports three ways of passing parameters to functions: what are they? Define an example function of each way.

They are 1) pass by value like int max(int x, int y){ return x > y ? x : y; } 2) pass by reference like void swap(int& x, int& y){ int tmp = x; x = y; y = tmp; } 3) pass by pointer like: void swap(int* x, int* y){ int tmp = x; *x = *y; *y = tmp; }

Give examples of using brace-enclosed comma-separated lists of values to initialize arrays of structures at the time of creating them.

Time* times = new Time[3] { { 11, 45, 17 }, { 22, 50, 15 }, { 19, 30, 10 }, }

Give an example of a dynamic array created in the heap. What operator would you use to destroy it and return it back to the operating system?

Time* times = new Time[3]; To destroy it, use : delete[] times;


Kaugnay na mga set ng pag-aaral

Chapter 12 Review Healthcare Information

View Set

Chapter 67 Care of the Normal Newborn Pretest 1

View Set

AP Psychology Chapter 2: Research Methods Multiple Choice Part 2/2

View Set

NCLEX RN - Cultural and Spirituality

View Set

UTHS World Geography A Unit 3 Study Set

View Set

Ch 7 - Survey of Audit, Attest, and Assurance Topics

View Set

Histology Chapter 6 Adipose Tissue

View Set