Programming Projects require more problem-solving than Practice Programs and can usually be solved many different ways. Visit www.myprogramminglab.com to complete many of these Programming Projects online and get instant feedback.
Do Programming Project 7 in Chapter 7 using a dynamic array. In this version of the problem, use dynamic arrays to store the digits in each large integer. Allow an arbitrary number of digits instead of capping the number of digits at 20.
Do Programming Project 3 in Chapter 7. In this version of the problem, return a new dynamic array where all repeated letters are deleted instead of modifying the partially filled array. Don’t forget to free the memory allocated for these returned dynamic arrays when the data is no longer needed.
Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length.
You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below:
Lab Number | Computer Station Numbers |
---|---|
1 | 1–5 |
2 | 1–6 |
3 | 1–4 |
4 | 1–3 |
Each user has a unique five-digit ID number. Whenever a user logs on, the user’s ID, lab number, and the computer station number are transmitted to your system. For example, if user 49193 logs onto station 2 in lab 3, then your system receives (49193, 2, 3) as input data. Similarly, when a user logs off a station, then your system receives the lab number and computer station number.
Write a computer program that could be used to track, by lab, which user is logged onto which computer. For example, if user 49193 is logged into station 2 in lab 3 and user 99577 is logged into station 1 of lab 4, then your system might display the following:
Lab Number Computer Stations
1 1: empty 2: empty 3: empty 4: empty 5: empty
2 1: empty 2: empty 3: empty 4: empty 5: empty 6: empty
3 1: empty 2: 49193 3: empty 4: empty
4 1: 99577 2: empty 3: empty
Create a menu that allows the administrator to simulate the transmission of information by manually typing in the login or logoff data. Whenever someone logs in or out, the display should be updated. Also write a search option so that the administrator can type in a user ID and the system will output what lab and station number that user is logged into, or “None” if the user ID is not logged into any computer station.
You should use a fixed array of length 4 for the labs. Each array entry points to a dynamic array that stores the user login information for each respective computer station.
The structure is shown in the figure below. This structure is sometimes called a ragged array since the columns are of unequal length.
One problem with dynamic arrays is that once the array is created using the new operator, the size cannot be changed. For example, you might want to add or delete entries from the array as you can with a vector. This project asks you to create functions that use dynamic arrays to emulate the behavior of a vector.
First, write a program that creates a dynamic array of five strings. Store five names of your choice into the dynamic array. Next, complete the following two functions:
string* addEntry(string *dynamicArray, int &size, string
newEntry);
This function should create a new dynamic array one element larger than
dynamicArray
, copy all elements fromdynamicArray
into the new array, add the new entry onto the end of the new array, increment size, deletedynamicArray
, and return the new dynamic array.
string* deleteEntry(string *dynamicArray, int &size, string
entryToDelete);
This function should search dynamicArray
for entryToDelete
. If not found, the request should be ignored and the unmodified dynamicArray
returned. If found, create a new dynamic array one element smaller than dynamicArray
. Copy all elements except entryToDelete
into the new array, delete dynamicArray
, decrement size, and return the new dynamic array.
Test your functions by adding and deleting several names to the array while outputting the contents of the array. You will have to assign the array returned by addEntry
or deleteEntry
back to the dynamic array variable in your main
function.
What if C++ had no built-in facility for two-dimensional arrays? It is possible to emulate them yourself with wrapper functions around a one-dimensional array. The basic idea is shown below. Consider the following two-dimensional array:
int matrix[2][3];
It can be visualized as a table:
matrix[0][0] |
matrix[0][1] |
matrix[0][2] |
matrix[1][0] |
matrix[1][1] |
matrix[1][2] |
The two-dimensional array can be mapped to storage in a one-dimensional array where each row is stored in consecutive memory locations (your compiler actually does something very similar to map two-dimensional arrays to memory).
int matrix1D[6];
matrix[0][0] |
matrix1D[0][1] |
matrix1D[0][2] |
matrix1D[1][0] |
matrix1D[1][1] |
matrix1D[1][2] |
Here, the mapping is as follows:
matrix[0][0] would be stored in matrix1D[0]
matrix[0][1] would be stored in matrix1D[1]
matrix[0][2] would be stored in matrix1D[2]
matrix[1][0] would be stored in matrix1D[3]
matrix[1][1] would be stored in matrix1D[4]
matrix[1][2] would be stored in matrix1D[5]
Based on this idea, complete the definitions for the following functions:
int* create2DArray(int rows, int columns);
This creates a one-dimensional dynamic array to emulate a two-dimensional array and returns a pointer to the one-dimensional dynamic array.
rows
is the number of rows desired in the two-dimensional array.
columns
is the number of columns desired in the two-dimensional array.
Return value: a pointer to a one-dimensional dynamic array large enough to hold a two-dimensional array of size rows * columns
.
Note that int ptr = create2DArray(2,3);
would create an array analogous to that created by int ptr[2][3];
void set(int *arr, int rows, int columns,
int desired_row, int desired_column, int val);
This stores val
into the emulated two-dimensional array at position desired_row
, desired_column
. The function should print an error message and exit if the desired indices are invalid.
arr
is the one-dimensional array used to emulate a two-dimensional array.
rows
is the total number of rows in the two-dimensional array.
columns
is the total number of columns in the two-dimensional array.
desired_row
is the zero-based index of the row the caller would like to access.
desired_column
is the zero-based index of the column the caller would like to access.
val
is the value to store at desired_row
and desired_column
.
int get(int *arr, int rows, int columns,
int desired_row, int desired_column);
This returns the value in the emulated two-dimensional array at position desired_row
, desired_column
. The function should print an error message and exit if the desired indices are invalid.
arr
is the one-dimensional array used to emulate a two-dimensional array.
rows
is the total number of rows in the two-dimensional array.
columns
is the total number of columns in the two-dimensional array.
desired_row
is the zero-based index of the row the caller would like to access.
desired_column
is the zero-based index of the column the caller would like to access.
Create a suitable test program that invokes all three functions.
Write a program that outputs a histogram of student grades for an assignment. The program should input each student’s grade as an integer and store the grade in a vector (covered in Chapter 8). Grades should be entered until the user enters −1 for a grade. The program should then scan through the vector and compute the histogram. In computing the histogram, the minimum value of a grade is 0 but your program should determine the maximum value entered by the user. Use a dynamic array to store the histogram. Output the histogram to the console. For example, if the input is:
20
30
4
20
30
30
−1
Then the output should be:
Number of 4's: 1
Number of 20's: 2
Number of 30's: 3