I have to redo programming exercise 5 of chapter 9 using dynamic arrays. but I have the information but I don't know how to use it in a dynamic arrays please help.
//Chapter 9: Programming Exercise 5
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char str[81];
int len;
int i;
cout << "Enter a string: ";
cin.get(str, 80);
cout << endl;
cout << "String in upper case letters is:" << endl;
len = strlen(str);
for (i = 0; i < len; i++)
cout << static_cast<char>(toupper(str[i]));
cout << endl;
return 0;
}
To redo the programming exercise using dynamic arrays, you will need to make the following changes to the code:
1. Instead of declaring a fixed-size character array `char str[81]`, you need to declare a pointer to a character array.
```cpp
char* str;
```
2. Use the `new` keyword to allocate memory for the dynamic character array. Allocate enough memory to store the maximum number of characters (80) plus one additional character for the null terminator.
```cpp
str = new char[81];
```
3. Replace the `cin.get(str, 80)` line with `cin.getline(str, 81)` for reading input from the user. This will ensure that the input is safely truncated if it exceeds the maximum length.
```cpp
cin.getline(str, 81);
```
4. To calculate the length of the dynamic character array, you can use the `strlen` function as before.
```cpp
len = strlen(str);
```
5. Instead of declaring a fixed-size integer variable `int i`, you can modify it to use `size_t` to represent the index of the characters in the dynamic array.
```cpp
size_t i;
```
6. To make the letter conversions, you can iterate over the dynamic array using a loop and convert each character to uppercase using the `toupper` function.
```cpp
for (i = 0; i < len; i++)
str[i] = static_cast<char>(toupper(str[i]));
```
Note: Since the dynamic character array is modified directly, there's no need to use `cout` to display the uppercase string separately.
7. Lastly, don't forget to deallocate the memory allocated for the dynamic array using the `delete[]` operator at the end of the program to prevent memory leaks.
```cpp
delete[] str;
```
That's it! With these changes, you have successfully rewritten the program to use dynamic arrays.
To refactor the given program to use dynamic arrays instead of fixed-size arrays, you need to follow these steps:
Step 1: Replace the declaration of the char array `str` with a pointer to a char array.
```
char* str;
```
Step 2: Allocate memory for `str` dynamically using the `new` operator.
```
str = new char[81];
```
Step 3: Modify the input statement to use the `cin.getline()` function instead of `cin.get()` to read a line of input and store it in `str`.
```
cin.getline(str, 81);
```
Step 4: Replace the `strlen()` function with a loop that counts the number of characters in `str` until a null character is encountered.
```
len = 0;
while (str[len] != '\0')
{
len++;
}
```
Step 5: Replace the `toupper()` function with the `std::toupper()` function from the `<cctype>` header.
```
#include <cctype>
```
Step 6: Change the loop to iterate over the dynamic array `str` using the `len` variable as the loop condition.
```
for (i = 0; i < len; i++)
{
cout << static_cast<char>(toupper(str[i]));
}
```
Step 7: Remember to free the dynamically allocated memory for `str` using the `delete[]` operator at the end of the program to prevent memory leaks.
```
delete[] str;
```
Here's the modified code using dynamic arrays:
```cpp
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char* str;
int len;
int i;
cout << "Enter a string: ";
str = new char[81];
cin.getline(str, 81);
cout << "String in upper case letters is:" << endl;
len = 0;
while (str[len] != '\0')
{
len++;
}
for (i = 0; i < len; i++)
{
cout << static_cast<char>(toupper(str[i]));
}
delete[] str;
return 0;
}
```
By following these steps, you can refactor the given program to use dynamic arrays.