CSC 101 exam 2 study guide

¡Supera tus tareas y exámenes ahora con Quizwiz!

...

What is the general syntax for processing all the elements of an array 1 by 1?

a runtime error would occur

how would you recognize an out of bounds issues in a snippet of code?

After the body executes, the for-loop then changes the count variable in some way. The step should change the count in a way in which it approaches the end number defined in the expression.

How does a for-loop cycle through the step phase?

This is the expression the for-loop tests to determine whether to execute the body again. If true, the body/step phases repeat. Defines the ending number the count variable should reach to stop the loop.

How does a for-loop cycle through the expression test phase

return value;

Can you generally define the syntax for creating a value returning function?

Yes, of course.

Can you write a function in C++?

yes

Can you write a loop in C++?

In this phase, the for loop creates and initializes a variable that is used to keep up with the current count of the repetition. It defines the starting number the for-loop will begin counting at. This stage is NOT repeated, unlike the other stages, and therefore only executes once at the beginning of the loop's execution.

How does a for loop cycle through the initialization phase?

Passing by value is when an argument's value is copied to the parameter variable. Passing by reference is when the source variable, where the function was called is "attached" to the parameter it is passed to.

Explain the difference between passing by value vs passing by reference.

It would be better to use a constant integer variable to store the size of the array because it makes it more readable and easier to handle.

Explain why it would be better to use a constant integer variable to store the size of the array

By using the string like an array, and their characters are lined up in memory like a 1D array. the individual characters are printed in numerical order.

How can we access the individual characters for strings?

By invoking the length () method on the string object and store the result in an integer variable

How can we get the size of a string?

You can initialize an array with a set of values by declaring it as follows: double num[10]= {2, 5, 3, 9, 1, 22, 44, 90, 45, 1}; or also by declaring the array as follows: double num[10]={0};

How can you initialize an array with a set of values?

cout <<

How do we write an output statement in C++? What operation/function would allow us to do this?

Like the while loop, a for-loop also has a body that defines what the for-loop does. It will be repeated the number of times the for-loop counts from start to end.

How does a for-loop cycle through the body phase?

A. First we need to declare 2 variables: 1. total - used to store how many times the program needs to loop 2. counter - used to keep up with a running tally of the number of times a loop repeats. B. Next, ask the user to enter a number C. After we get our input, we will then test the while loop's conditional expression. Just like the if statement the while loop's body will only execute if the expression is false D. Can insert an increment to increase the value of a variable by 11. Finally, a loop control variable will help the loop to execute.

How does a while loop execute?

The element arrangement is different in a 2-dimensional array when compared to a 1-dimensional array due to the 2-dimensional array's ability to store data in a tabular format...

How is the element arrangement different in a 2-dimensional array as compared to a 1-dimensional array?

for (int count = start; counter expression; cout += n) { //body statements }

How would you generally type a for loop?

Functions allow you to divide a complicated program into smaller, more manageable pieces, which have the following benefits: Allow you to focus on a small part of the program rather than the whole solution. This would allow us to also find errors easier, as programs can now be tested by function rather than all at once. Support better teamwork, as function construction can be assigned to each team member. Having team members work on different functions of the program will make implementation faster.

Identify some of the reasons that we may need to write our programs as a set of functions, as opposed to writing the entire program in main.

Yes in the order of precedence you can

If given a logical expression, can you evaluate it?

Yes.

If given a program with a function, can you analyze/trace its execution?

the identifier, parameters, and how it is accessed

If given a program with a set of variables, functions, or other identifiers, how can you determine the scopes of each?

The index for the first element of the array of size 8 would be as follows: The index of the first element of the array is as follows: my_array[0]=34; The index of the last element of the array is as follows:my_array[8]=48; The index of the 4th element of the array is as follows:my_arrray[4]=22;

If we had an array of size 8, what is the index of the first element of the array? What is the index of the last element of the array? What is the index of the 4th element of the array?

....

If we had two arrays, arr1 and arr2, and we tried to copy the elements of arr1 to arr2, would "arr1 = arr2;" be valid C++ code? If not, how could we perform a copy operation on 2 arrays?

The find method is a string method that returns that index of the first occurrence of the provided string. int index = message.find(".");

If we wanted to know the first position of a given character in a string, what function would allow us to do that?

using std::ifstream;

If we wanted to perform file input, we would have to use which stream type?

using std::ofstream;

If we wanted to perform file output, we would have to use which stream type?

use a position number in erase to remove a character at a specific position. message.erase(index, 1);

If we wanted to remove a character at a specific position, what function would allow us to do that?

The general logic for processing a 2D array is by using a nested loop

What is the general logic for processing/traversing a 2D array?

&&

The "and" operator for boolean and logic expressions

!

The "not" operator in boolean and logic expressions

Do-while loop

To be used when the number or repetitions cannot be calculated, but you know the body needs to execute at least once.

The logical operators, like the relational operators, both evaluate to true or false. However, the "and" and "or" operators are used to "chain" together to form complex logical/relational expressions. The syntax for the logical operators uses the values: "!, &&, and ||"

What are the Boolean(logical) operators in C++?

1. Initialization 2. Condition/Expression 3 Body 4. Step

What are the four phases of a for loop?

Relational operators control logical expressions which compare two values to each other. Their purpose is that when the expression is evaluated the result will be used to determine which statements get executed next.

What are the relational operators in C++ and what are their purposes?

single dimensional array - an array in which the components are arranged in a list form Multi-dimensional array - an array in which the components are arranged in a table format.

What are the two types of arrays we looked at in this chapter and how are the arranged in each?

Whenever an array's index is out of bounds, it means that the program could have encountered a runtime error. since it tried to illegally access protected memory not assigned to the array, or even manipulate memory cells that were not meant to be changed.

What does it mean for an arrays index to be out of bounds?

Concatening two strings is combining them together as one whole string. We can concatenate two strings in C++ by using the plus(+) operator and using the append method

What does it mean to concatenate two strings? How can we do so in C++?

A new file will be placed in that location

What happens if we attempt to write to a file that does not exist in C++?

it gets deleted from the memory

What happens to a variable once its scope expires?

The program can experience a runtime error

What happens when we attempt to read from a file that does not exist in C++?

Boolean is a built-in data type to C++ for storing the results of a logical expression. Boolean variables can be created with the data type and store either true or false.

What is a Boolean variable? What two possible values can a Boolean hold?

A post-test loops, where the body is executed before the expression.

What is a do while loop? How would you generally use it? and how is it different from other loops?

The for loop, is a specialized form of a while loop that is used to simplify the creation of a counter-controlled loop.

What is a for loop? What is it primarily used for?

An expression that evaluates to true or false.

What is a logical expression?

the variable that determines how the loop will be executed

What is a loop control variable?

Alternatively referred to as a substr, a prefix or suffix of any string. first_word = message.subst(0, index_space);

What is a sub string? What function would allow us to create a substring of another string?

The switch statement in C++ is the best alternative to the lengthy if statements that are used to compare a variable to different integral values. It is a multi-way branch statement. The switch statement is the control statement that allows any value to change the control of the execution. The body of each case does NOT need curly braces if there are two or more statements in it, unlike the if/else if. All cases MUST have the break keyword proceeding each set of statements for that case. Without the break keyword, all cases will be executed, starting with the first matching case.

What is a switch statement?

A function that does not return a value when its called, to define a void function we would use this syntax as an example: void function_name(int a, int b, int c, ...) { // statements } (where the return type is specified as void)

What is a void function? How would you make a void function in C++?

The while loop is a repetitive structure that allows us to execute a body of code repeatedly, as long as the expression continues to evaluate to true. while (expression) { //statements to repeat }

What is a while loops and how would you generally type it?

each item in an array

What is an element in an array?

An if-else statement controls conditional branching. Statements in the if-branch are executed only if the condition evaluates to a non-zero value (or true ). If the value of condition is nonzero, the following statement gets executed, and the statement following the optional else gets skipped. An else statement would evaluate when a problem requires you to base your results on more than two criteria.

What is an else if statement? When would an else if evaluate?

A control structure in a programming language is a type of construct that provides alternatives to sequential programming law.

What is control structure in programming?

Identifiers declared outside of every function definition

What is global scope?

Identifiers declared within another body of code (function, loop, if statement, class)

What is local scope?

All identifiers (variables, functions, arrays, classes, and so on) all have one that specifies where that identifier is accessible/ visible. We have one of the two types of scopes in C++ 1. local 2. global(the one we use)

What is scope in C++?

A function in mathematics is considered a rule or correspondence/mapping between values, called the function arguments, and the unique values of the function associated with the arguments.

What is the definition of a function?

A collection of a fixed number of components (also called elements) of the same data type and in sequential memory spaces.

What is the definition of an array?

The else statement will execute its body when the expression evaluates to false. When a programming situation needs you to choice between a two way selection.

What is the else statement? When would the body of an else statement be executed?

a "self-contained" module of code that accomplishes a specific task

What is the formal definition of a program function?

In C++, we use an if statement to incorporate a one-way selection in a program. When an if statement's body has only 1 line, you may leave off the surrounding curly braces. However, if you intend to write an if statement body that has more than one line of code, then you must include curly braces.

What is the if statement? When would the body of an if statement be executed?

The fstream library

What is the name of the library that allows us to perform file input/output in C?

The reason we had to do this is due to the way C++ acknowledges valid functions. Functions are only valid/usable on the lines they are defined and any other functions below them in your cpp file. This means if we defined main first, we would not be able to call larger inside main. However, you can specify a function prototype of larger to give main and any other function above it the ability to call that function.

What is the purpose of a function prototype?

The syntax for passing an array to a function is as follows:return_type function_name(datatype array_name[ ], int array_size) {//do something w array....}

What is the syntax for passing an array to a function?

C++ has a library of functions that help us test the type of characters in a string. These functions included in the 'cctype" include statement.

What library of functions allow us to compare individual characters?

1. Count-controlled: the while loop iterates based on some beginning number and does not stop repeating until the LCV reaches some ending number. 2. Sentinel-controlled: types of loops that are used to input values from the user continuously 3. flag-controlled: loops that used a boolean to control the loop

What types of loop control variables did we study in this chapter? What scenarios would you use each in?

1. Value-returning functions: a function that returns some type of data/value back to where its called 2. Void functions - a function that does not return a value/some type of data 3. Return type - defines the data type of the value that the function will return. 4. value-returning

What types of user defined functions can we create?

while loop

When the number of repetitions cannot be predicted with the possibility of the loop having no repetitions

for-loop

When you know, or can calculate in advance the number of repetitions needed.

using namespace std; cout << "It is a nice day outside!" << endl; cout << "I think I am going to go for a swim!" << endl;

Write 1 - 3 lines of code that would output the following message to the user: "It is a nice day outside! I think I am going to go for a swim!"

yes depending on the type of loop that is used

can you read/analyze a loop in C++ and determine its purpose and resulting execution?

Yes, it can be done so by assigning all elements to the same value in the array.Ex: double num[10] = {0};

can you write a line of code that can access element 3 in an array? (size 8)

||

the "or" operator in a boolean and logic expression


Conjuntos de estudio relacionados

Interior/Exterior Polygon Angles

View Set

Chapter 25: The Fetal Face and Neck

View Set

Wk 13 - Glycolysis and Gluconeogenesis ?'s

View Set

Rock Music Styles A History Ch 6

View Set

chapter 62 ~ management of pts w cerebrovascular disorders

View Set