Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to practice using dynamically allocated arrays, arrays of po

ID: 3758956 • Letter: W

Question

Write a C++ program to practice using dynamically allocated arrays, arrays of
pointers and pointer traversal of an array. Main will contain the following
variables:
- An int variable for the array size (will be read in from a file)
- A pointer to (C++) string that will "point to" a dynamically allocated array of
string objects
- An array of pointers to strings that will be declared with the size read from
the file OR
- Optional: instead of an array of pointers, declare a pointer to pointer to
(C++) string that will "point to" a dynamically allocated array of POINTERS to
string objects


The program will do the following: (NOTE: THE FIRST 3 BULLETS SHOULD BE
DONE IN ONE FUNCTION, BUT NOT IN MAIN)
read from a text file, which has the how many strings (int) in the file at
the beginning of the file, then that number of string after it, one per line.
(USE THE openInputFile FUNCTION GIVEN BELOW)
dynamically allocate memory for a one-dimensional array of strings (c++
strings) using the size read from the file
read into the array of strings, one whole line per string (up to the size, still
make sure you don't go outside the array)
change each string to all lowercase USING A function (may do
this in the same loop you read, MUST convert one char at a time)
dynamically allocate OR declare an array of pointers to string with the
same size as the array of strings
assign to each element of the array of pointers to strings the address of
the corresponding element in the array of strings (the one you read into)
find how many strings are a palindrome and display them USING THE
ARRAY OF POINTERS TO STRINGS (with a label "is a palindrome") or if
none, display "not a palindrome". Note that a palindrome word is a word
that reads the same forwards as backwards. Assume there are only
letters in each string.
mixup the ARRAY OF POINTERS TO STRINGS using the algorithm
described in "Mixing up algorithm" below, and using the rand function
(also reviewed below)
display the strings using the array of strings (input order), BUT DISPLAY
AFTER THE POINTER ARRAY IS MIXED UP!

display the strings using the array of POINTERs to string (mixed up order)

deallocate all of the dynamically allocated memory!

Note: USE SEVERAL FUNCTIONS WITH PARAMETERS! DO NOT USE ANY EXTERNAL
VARIABLES (variables declared outside of main or outside of any function)!!
(const declarations OR #define's of numbers are OK.) Also, main should be
mostly function and "subroutine" calls, and no function should be "too long".

Include in program:
- External const declarations for each constant, except 0 or 1 (especially array
size)-- and USE them
- Dynamically allocated array(s)
- Error checking for invalid number of elements, array bounds
- input text file
- functions (with pointer return value and with array parameters)
- pointer traversal (just ONE, NOT array[i] notation and NOT *(array+i)) for
any array you want that's in the program
- Comments for program, variables and functions

Mixing up algorithm: For each element (i= 0 to the last element), get a random
index (0 to size-1, inclusive), and swap the ith element with the element at the
random index.

Using the rand function:
rand() returns an int between 0 and RAND_MAX (inclusive), where
RAND_MAX is #define'd in or )
#include and also
srand(...) is function for initializing the random function:
o Call ONLY ONCE at the beginning of a run (NOT every time
you call rand(), nor inside a function, nor inside a loop)
o srand(time(NULL));// this is how you call srand
(time(...) is in )
To get a number between MIN and MAX (inclusive):
((rand() % (MAX-MIN+1)) + MIN

OpenInputFile Function

//USE THE FOLLOWING IN YOUR MAIN FILE for opening the input file:

bool openInputFile(ifstream &ifs)

{

       string filename;

       cout << "Enter the input filename: ";

       getline(cin, filename);

       // IF USING A MAC, ADD CODE SHOWN BELOW***

       ifs.open(filename.c_str());

       return ifs.is_open();

}

***ADD THIS CODE IF USING A MAC because the Mac will include

       if( isspace(filename[filename.length()-1]) )

              filename.pop_back();

NOTE: you'll need to #include to use isspace

TestInputFile.txt:

23
action
cabbage
civic
Education
feather
locket
lumber
Madam
mountain
noon
partner
passenger
picture
plastic
pocket
POP
railway
reward
stranger
sweater
temper
visitor
weather

Test Run:

Enter input filename: TestInputFile.txt
civic is a palindrome
madam is a palindrome
noon is a palindrome
pop is a palindrome
The strings in input order:
action
cabbage
civic
education
feather
locket
lumber
madam
mountain
noon
partner
passenger
picture
plastic
pocket
pop
railway
reward
stranger
sweater
temper
visitor
weather
The strings in mixed up order:
mountain
temper
pocket
visitor
lumber
partner
passenger
feather
action
plastic
sweater
weather
cabbage
pop
noon
reward
locket
stranger
railway
education
madam
picture
civic

Press any key to continue . . .

Explanation / Answer

Dynamic memory allocation :

using pointers:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote