Leap Year Algorithm, Study Guides, Projects, Research of Computer Communication Systems

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

2023/2024

Available from 08/25/2024

cleana
cleana 🇺🇸

251 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 218 study questions and answers
2024
LESSON 1 -
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Leap Year Algorithm and more Study Guides, Projects, Research Computer Communication Systems in PDF only on Docsity!

COMP 218 study questions and answers

LESSON 1 -

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:

  1. Divide the year by 4 and if the remainder isn't 0
  2. Return false (The year is not a leap year)
  3. Otherwise, divide the year by 100 and if the remainder isn't 0,
  4. Return true (The year is a leap year)
  5. Otherwise, divide the year by 400 and if the remainder isn't 0,
  6. Return false (The year is not a leap year)
  7. Otherwise, Return true (the year is a leap year) Indicate which line of the algorithm tells you whether the date is a leap year in each of the following cases: a. 1900 Line = b. 1945 Line = c. 1600 Line = d. 1492 Line = e. 1776 Line = - a. 1900 Line = 6 b. 1945 Line = 2 c. 1600 Line = 7 d. 1492 Line = 4 e. 1776 Line = 4 How would you extend the leap year algorithm to tell you also when a year is a millennium year (a multiple of 1000)? - Divide by 1000 and see if the remainder is

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

  • 3)); // Find second location of "and" in shortened string second = temp.find("and"); // Remove next section of string temp = temp.substr(second + 3, temp.length() - (second + 3)); // Adjust second to account for deleting first part of string second = second + first + 3; // Locate third "and" and adjust for prior deletions third = temp.find("and") + second + 3; If input data are entered as follows: 10 20 30 40 50 60 70 80 And the input statements that read it are: cin >> a >> b >> c; cin >> d >> e >> f; cin >> a >> b; What are the contents of the variables a, b, c, d, e and f after the statements have been executed? - After 1st statement a = 10, b = 20 and c = 30 After 2nd statement d = 40, e = 50 and f = 60

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 using namespace std;

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 using namespace std; 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; }

// and then prints them. #include using namespace std; int main () { char char1, char2, char3, char4; cout << "Input four characters. Press Return." << endl; cin.get(char1); cin.get(char2); cin.get(char3); cin.get(char4); cout << char1 << char2 << char3 << char4; return 0; } What will be printed if the input data is 31^45↓? (Note: a space is indicated by ^, and a return by ↓)? - 31^ Use the following program to answer the question: #include #include using namespace std; int main () { double dblNumber = 12340.66; int intNumber = 57; cout << fixed << showpoint; ......

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 #include using namespace std; int main () { double dblNumber = 12340.66; int intNumber = 57; cout << fixed << showpoint; ...... } What will the cout statement below display? (Note: a space is indicated by ^)cout << intNumber % 2 << ''; - 1 Use the following program to answer the question: #include #include using namespace std; int main () { double dblNumber = 12340.66; int intNumber = 57; cout << fixed << showpoint; ......

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 using namespace std; int main() { int a, b, sum ; double c ; a = 3; b = 5; c = 14.1; sum = a + b + c; c /= a; b += c - a;

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 -