Name Vicki Chen ID 8757122 CS 16 Homework Assignment 05 1 Declare a pointer variable and assign a value to it int v1 p1 v1 0 p1 v1 2 Use the new operator to create a new variable in the freestore p1 new int 3 Write a definition for a type called NumberPtr to be a type for pointers to dynamic variables of type int typedef int NumberPtr 4 Use the NumberPtr type to declare a pointer variable called my point NumberPtr my point 5 Write a definition for pointer variables that will be used to point to dynamic arrays The array elements are of type char Call the type CharArray typedef char CharArray 6 Write code to fill array entry with 10 numbers typed at the keyboard int entry entry new int 10 cout Enter 10 integers n for int i 0 i 10 i cin entry i 7 Describe the benefits of reading numeric data as characters before converting the characters to a number Reading the whole file of data as characters and then converting to numbers is beneficial so that none of the data is lost in the process 8 Give use examples for the atoi atol and atof functions atoi 1234 returns the integer 1234 atol 123 returns 123 atof 9 99 returns 9 99 9 What is the character that ends a C string 0 10 Write code to read an entire line into a string object getline 11 Use the string function at to access individual characters in a string object std cout str at i 12 Write code to convert a string to a C string char c string new char s length 1 13 Declare and initialize a vector of 10 doubles vector double v 10 0 0 14 Write code to increase the size of a vector in at least two different ways v resize 15 Name Vicki Chen ID 8757122 SECOND METHOD append default elements to vector using push back for int i 0 i 5 i v push back 0 0 15 Describe the difference between a vector s size and its capacity Size is not allowed to differ between multiple compilers The size of vector is the number of elements that it contains which is directly controlled by how many elements you put into the vector Capacity is the amount of space that the vector is currently using 16 Identify a possible source of a stack overflow error An error message that says stack overflow is telling you that the computer has attempted to place more activation frames on the stack than are allowed on your system A likely cause of this error message is infinite recursion 17 Write a recursive void function with one parameter a positive integer that writes out that number of s to the screen void stars int n cout if n 1 stars n 1 18 Write write vertical so the digits are output in reverse order void write vertical int n if n 0 return cout n 10 n write vertical n 10 19 Determine the output of this function if called with rose 4 int rose int n if n 0 return 1 else return rose n 1 n The output is 24 20 Write a recursive function definition for the following function int squares int n Precondition n 1 Returns the sum of the squares of numbers 1 through n int squares int n if n 1 return 1 else return squares n 1 n n 21 What does this program print EXPLAIN every line Name Vicki Chen ID 8757122 include iostream includes iostream header files for using cout using namespace std uses std namespace int main main function main part of the program starts here int a 3 3 a is declared as an integer array of size 3x3 initialized with values 1 2 3 4 5 6 7 8 9 int pa 3 a 0 a 1 a 2 initializing pa as a pointer to every row of a int p a 0 initializing p as pointer to first element of a int i declaring variable i as a integer 311 545 779 for i 0 i 3 i for loop incrementing i from 0 to 2 by one cout a i 2 i a i a i i endl first just takes directly from array second takes address because of and third address of the equation a 0 2 3 a 0 1 a i i 1 cout endl goes to next line in output screen for i 0 i 3 i for loop incrementing i from 0 to 2 by one cout pa i p i endl cout endl return 0 return statement that ends the program For every loop iteration it prints the value of pa which holds every row of a so pa i will print first element of every row p prints every element in first row for each iteration So output is 11 42 73 22 What does this program print EXPLAIN every line include iostream includes iostream header files for using cout using namespace std uses std namespace int main main function main part of the program starts here int a 0 1 2 3 4 a is initialized with values 0 to 4 int p a a 1 a 2 a 3 a 4 p holds the address of individual elements of a int pp p pp is pointing to the same memory p points to cout a a endl prints address of array a and value of first element of a cout p p p endl prints address of array p and address a and first value at p which Is address of a and value of first element of a cout pp pp pp endl same as above since pp and p points to the same array a a 1 a 2 a 3 a 4 pp pp points to p 1 cout pp p pp a pp endl pp p index difference of second element of p array and first element of p array 1 pp a is difference of a 1 and a 1 pp value at a 1 1 Name Vicki Chen ID 8757122 pp pp points to 3rd element of p array This is same as pp as is right to left for these and operators cout pp p pp a pp endl pp p index difference of 3rd element and 1st element of p array 2 pp a is difference of a 2 and a 2 pp value at a 2 2 pp pp points to 4th element of p array This is same as pp cout pp p pp a pp endl pp p index difference of 4th element and 1st element of p array 3 pp a is difference of a 3 and a 3 pp value at a 3 3 pp This is same as pp 4th element a 3 is changed to a 4 in p array cout pp p pp a pp endl pp p index difference of 4th element and 1st element of p array 3 pp a is difference of a 4 and a 4 pp …
View Full Document
Unlocking...