Chapter 6 – Vectors and Arrays 1.Write each of the following instructions. a.Dec
ID: 3775364 • Letter: C
Question
Chapter 6 – Vectors and Arrays
1.Write each of the following instructions.
a.Declare a vector named first_one which contains 10 doubles.
b.Declare a vector named second_one which will contain strings but initially contains none.
c.Assign the value 3.5 to the last element of first_one.
d.Create a new string in second_one and assign it the value of “Jane Dough”.
2.Write a piece of a program to do the following. Set up a vector of strings named student_list. Use a loop to get data to add to this vector. Ask the user to enter a name or control-d to quit. As long as the user does not enter control-d, read the input line and add it to the end of student_list.
3.Rewrite the code from the previous problem. Make it a function which will return a vector.
4.Assume that the vector student_list contains a group of strings. Write a loop that will print each of the names from first to last, one per line.
5.Explain how the push_back and pop_back functions work.
6.Write a function that will accept a vector of doubles. The vector should be passed by reference. It will return 0 if the vector has a length of zero. Otherwise, it will return the largest number in the vector.
7.Write a function that will accept a vector of doubles. The vector should be passed by reference. It will return 0 if the vector has a length of zero. Otherwise, it will return the average of all of the numbers in the vector.
8.Write a function which will accept a vector of integers and return the Boolean value true if the numbers are in sequence from smallest to largest (duplicate values are allowed). The function returns the Boolean value false if any number in the vector is larger than a number which follows it.
9.Write a function which will accept 2 parameters. Each parameter is a vector of doubles. The vectors are of equal length. Compare the corresponding values (i.e. the vector element with the same subscript) and return the number of times that the number in the first vector is larger than the number in the second.
10.Write a function named no_duplicate that accepts 2 parameters. The first is a vector of strings. The second parameter is a single string. The function returns the Boolean value true if the second parameter is not found in the first parameter. Otherwise, the function returns false if the second parameter is found in the vector.
11.Explain what each instruction in the following procedure does.
void get_table(vector<vector <double> >& table)
{
int rows = table.size();
for (int r = 0; r < rows; r++) {
int columns = table[r].size();
for (int c = 0; c < columns; c++)
{
cout << "Enter value for (" << r << ", " << c << "): ";
cin >> table[r][c];
}
}
}
12.Explain what each of the following instructions does.
int main()
{
vector<vector <double> > m;
int rows, columns;
cout << "how many rows?: ";
cin >> rows;
m.resize(rows);
for (int r = 0; r < rows; r++)
{
cout << "how many columns for row " << r << "?: ";
cin >> columns;
m[r].resize(columns);
}
get_table(m);
13.What are some differences between vectors and arrays?
14.Can a function in C++ return an array? Can a function return a vector?
Chapter 7 – Pointers
1.The book talks about creating variables in stack memory and heap memory. Explain how each of them works. Why is it important to distinguish between them?
2.What is the difference between the following statements?
a.Employee harry;
b.Employee* harry = new Employee;
3.What does each of the following instructions do?
a.Product* p = new Product;
b.Employee* boss = NULL;
4.Explain what each instruction in the following does.
#include<iostream>
#include<string>
#include "node.h"
using namespace std;
int main() {
Node* top = NULL;
Node* current = NULL;
string the_name;
cout << "Enter a job name or control-d to quit: ";
getline(cin, the_name);
while (! cin.fail()) {
current = new Node(the_name, top);
top = current;
cout << "Enter a job that precedes the others or ^d to quit: ";
getline(cin, the_name);
}
cout << " Listed in reverse order the jobs are: ";
current = top;
while (current != NULL) {
cout << current->get_name() << endl;
current = current->get_next();
}
cout << "done ";
return 0;
}
5.Why should you never delete an object that was allocated on the stack?
6.Assume that one node may have more than one pointer which points to it. Why does this cause potential problems for deleting the node?
Chapter 9 – Streams
1.Explain what each of the following instructions does.
a.ifstream some_data;
b.some_data.open(“my_stuff.dat”);
c.ofstream more_data;
d.more_data.open(“other_stuff.dat”);
2.Given the instructions in the previous problem, write each of the follow pieces of a program.
a.Write a loop that will read doubles from the input file. Each number will be put into a variable named the_number and then added to a variable named the_total.
When the end of file is reached, print the number using cout.
b.Write a loop that will read strings from cin and write them to the output file until the user enters control-d.
3.When a file is passed as a parameter, why must it be passed by reference?
4.Explain each parameter in the following declaration for the main function.
int main(int argc, char* argv[])
5.What are sequential files? What are “random access files”?
Explanation / Answer
chapter 9
1. in first line ifstream is inputstream and some_data is an object
second line we are opening a file using some_data object.the namr of file is my_stuff.dat
third line ofstrem means output stream,this is for writing operation into a file and other_stuff i an output object
last line we are opening a file other_Stuff.dat for writing using more_data object
3.Toget the location of the fil,we pass it as a reference.we access a file by its address.so we use pointers to access a file.
4.argc is used for passing number of arguments
argv[] is a pointer array which points to each argument passed to the program under consideraton
5.in sequential files we access and write to a file in order from beginning
in random access file enables we can read or write information anywhere in the file.
chapter 7
1.
stack:this is used for static memory allocation and here memory space is fixed.access is fast here
heap:It is used for dynamic memory llocation during run time .Here memory space is randmly taken from disk and it is not fixed.access is slow here
2. here we are creating a normal object in c++
here we are creaing a pointer in java
4.here we never know what happens.this is an undefined and strange behavior.
5.the pointers become dangling
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.