Podcast
Questions and Answers
What does the line 'std::vector crossed_out = std::vector(n, false);' do?
What does the line 'std::vector crossed_out = std::vector(n, false);' do?
- Creates a vector of Booleans initialized to false. (correct)
- Creates a vector of Booleans initialized to true.
- Creates a vector of characters initialized to empty.
- Creates a vector of integers initialized to zero.
The part 'for (int i=0; i < n; i++)' indicates that the loop will iterate n times.
The part 'for (int i=0; i < n; i++)' indicates that the loop will iterate n times.
True (A)
What is the primary purpose of a vector in programming?
What is the primary purpose of a vector in programming?
To store a dynamically sized collection of elements.
In C++, a vector is part of the ________ Library.
In C++, a vector is part of the ________ Library.
Match the following vector operations with their descriptions:
Match the following vector operations with their descriptions:
What does a precondition define in the context of a function?
What does a precondition define in the context of a function?
The postcondition specifies what is required to be true before the function is called.
The postcondition specifies what is required to be true before the function is called.
What happens if a function is called with invalid preconditions?
What happens if a function is called with invalid preconditions?
The function 'pow' has a postcondition that guarantees the return value is ___ when the precondition is met.
The function 'pow' has a postcondition that guarantees the return value is ___ when the precondition is met.
Which of the following is a recommended practice for defining preconditions?
Which of the following is a recommended practice for defining preconditions?
Match the following terms with their definitions:
Match the following terms with their definitions:
Weak postconditions provide the most detailed information available regarding a function's effects.
Weak postconditions provide the most detailed information available regarding a function's effects.
What should you document to help understand the function's behavior under its preconditions?
What should you document to help understand the function's behavior under its preconditions?
If a function is called with preconditions not met, it may lead to ________ behavior.
If a function is called with preconditions not met, it may lead to ________ behavior.
What is the primary goal when defining preconditions and postconditions for functions?
What is the primary goal when defining preconditions and postconditions for functions?
What is the consequence of using an assert statement with a false expression?
What is the consequence of using an assert statement with a false expression?
Preconditions are always checked using assert statements.
Preconditions are always checked using assert statements.
What are the postconditions in the root function example?
What are the postconditions in the root function example?
The expression used in an assert statement must be convertible to ______.
The expression used in an assert statement must be convertible to ______.
Match the type of assertion with its purpose:
Match the type of assertion with its purpose:
What is the purpose of using assert statements in function 'pow'?
What is the purpose of using assert statements in function 'pow'?
In the gcd function example, the assertion 'assert(x > 0 && y > 0);' confirms that both inputs are non-negative.
In the gcd function example, the assertion 'assert(x > 0 && y > 0);' confirms that both inputs are non-negative.
Why is stepwise refinement important in programming?
Why is stepwise refinement important in programming?
Using assertions helps to enforce ______ during program execution.
Using assertions helps to enforce ______ during program execution.
Match the programming concept with its definition:
Match the programming concept with its definition:
What is the first step in the process of stepwise refinement?
What is the first step in the process of stepwise refinement?
The refinement process only focuses on coding functions without considering data representation.
The refinement process only focuses on coding functions without considering data representation.
What is the purpose of stepwise refinement in programming?
What is the purpose of stepwise refinement in programming?
In the example problem, the rectangles are defined by their top-left corner coordinates and ______.
In the example problem, the rectangles are defined by their top-left corner coordinates and ______.
Match the rectangles with their respective properties:
Match the rectangles with their respective properties:
What is a critical factor to check when determining if two rectangles overlap?
What is a critical factor to check when determining if two rectangles overlap?
The refinement process leads to the development of partial solutions that can be used in other problems.
The refinement process leads to the development of partial solutions that can be used in other problems.
In the main function of the coarse solution, what are the two main actions captured?
In the main function of the coarse solution, what are the two main actions captured?
Width and height inputs for the rectangles may be ______.
Width and height inputs for the rectangles may be ______.
Which of the following is NOT a step in the refinement process?
Which of the following is NOT a step in the refinement process?
What is true about function declarations in programming?
What is true about function declarations in programming?
A function's definition is never considered a declaration.
A function's definition is never considered a declaration.
What must be true about the lines of code that follow a function declaration?
What must be true about the lines of code that follow a function declaration?
The statement 'void B (int i);' is an example of a ___ declaration.
The statement 'void B (int i);' is an example of a ___ declaration.
Match the following terms with their definitions regarding functions:
Match the following terms with their definitions regarding functions:
Flashcards
String
String
A sequence of characters. It can represent words, sentences, or any combination of text.
Vector
Vector
A data structure that stores a collection of elements in a specific order. Each element can be accessed using an index.
Boolean vector
Boolean vector
A data structure that uses a Boolean value (true or false) to indicate whether a specific element is present or not.
Iterator
Iterator
Signup and view all the flashcards
Indexed data structure
Indexed data structure
Signup and view all the flashcards
Precondition
Precondition
Signup and view all the flashcards
Postcondition
Postcondition
Signup and view all the flashcards
Correctness
Correctness
Signup and view all the flashcards
Undefined Behavior
Undefined Behavior
Signup and view all the flashcards
Weak Precondition
Weak Precondition
Signup and view all the flashcards
Strong Postcondition
Strong Postcondition
Signup and view all the flashcards
White Lies
White Lies
Signup and view all the flashcards
Abstraction
Abstraction
Signup and view all the flashcards
Function Documentation
Function Documentation
Signup and view all the flashcards
Reasonable Limits
Reasonable Limits
Signup and view all the flashcards
Assertion
Assertion
Signup and view all the flashcards
Stepwise Refinement
Stepwise Refinement
Signup and view all the flashcards
How to guarantee preconditions in a function?
How to guarantee preconditions in a function?
Signup and view all the flashcards
Using assertions for preconditions and postconditions.
Using assertions for preconditions and postconditions.
Signup and view all the flashcards
Benefits of using assertions
Benefits of using assertions
Signup and view all the flashcards
What is the first step in using stepwise refinement?
What is the first step in using stepwise refinement?
Signup and view all the flashcards
Why are assertions important in programming?
Why are assertions important in programming?
Signup and view all the flashcards
Whatare the potential drawbacks of assertions?
Whatare the potential drawbacks of assertions?
Signup and view all the flashcards
Coarse Solution
Coarse Solution
Signup and view all the flashcards
Function Implementation
Function Implementation
Signup and view all the flashcards
Data Representation
Data Representation
Signup and view all the flashcards
Problem Decomposition
Problem Decomposition
Signup and view all the flashcards
Function Declaration Scope
Function Declaration Scope
Signup and view all the flashcards
Function Definition as Declaration
Function Definition as Declaration
Signup and view all the flashcards
Separate Function Declaration
Separate Function Declaration
Signup and view all the flashcards
Function Prototype
Function Prototype
Signup and view all the flashcards
Function Declaration Syntax
Function Declaration Syntax
Signup and view all the flashcards
Study Notes
Introduction to Computer Science
- Course details: 252-0032, 252-0047, 252-0058
- Authors: Manuela Fischer and Felix Friedrich
- Department: Department of Computer Science, ETH Zurich
- Semester: Fall 2024
Vectors
- Vectors store sequences of homogeneous data.
- Iteration over numbers:
for (int i=0; i<n; ++i) {...}
- Sieve of Erathostenes example: calculates prime numbers less than n.
- Crossing out non-prime numbers using a boolean vector.
- Vector type:
std::vector<T>
where T is the element type. - Vector initialization:
std::vector<T>(n,e)
where n is the number of elements and e is the initial value for each element. Other initialization methods (e.g., initializer lists) are possible. - Element access:
[]
index operator, used for reading and writing elements. std::vector<int> v = std::vector<int>(10); std::cout << v.at(10);
example shows accessing elements using.at()
, which performs bounds checking at runtime. Using[]
might cause undefined behavior if referencing outside container bounds.- Vectors can be initialized with different values (e.g., 0, 2, a direct list of values).
Memory Layout and Properties
- Vectors store elements in contiguous memory.
- Random access is efficient.
Nested Vectors (Vectors of Vectors)
- Used to represent multidimensional structures like tables or matrices.
- Can be initialized using initialization lists or filling with a specific size and default value.
- Access elements using nested indexing:
m[i][j]
for rowi
and columnj
. - Memory storage is flat, row-by-row.
- Example to compute sum of lower triangle of a square matrix using nested for loops.
Beware of Mixed Integer Expressions
- Vector functions often use unsigned integers (e.g.,
v.size()
). - Mixed integer expressions can lead to unexpected behavior (e.g., infinite loops due to unsigned arithmetic—common error).
- Compiler warnings can help detect potential issues. Example
for (int x = 0; a.size()-x-1 >= 0; ++x){ std::cout << x; }
demonstrates an infinite loop (due to the unsigned nature ofa.size()
).
Reference Types
- Pass by reference: Parameter receives the address of the actual argument, allowing modifications to be reflected in the caller.
- Pass by value: Parameter receives a copy of the actual argument, modifications do not affect the original argument.
- Reference types are denoted by
T&
(e.g.,int&
,std::vector<double>&
). - Reference variables act as aliases to the objects they reference. References must have an existing object to reference.
- Return by reference: Function returns a reference, allowing modifications to the original argument directly. Use of reference return type leads to changes to the original object, like
int& inc(int i)
example. - Temporary objects: A reference to a temporary object can lead to undefined behavior. A reference must remain valid for the lifetime of the object it refers to. Example:
int& foo(int i) { return i; }
.
Const References (const T& )
- Passing elements by constant reference (const T&): Prevents accidental modification of the passed object within the function, while still avoiding unnecessary copying of large objects.
- Improves performance by avoiding unnecessary copying (especially for large objects). Example of computing vector average using
const std::vector<double>& values
which prevents modification.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz focuses on the concept of vectors in computer science, covering their storage, iteration, and properties. It includes examples such as the Sieve of Eratosthenes and illustrates the use of nested vectors. Test your understanding of how vectors function and their application in programming.