In C++, write two functions named printArray and deleteRepeats along with a main to test them according to the following specifications. Be sure to use function prototypes and place the actual function definitions after main.
printArray will take two parameters: an array of characters and an integer representing the number characters stored in the array. It will print the characters from the array, labeled by their index, to the screen up to the specified size.
deleteRepeats will take the same two parameters, except that the integer must be passed by reference. The function must remove any repeated characters from the array and reduce the size to match the reduced number of characters. The contents of the array beyond the newly reduced size do not matter.
main must test deleteRepeats with 4 different sets of data. For each test, you will print the array, call deleteRepeats, and then print the array again. One data set must contain only unique characters. One data set must contain one character repeated several times. Two will be different mixtures of characters, including some repetition. Make sure at least of your arrays is completely full to start with and that at least one of them is not completely full to start with.
There should be no user interaction in this program.
#include <iostream>
#include <cstdlib> // Must be included to use the rand function, in this program
using namespace std;
void generateRandomCharacters(char *s, int arraySize); // Generate random characters prototype
void printCharacterFrequency(char *s, int arraySize); // Print character frequency prototype
void deleteRepeats(char *s, int &arraySize); // Delete repeats prototype
void printArray(char *s, int arraySize); // Print array prototype
int main()
{
char cArray[1000];
int arrayLength = 1000; // Limiting array length to 1000
srand(100); // This sets a specific sequence of random numbers
generateRandomCharacters(cArray, arrayLength); // Generates random freqyency
cout << "\nThe Random Array:\n";
printArray(cArray, arrayLength); // Prints 1000 "random" characters
printCharacterFrequency(cArray, arrayLength); // Pritns Char and Frequency table
deleteRepeats(cArray, arrayLength); // Deletes all the repeated characters
cout << "\nThe Compressed Array:\n";
printArray(cArray, arrayLength); // Prints new list of "random" characters, without repeats
printCharacterFrequency(cArray, arrayLength); // Prints the new Char and Frequency table after delete
return 0;
}
/*
Generate Random Characters
This function takes in an array parameter to an array of characters and an integer corresponding to
the size of the array. The function then populates the array with randomly generated characters.
Using the rand function, it generate random numbers from 97 to 122, then converts them to characters
*/
void generateRandomCharacters(char *s, int arraySize)
{
int num;
for (int i = 0; i < arraySize; i++)
{
num = 97 + rand() % 26;
s[i] = static_cast<char>(num);
}
}
/*
Print Character Frequency
This function takes in an array parameter to an array of characters and an integer corresponding to
the size of the array. This function then counts the occurrences (frequency) of each character in the array.
*/
void printCharacterFrequency(char *s, int arraySize)
{
int freq[26];
int i = 0;
int iCount = 0;
char findChar;
int cumFreq = 0;
for(iCount = 0; iCount < 26; iCount++) // Initialize the array "freq"
freq[iCount] = 0;
for(iCount = 0; iCount < 26; iCount++)
{
findChar = static_cast<char>(97 + iCount);
for(i = 0; i < arraySize; i++)
{
if(findChar == s[i])
freq[iCount]++;
}
}
cout << "\nFrequency Table:\nChar \tFrequency\n"; // Frequency Table output
for(iCount = 0; iCount < 26; iCount++)
{
cout << static_cast<char>(97 + iCount) << "\t" << freq[iCount] << "\t";
cumFreq = cumFreq + freq[iCount];
cout << cumFreq << "\n";
}
}
/*
Delete Repeats
This function takes two parameters: a char array parameter pointing to the array of characters
and an int reference parameter indicating the number of elements used in the array.
*/
void deleteRepeats(char *s, int &arraySize)
{
int startPos = 0;
int nDeleted = 0;
while(startPos < arraySize)
{
nDeleted = 0;
for(int i = startPos + 1; i < arraySize; i++)
{
if(s[i] == s[startPos])
nDeleted++;
else
s[i - nDeleted] = s[i];
}
arraySize -= nDeleted;
startPos++;
}
}
/*
Print Array
Prints the characters generated by the generateRandomCharacter function.
*/
void printArray(char *s, int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
cout << s[i];
}
cout << "\n";
}
In C++, write two functions named printArray and deleteRepeats along with a main to test them...
In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...
In c++
1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...
Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...
1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...
in C++ and also each function has its own main function so
please do the main function as well. Previously when i asked the
question the person only did the function and didn't do the main
function so please help me do it.
1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...
C++ Program Int Main First Please Write one program that does the following: 1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average. a. Declare an integer array with the name of “grades” of size 10 in the main function. b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array. i. The function will receive...
Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...
PLEASE HELP!!! C PROGRAMMING CODE!!!
Please solve these functions using the prototypes provided. At
the end please include a main function that tests the functions
that will go into a separate driver file.
Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...
In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...
1. All functions should be written AFTER the main procedure. 2. A function prototype should be written for each function and placed BEFORE the main procedure. 3. Each function should have a comment explaining what it does. 4. Each function parameter should have a comment explaining the parameter. 5. Prompt for the number of elements of the list. 6. Allocate an array whose size equals the number of elements specified by the user. 7. Read into the array the elements...