Write a program. Create a string array in the main0, called firstNameArray, init
ID: 3720711 • Letter: W
Question
Write a program. Create a string array in the main0, called firstNameArray, initialize with 7 first names Jim, 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, printAllNames, 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 printAlINames 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, 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.Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
int searchArray(string p, string *a, int n){
for (int i =0; i<n; i++){
if (a[i] == p ){
return i;
}
}
return 7;
}
void printAllNames(string *a, int n){
for (int i =0; i<n; i++){
if (a[i] != "")
cout << a[i] << endl;
}
}
void deleteName(string p, string *a, int n){
int found = 0;
for (int i = 0; i<n; i++){
if (a[i] == p){
a[i] = "";
found = 1;
break;
}
}
if (found == 0)
cout << "Name not found ";
}
int main(){
string firstNamesArray[] = {"Jim","Tuyet","Ann","Roberto","Crystal","Valla","Mathilda"};
int index = searchArray("Jim",firstNamesArray,7);
if (index == 7){
cout << "Name not found ";
}
else {
cout << firstNamesArray[index] << endl;
}
cout << "Given array------------- ";
printAllNames(firstNamesArray,7);
deleteName("Ann", firstNamesArray,7);
cout << "After deletion------------- ";
printAllNames(firstNamesArray,7);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.