In: Computer Science
Create and initialize a string array, write and use 3 functions.Write a program.
Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda
Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found, else it return the number 7.(This function needs 3 parameters –see code examples above)
Write the code in the main program to call/use the searchArray function. Check the return value for the index value returned. Print the name using the index number(0 to 6), or prints ‘name not found’ if it return a 7.
Write a second function, print AllNames, that will print all the names in the array.Pass the array into the function. (This function needs two parameters –see code example above)
Write the code in the main program to call/use this printAllNames function.
Write a third function, called deleteName, that will delete a name from the array. Check first to see if the name is in the array, before you try to delete it(use the searchArray function).If you find the name, then write “ “ to the location in the array. ... Just making spot blank.(This function requires 3 parameters –see code examples above).
Call the printAllNames function to print the array to verify you have deleted a value.Print out the array... if the spot/index in the array is blank do not print it.
C++
In case of any query do comment. Thanks
Note: Your question was stating about (This function needs 3 parameters –see code examples above) whihch you did not clarify, I assumed this must be length of the array, if you have any other parameter do let me know I will update answer.
Code:
#include <iostream>
using namespace std;
//function to search given name in firstNameArray, length is third parameter
int searchArray(string name, string firstNameArray[], int length){
for (int i = 0; i < length; i++) {
if(firstNameArray[i] == name){
return i;
}
}
return 7;
}
//print all values in array
void printAllNames(string firstNameArray[], int length){
for (int i = 0; i < length; i++) {
if(!firstNameArray[i].empty()) //if empty name then don't print it
cout << firstNameArray[i] <<endl;
}
}
//function to delete name from aray by making it ""
void deleteName(string name, string firstNameArray[], int length){
int index = searchArray(name,firstNameArray,length);
if(index !=length){
firstNameArray[index] ="";
}
}
int main()
{
//declare an array and initialized
string firstNameArray[] = {"Jim", "Tuyet", "Ann", "Roberto", "Crystal", "Valla", "Mathilda"};
int length=7;
int index =-1;
string name = "Jack"; //search for string jack which will return 7
index = searchArray(name,firstNameArray,length);
if(index == length){
cout << name << " not found" <<endl;
}
else
{
cout << firstNameArray[index]<<endl;
}
//display array names
printAllNames(firstNameArray,length);
//trying to delete Roberto
deleteName("Roberto",firstNameArray,length);
cout<< endl<< "After deleting Roberto" <<endl <<endl;
printAllNames(firstNameArray,length);
return 0;
}
==============Screen shot of the code for indentation=======
output: