-
Consider the following function, which takes 2 arrays of integers
and returns their sum as a dynamically allocated array of integers.
int* sumArrays(int* a, int* b, int N){
int* c = new int[N];
for (int i = 0; i < N; ++i)
c[i] = a[i] + b[i];
return c;
}
Does this code compile and run? Explain its errors or disadvantages
and give a correct or improved solution.
-
Implement a function
void addMatrices( double A[], double B[], double C[],
int m, int n, int M, int N ) { ... }
which, given two m by n matrices
A and B
stored in M by N 2-D arrays,
computes the sum C = A + B and stores
it in a 3rd M x N 2-D array.
Receive all the 2-D arrays as 1-D arrays
so that your function can correctly handle any 2-D arrays, irrespective
of the arrays' column dimensions.
-
Can a pointer be passed by reference? If not, explain why not.
If so, briefly describe an example in which passing a
pointer by reference is necessary for correct operation.
-
What is the output of the following program?
#include <iostream>
using namespace std;
int main(){
int i = 5, j = 7, k;
int* p, *q;
p = &i;
q = &j;
cout << i << ' ' << j << endl; //
k = *p;
*p = *q;
*q = k;
cout << i << ' ' << j << endl; //
return 0;
}
-
Explain what the following one-line function does and precisely how it does it.
void myStrcpy( char* a, char* b){ while (*a++ = *b++) ; }
In particular,
- What is the value of *b++?
- What is the boolean value of *a++ = *b++?
- What does the ++ operator increment?
- What is the body of the while loop?
- When and why does the loop terminate?
- What are the function's preconditions?
-
Implement a C++ function to make
histograms with bins of data displayed
vertically instead of horizontally.
-
Implement a C++ program to reverse the order of the elements
in an array, without using another array.
-
Implement the setUnion(...) function for both the
fixed-length-array version and the
variable-length-array version
of the online set class example.
-
Savitch, Ch. 6, Programming Project 3, p. 256.
Implement a menu-driven C++ program to allow users to create
and manipulate points in the plane.
-
Use your solution to the previous exercise to implement a Rectangle
data type as a pair of points, northwest corner and southeast corner.
All rectangles of this type will have their sides aligned with x,y
coordinate axes, by assumption. Give your type enough of a public
interface to make it useful to clients. Include mutators, accessors,
a default constructor, and area() and perimeter()
member functions.
Implement a non-member function
double overlap(const Rectangle&, const Rectangle&) which
computes the area of the overlap region of its two arguments.
Optional Practice Problems
-
Implement a function to enumerate
(list) all permutations
(reorderings) of a given character string. For simplicity,
you may assume all characters in the string are distinct.
How does the running time of your program depend on the length
of the string?
-
Implement a function to enumerate (list) all combinations
(subsets) of user-specified size k of a given character string of
length n > k. For simplicity,
you may assume all characters in the string are distinct.
How does the running time of your program depend on the length
of the string?
-
-
Implement a function
atoi()
to convert an integer stored as a
character string of digits into a actual integer value.
(Implement the function; don't simply call the library version.)
-
Implement a function
itoa()
to convert an integer
to a corresponding character string of digits.
-
Write a program to read in two arrays from standard input
and use the first array (the "key") to decode an encrypted message stored
in the second array. The first array is a reordering of the alphabet and
space character ' ', for example,
char key[] = "mgrjuxpa lycdvkzhoftqniswbe";
The second array consists of integers between 0 and 26 inclusive whose
values correspond to letters in the key (indexed from 0 through 26).
Sample I/O.
Enter the key:
mgrjuxpa lycdvkzhoftqniswbe
Enter the message one number at a time.
Enter a negative number when done.
18 2 17 1 -5
The decoded message is: frog
Remark. This exercise is good practice, but it isn't a safe
way to do encryption.
-
Modify your solution to Programming Project 7
to store each user's account information in a separate file, instead
of storing all users' account info together in the same file.
-
Consider the
timeToSTLsort example.
This example shows that, when the selection sort algorithm
is used to sort a list of numbers,
the run time required grows in proportion to the square
of the length of the list; whereas when the STL sort function
is used, the average run time required grows in direct proportion
to the length of the list.
Download the source, read it, compile, and run it. Verify the results.
Use 10000 as the minimum array length, 100000 as the maximum array
length, and 10000 as the array length increment. (If your machine
doesn't have enough speed or memory to handle these values, then
divide them all by 10).
Use N = 5 trials per array size.
Gather the results and plot them in the x-y plane.
-
Explain precisely how and why the gcd(int, int) function below
correctly computes the greatest common divisor of two integers.
void swap(int& a, int& b)
{ int temp = a; a = b; b = temp; }
int gcd(int a, int b){
assert (a || b);
if ( a < 0 ) a = -a;
if ( b < 0 ) b = -b;
if ( a < b ) swap(a,b); // Force a >= b.
if ( b == 0 ) return a;
int r = a%b;
if (r == 0) return b;
else return gcd(b, r);
}