Assignment 1
Assignment 1
Assignment 1
Statements
1. Explain how you would determine if a statement is a preprocessor statement?
- Preprocessing statements are statements that begin with the # sign
2. In the following program, mark every line that is identified by the compiler as a
preprocessor statement or a comment.
// This is a simple program that does nothing useful.
#include <iostream>
#include <cmath>
//#include <algorithm>
int main() {
std::cout << “#include <iostream>” << “allows me to print to
console” << std::endl;
//cout << “#include <algorithm> gives me algorithms” << std::endl;
return 0;
}
/* This is the end of file.*/
Functions
1. Explain the purpose of a function declaration and its function definition.
- A function declaration tells the compiler that a function will be used later in the program
- The function definitions tells the compiler what the function does
3. How many function declarations and function definitions are there in the
program below?
int one();
bool is_one();
int one() {
return 1;
}
bool is_one(int value) {
return (value == one());
}
int main() {
std::cout << “one?: “ << is_one(5) << std::endl;
return 0;
}
Literals
1. How is an integer literal different than a floating-point literal?
- Integer literals have no decimals while floating-point literals contain decimals or exponents
2. Explain the difference between a statement that prints to the console "1", '1',
and 1.
- “1” prints the string 1
- ‘1’ prints the char 1
- 1 prints the integer 1
3. Write the statement to print the following text to the console where the first
character printed is the apostrophe (that is, no leading spaces). There are three
spaces before the “W” and a single space everywhere else. ' '\/_\/ Welcome
to ECE 150! \\\\' '
2. What are the differences in the following two local variable declarations?
int n; => Declares an integer n with a random value that is stored at the specific memory point
char ch; => Declares a character ch with a random value stored at the specific memory point
3. Write a single statement to initialize a local variable of integer type to the value
150.
- int temp{150}
5. Write a program that has two statements. The first statement declares a local
variable of integer type initialized to the value 10 (similar to question 3), and the
second statement assigns 150 to the local variable.
int temp{150}
temp = 150;