













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An algorithm to determine whether a given year is a leap year or not. It provides a step-by-step approach to evaluate the year by dividing it by 4, 100, and 400, and returning the appropriate boolean value. The document also includes several example years to demonstrate the application of the algorithm. The content covers fundamental programming concepts such as conditional statements, logical operations, and algorithm design, making it a valuable resource for students learning programming, particularly in the context of c++.
Typology: Study Guides, Projects, Research
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Definitions - A set of instructions written in a specific programming language that causes a computer to perform a specific task is referred to as COMPUTER PROGRAM. RAW FACTS are referred to as date while PROCESSED FACTS are referred to as information. A language with strict grammar rules, symbols and special words used to construct a computer program is a PROGRAMMING LANGUAGE. A step-by-step list of instruction that, when carried out, produces a solution to a problem is referred to as ALGORITHM. Given the algorithm in the leap year case study Divide the year by 4 and if the remainder isn't 0Return false (The year is not a leap year)Otherwise, divide the year by 100 and if the remainder isn't 0,Return true (The year is a leap year)Otherwise, divide the year by 400 and if the remainder isn't 0,Return false (The year is not a leap year)Otherwise, Return true (the year is a leap year) Decide whether the following years are leap years: a. 1900 b. 2000 c. 1996 d. 1998 - a. 1900 No b. 2000 Yes c. 1996 Yes d. 1998 No
Given the algorithm from the leap year case study with the lines numbered as follows:
LESSON 2 -
c. 25 % 7 + 9. e. int(15.0 + 12.0 * 2.2 - 3 * 7) g. 18 / 1.0 - a. 21 Explanation of result: 27 + 8 / 5 - 7 = 27 + 1 - 7 = 28 - 7 = 21 b. 21.6 Explanation of result: 27.0 + 8.0 / 5.0 - 7.0 = 27.0 + 8.0 / 5.0 - 7.0 = 27 + 1.6 - 7 = 28.6 - 7 = 21. c. 13.0 Explanation of result: 25 % 7 + 9.0 = 4 + 9.0 = 13. e. 20 Explanation of result: int(15.0 + 12.0 * 2.2 - 3 * 7) = int(15.0 + 26.4 - 21) = int(41.4 - 21) = int(20.4) = 20 g. 18. Given int variables called dollars, quarters, dimes, nickels and pennies, write an expression that computes the total amount of the money represented in the variables. The result should be an integer value representing the number of pennies in the total. - dollars * 100 + quarters * 25 + dimes * 10 + nickels * 5 + pennies Write expressions that implement the following formulas: a. 3X + Y b. A² + 2B + C c. ((A + B) / (C - D)) * (X / Y) d. ((A² + 2B + C) / D) / (XY) - a. 3 * X + Y b. A * A + 2 * B + C c. ((A + B) / (C - D)) * (X / Y) d. ((A * A + 2 * B + C) / D) / (X * Y) Use the C++ precedence rules to remove any unnecessary parentheses from the following expressions: a. ((a * b) + (c * d))
b. ((a * b) / (c * d)) c. ((a + b) + ((c / (d + e)) * f)) d. (((a + b) / (c + d)) * (e + f)) - a. a * b + c * d b. a * b / (c * d) c. a + b + c / (d + e) * f d. (a + b) / (c + d) * (e + f) Select the option that has the same result as the statement a+=5; - a= a+5; Select the option that has the same result as the statement a += bc; - a=a+(bc); Select the option that has the same result as the statement a-= b+c; - This statement will result in a syntax error Select the option that has the same result as the statement a= b+c; - a=a(b+c); Given these assignments to string variables :string1 = "Bjarne Stroustrup"; string2 = "C"; string3 = "programming language"; string4 = "++ because it is a successor to the "; string5 = " named his new "; What is the value of the following expression? string1 + string5 + string3 + " " + string2 + string4 + string2 + " " + string3 + "." - "Bjarne Stroustrup named his new programming language C++ because it is a successor to the C programming language."
white row with interior blanks in the blacks blackBlankRow = BLACK_BLANK + WHITE + BLACK_BLANK + WHITE + BLACK_BLANK + WHITE + BLACK_BLANK + WHITE; // Create a black-white row by concatenating the basic strings blackRow = BLACK + WHITE + BLACK + WHITE + BLACK + WHITE + BLACK + WHITE; // Print five white-black rows cout << whiteRow << endl; cout << whiteRow << endl; cout << whiteBlankRow << endl; cout << whiteBlankRow << endl; cout << whiteRow << endl; // Print five black-white rows cout << blackRow << endl; cout << blackRow << endl; cout << blackBlankRow << endl; cout << blackBlankRow << endl; cout << blackRow << endl; // Print five white-black rows cout << whiteRow << endl; cout << whiteRow << endl; cout << whiteBlankRow << Given that the string variables str1 and str2 contain: "you ought to start with logic" and "ou" Respectively, what is the result of each of the following expressions? a. str1.length() b. str1.find(str2) c. str1.substr(4, 25) d. str1.substr(4, 25).find(str2) e. str1.substr.(str1.find("logic"), 3) f. str1.substr(24, 5).find(str2.substr(0, 1)) g. str1.find("end") - a. 29 b. 1 c. "ought to start with logic" d. 0 e. "log" f. 1 g. string::npos
Write a series of assignment statements that find the first three positions of the string "and" in a string variable sentence. The positions should be stored in int variables called first, second and third. You may declare additional variables if necessary. The contents of the sentence should remain unchanged. - string temp; temp = sentence; first = temp.find("and"); // Get first location // Remove first section of string temp = temp.substr(first + 3, temp.length() - (first
Write the statements necessary to prompt for and input three floating point values, and then output their average. For this exercise, assume that the user has plenty of experience with computers and thus needs minimal instructions. The user should enter all three values on one line. - float number1; float number2; float number3; float average; cout << "Enter the three numbers separated by spaces: "; cin >> number1 >> number2 >> number3; average = (number1 + number2 + number3) / 3; cout << "The average is: " << average << endl; Write a single input statement that reads the following lines of data into the variables streetNum, street1, street2, town, state and zip. 123 Myplace Avenue City, State 12345 - In this solution the comma is included as part of the city name. cin >> streetNum >> street1 >> street2 >> town >> state >> zip; Given the following program: /* Sample program Written in C++ */ int main( ) { int num; cin >> num; num = num + 5; // add 5 to num
num = num * 2; // multiply num by 2 cout << "number = " << num << endl; return 0; } "}" is: - A punctuation symbol Given the following program: /* Sample program Written in C++ / int main( ) { int num; cin >> num; num = num + 5; // add 5 to num num = num * 2; // multiply num by 2 cout << "number = " << num << endl; return 0; } "num" is: - An identifier Given the following program: / Sample program Written in C++ */ int main( )
/* Sample program Written in C++ */ int main( ) { int num; cin >> num; num = num + 5; // add 5 to num num = num * 2; // multiply num by 2 cout << "number = " << num << endl; return 0; } "main" is: - A keyword The identifier char is - Invalid because it is reserved word The identifier char2 is: - Valid since it is syntactically correct The identifier 2char is: - Invalid because it starts with an illegal character Use the following program to answer the question: // This program prompts for and reads four characters // from the keyboard using the extraction operator cin // and then prints them. #include
int main () { char char1, char2, char3, char4; cout << "Input four characters. Press Return." << endl; cin >> char1 >> char2 >> char3 >> char4; cout << char1 << char2 << char3 << char4; return 0; } What will be printed if the input data is ^^^^1b2c↓? (Note: a space is indicated by ^, and a return by ↓)? - 1b2c Use the following program to answer the question: // This program prompts for and reads four characters // from the keyboard using the extraction operator cin // and then prints them. #include
// and then prints them. #include
What will the cout statement below display? (Note: a space is indicated by ^)cout << setw(1) << intNumber << setw(7) << dblNumber << ''; - 5712340.7 Use the following program to answer the question: #include
What will be stored in variables a after the following statements are executed? (? Means we don't know what is stored in that variable.) int a = - 10; a = 10 % 10 / 10; - a= What will be stored in variables a, b after the following statements are executed?int a, b = 1;a = b++; (Note:? means that we don't know what value is stored in a variable.) - a=1 b= Which ones of the following expressions have the same values? i) int(10 / 3) ii) int(9) / int (3.2) iii) int(9 / 3.2) iii) int(12.4) % 4i v) int (15.9) / 4 - i),ii) and v) Read this program and select the output. #include
a *= 2 * b + c; cout << a << '\t' << b << '\t' << c << '\t' << sum << endl; return 0 ; } - 50 6 4.7 22. You are given the String object declarations: String one = "end", two = "file", newStr; Which one of the statements below will result in newStr to have the value "end-of- file"? a) newStr = one + '-' + "of" + '-' + two; b) newStr = one + "-" + "of" + "-"+ two; c) newStr = one + - of- + two; d) newStr = "one"; newStr += "-of-"; newStr += "two"; e) All of the above - a and b Which statements will print the length (number of characters) of the variable name given the following statement? string name = "Jane Doe"; - cout<<"The length of"<<name<<"is"<<name.length()<<"characters"; Given the following declaration statementstring name = "Jane Doe"; What will the following statement output? cout << name.substr(name.find(" ")+1, name.length())<<"," << name.substr(0,1); - Doe.J LESSON 3 -