Please.... I need help with this.. Please provide comments and a successful scre
ID: 3777931 • Letter: P
Question
Please.... I need help with this.. Please provide comments and a successful screenshot as well.
C++
*******************************************************************************************
Write a program.
Create a string array in the main(), 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 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.
Explanation / Answer
Here is the below C++ Code for the given Scenario:
#include "stdafx.h"
#include <string>
#include <iostream>
int FindName(std::string* names, std::string name, int size)
{
for (int i = 0; i < size; i++)
{
if(names[i] == name)
return i;
}
return size;
}
void PrintNames(std::string* names, int size)
{
for (int i = 0; i < size; i++)
{
std::cout << names[i] << std::endl;
}
}
void DeleteName(std::string* names, std::string name, int size)
{
if(FindName(names, name, size) == size)
return;
for (int i = 0; i < size; i++)
{
if(names[i] == name)
names[i] = "";
}
}
int main()
{
std::string firstNameArray[7];
firstNameArray[0] = "Jim";
firstNameArray[1] = "Tuyet";
firstNameArray[2] = "Ann";
firstNameArray[3] = "Roberto";
firstNameArray[4] = "Crystal";
firstNameArray[5] = "Valla";
firstNameArray[6] = "Mathilda";
int index = FindName(firstNameArray, "Ann", 7);
if(index == 7)
std::cout << "Name not found" << std::endl;
else
std::cout << std::to_string(index) << std::endl;
PrintNames(firstNameArray, 7);
DeleteName(firstNameArray, "Ann", 7);
std::cout << " ";
PrintNames(firstNameArray, 7);
::getchar();
return 0;
}