Chapter 8 Exercises (Arrays) 2.1 An array is a data structure which stores one o
ID: 3625668 • Letter: C
Question
Chapter 8 Exercises (Arrays)2.1 An array is a data structure which stores one or more values all of the same ___ ___ (2 words; hint: like int or double). Each array location which stores a value is called an ___.
2.2 Each element of an array is referenced by an ___ (which is also called a ___).
2.3 Write a C++ statement which defines an array of ints named A of size 10. When we define an array by specifying the size of the array at compile-time (i.e., at the time we write the code), the array is said to be ___ allocated. If we define the array and specify the size at run-time, then the array is allocated ___.
2.4 Write a C++ statement which assigns 100 to the fifth element of A.
2.5 Write a C++ statement which defines a double variable named avg and assigns to avg the average of the first and last elements of A. Do not use integer division when calculating the average.
2.6 Write a for loop which computes the sum of the elements of A and stores the sum into a defined int variable named sum.
2.7 Write a for loop which displays the elements of A on the output window in reverse order. Output one integer per line.
2.8 Arrays cannot be assigned using the assignment operator = in C++, e.g., A = B; is illegal if A and B are both arrays. Rather, to assign one array to another array, we have to write a function which copies the elements one-by-one. Write a complete C++ function named void ArrayCopy(int Dest[], int Src[], int Size) which "assigns" the source array Src to the destination array Dest by copying each element of Src to Dest. The sizes of Src and Dest are specified by the Size input parameter.
2.9 In mathematics, a vector is a one-dimensional list containing one or more values, e.g., v = < 3.4, -2.2, 1.1 > would be a vector storing three real numbers. Vectors of real numbers are easily implemented in C++ as one-dimensional arrays of doubles. Given two same-sized vectors v and w, the dot product of v and w, denoted v · w is the sum of the products of corresponding terms. For example, if v = < 1, 2, 3 > and w = < 2, 0, -1 >, then v · w = 1 · 2 + 2 · 0 + 3 · -1 = -1. Write a complete C++ function named double DotProduct(double v[], double w[], int Size) which calculates and returns the dot product of v and w.
2.10 The C programming language (on which C++ is based) does not have a string data type or a string class like C++ does. Rather, strings in C are represented as one-dimensional arrays of characters with a null character '' stored at the end of the string. These are called C-strings1. Shown below is the C-string representation of variable name which is defined on the left as a onedimensional array of char. The length of a C-string is the number of characters in the string excluding the null character; the null character is not part of the string, but rather serves to indicate where the string ends in memory. This is necessary because the C-string array may actually be larger than the C-string contained within it, i.e., there may be some wasted space. For this exercise, write a complete C++ function named int strlen(char s[]) which calculates and returns the length of the C-string stored in C-string variable s. Note that s may be larger than the string contained within it, but it will not be smaller. Hint: write a for loop and let the index variable i vary from 0, 1, 2, .... Stop looping when s[i] is the null character.
char name[] = "Wilma";
2.11 As we saw in Exercise 2.8, it is not possible to assign one array to another using the assignment operator =. Therefore, if s1 and s2 are two C-string arrays, then to assign s2 to s1 by writing s1 = s2; would not work. Rather, a function has to be written and called which copies one string to another. Write the code for a function void strcpy(char Dest[], char Src[]) which copies the Cstring stored in the Src array to the C-string array Dest.
2.12 A two-dimensional array is one with two dimensions: rows and columns. Write a statement which defines a two-dimensional array of chars named C. The dimensions of C are five rows and seven columns.
2.13 Write nested loops which will initialize C by setting each element to the '*' character.
2.14 A mathematical matrix of real numbers is essentially a two-dimensional array of doubles. A square matrix is one with the same number of rows as columns, e.g., a 3x3 or 100x100 matrix. An identity matrix is a square matrix where all of the elements are zero except for the elements along the main diagonal, i.e., the elements M[i][j] = 0 when i ? j and M[i][j] = 1 when i = j. Write a complete C++ function void Identity(double M[][SIZE]) which initializes M to be the SIZE x SIZE identity matrix (assume SIZE is an integer constant defined elsewhere and is greater than or equal to one). The number of rows in M is SIZE and the number of columns is also SIZE. Note: when a two-dimensional array is passed as a parameter to a function, in the function header the number of rows does not need to be specified, but the number of columns must be specified. The reason is explained in your book, and it essentially boils down to this: in order to locate element M[r][c] in memory, the compiler must generate code which calculates the address of M[r][c]. That calculation (or formula) depends on the compiler knowing the number of columns in M. It does not involve the number of rows, which is why we do not need to specify the number of rows in M in the function header.
2.15 During certain matrix operations, it is necessary to swap two rows of a matrix M. For example, if M has five rows, we may wish to swap the elements in rows 0 and 4. This can be done with a single for loop and a swap routine (i.e., temp = x; x = y; y = temp;). Write the code for a function void SwapRows(double M[][SIZE], int row1, int row2) which swaps rows row1 and row2 of M which has SIZE columns. Note: this function does not need to know the number of rows in M because the code that you will write does not involve the number of rows.
Explanation / Answer
2.1 An array is a data structure which stores one or more values all of the same __type_ ___ (2 words; hint: like int or double). Each array location which stores a value is called an _address__.
2.2 Each element of an array is referenced by an _*__ (which is also called a _pointer__).
2.3 Write a C++ statement which defines an array of ints named A of size 10. When we define an array by specifying the size of the array at compile-time (i.e., at the time we write the code), the array is said to be static___ allocated. If we define the array and specify the size at run-time, then the array is allocated _dynamic__.
int A[10];
2.4 Write a C++ statement which assigns 100 to the fifth element of A.
A[4] = 100;
2.5 Write a C++ statement which defines a double variable named avg and assigns to avg the average of the first and last elements of A. Do not use integer division when calculating the average.
double avg = (A[0] + A[9] )/ 2.0;
2.6 Write a for loop which computes the sum of the elements of A and stores the sum into a defined int variable named sum.
int sum=0;
for(int i=0; i<10; i++)
sum = sum+A[i];
2.7 Write a for loop which displays the elements of A on the output window in reverse order. Output one integer per line.
for(int i=9; i>=0; i--)
cout << A[i] << endl;
2.8 Arrays cannot be assigned using the assignment operator = in C++, e.g., A = B; is illegal if A and B are both arrays. Rather, to assign one array to another array, we have to write a function which copies the elements one-by-one. Write a complete C++ function named void ArrayCopy(int Dest[], int Src[], int Size) which "assigns" the source array Src to the destination array Dest by copying each element of Src to Dest. The sizes of Src and Dest are specified by the Size input parameter.
void ArrayCopy(int Dest[], int Src[], int Size)
{
for(int i=0; i<Size; i++)
Dest[i] = Src[i];
}
2.9 In mathematics, a vector is a one-dimensional list containing one or more values, e.g., v = < 3.4, -2.2, 1.1 > would be a vector storing three real numbers. Vectors of real numbers are easily implemented in C++ as one-dimensional arrays of doubles. Given two same-sized vectors v and w, the dot product of v and w, denoted v · w is the sum of the products of corresponding terms. For example, if v = < 1, 2, 3 > and w = < 2, 0, -1 >, then v · w = 1 · 2 + 2 · 0 + 3 · -1 = -1. Write a complete C++ function named double DotProduct(double v[], double w[], int Size) which calculates and returns the dot product of v and w.
double DotProduct(double v[], double w[], int Size)
{ double dotpro;
for(int i=0; i<Size; i++)
dotpro = dotpro + v[i]*w[i];
return dotpro;
}
2.10 The C programming language (on which C++ is based) does not have a string data type or a string class like C++ does. Rather, strings in C are represented as one-dimensional arrays of characters with a null character '' stored at the end of the string. These are called C-strings1. Shown below is the C-string representation of variable name which is defined on the left as a onedimensional array of char. The length of a C-string is the number of characters in the string excluding the null character; the null character is not part of the string, but rather serves to indicate where the string ends in memory. This is necessary because the C-string array may actually be larger than the C-string contained within it, i.e., there may be some wasted space. For this exercise, write a complete C++ function named int strlen(char s[]) which calculates and returns the length of the C-string stored in C-string variable s. Note that s may be larger than the string contained within it, but it will not be smaller. Hint: write a for loop and let the index variable i vary from 0, 1, 2, .... Stop looping when s[i] is the null character.
char name[] = "Wilma";
int strlen(char s[])
{
int i=0;
for(i=0; s[i]!='', i++);
return i;
}
2.11 As we saw in Exercise 2.8, it is not possible to assign one array to another using the assignment operator =. Therefore, if s1 and s2 are two C-string arrays, then to assign s2 to s1 by writing s1 = s2; would not work. Rather, a function has to be written and called which copies one string to another. Write the code for a function void strcpy(char Dest[], char Src[]) which copies the Cstring stored in the Src array to the C-string array Dest.
void strcpy(char Dest[], char Src[])
{
int i;
for(i=0; Src[i]!=''; i++)
Dest[i] = Src[i];
Dest[i] = ''; // Append null character;
}
2.12 A two-dimensional array is one with two dimensions: rows and columns. Write a statement which defines a two-dimensional array of chars named C. The dimensions of C are five rows and seven columns.
char C[5][7];
2.13 Write nested loops which will initialize C by setting each element to the '*' character.
for(int i=0; i<5; i++)
for(int j=0; j<7; j++)
C[i][j] = '*';
2.14 A mathematical matrix of real numbers is essentially a two-dimensional array of doubles. A square matrix is one with the same number of rows as columns, e.g., a 3x3 or 100x100 matrix. An identity matrix is a square matrix where all of the elements are zero except for the elements along the main diagonal, i.e., the elements M[i][j] = 0 when i ? j and M[i][j] = 1 when i = j. Write a complete C++ function void Identity(double M[][SIZE]) which initializes M to be the SIZE x SIZE identity matrix (assume SIZE is an integer constant defined elsewhere and is greater than or equal to one). The number of rows in M is SIZE and the number of columns is also SIZE. Note: when a two-dimensional array is passed as a parameter to a function, in the function header the number of rows does not need to be specified, but the number of columns must be specified. The reason is explained in your book, and it essentially boils down to this: in order to locate element M[r][c] in memory, the compiler must generate code which calculates the address of M[r][c]. That calculation (or formula) depends on the compiler knowing the number of columns in M. It does not involve the number of rows, which is why we do not need to specify the number of rows in M in the function header.
void Identity(double M[][SIZE])
{
for(int i=0; i<SIZE; i++)
for(int j=0; j<SIZE; j++)
if(i==j) M[i][j] = 1;
else M[i][j] = 0;
}
2.15 During certain matrix operations, it is necessary to swap two rows of a matrix M. For example, if M has five rows, we may wish to swap the elements in rows 0 and 4. This can be done with a single for loop and a swap routine (i.e., temp = x; x = y; y = temp;). Write the code for a function void SwapRows(double M[][SIZE], int row1, int row2) which swaps rows row1 and row2 of M which has SIZE columns. Note: this function does not need to know the number of rows in M because the code that you will write does not involve the number of rows.
void SwapRows(double M[][SIZE], int row1, int row2)
{
int tmp;
for(int i=0; i<SIZE; i++)
{
tmp = M[row1][i];
M[row1][i] = M[row2][i];
M[row2][i] = tmp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.