0% found this document useful (0 votes)
13 views

Assignment 1

The document provides an overview of assignments for an introductory C++ course. It includes questions about preprocessing statements, functions, literals, and variables with answers provided. Key topics covered are function declarations and definitions, the main function, integer vs floating-point literals, and initializing variables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 1

The document provides an overview of assignments for an introductory C++ course. It includes questions about preprocessing statements, functions, literals, and variables with answers provided. Key topics covered are function declarations and definitions, the main function, integer vs floating-point literals, and initializing variables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ECE 150 Fall 2022

Assignment 1

Logistics and academic conduct


1. Give two examples of how you could assist a peer of yours on projects without
violating the academic conduct policies for this course.
- Assist in thought process by guiding them through the problem without directly revealing
the answer
- Assist with syntax errors

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.*/

- Parts highlighted in yellow are preprocessing statements

3. How many blocks of statements are there in the following program.


// 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;
{ std::cout << “a”;
{ std::cout << “1”; std::cout << “2”; } std::cout << “b”;
}
return 0;
}

- There are 3 blocks of code, one for each pair of brackets

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

2. What information can a function declaration convey to the compiler?


- The function declaration tells the compiler the function name, return type and parameters

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;
}

- There are 2 function declarations and 3 function definitions

4. Explain why the main(...) function is special in C++?


- Main() is the main method in c++ meaning that it is where the compiler runs the program
regardless of where it is located within the source code

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! \\\\' '

std::cout << \' \'\\/_\\/. Welcome to ECE 150! \\\\\\\\\' \'

Variables and initializations


1. Why does one need to use variables in a program?
- We need a way to store values in memory so we can access them later

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}

4. What are the differences between the following three statements?


int n; => Declares a local integer with whatever it is at the memory location
int n{};=>Declares a local integer with a default value
int n{0};=>Initialize a local integer with a default value of 0

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;

You might also like