Question

// This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<Enter <<size< students names:<<endl; for(int i-0; i<size;i++) cin>>students_names[i]; students names-add name(students nam es, size); display_names (students_names, size); delete[] students_names; system(pause) return ;

media%2F40e%2F40e9636d-a99b-4882-9350-77

media%2F291%2F29153789-8eb4-405c-8fa8-c9

using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
using namespace std;
void display_names(string* s_names, int size)
{

for(int i=0;i<size;i++)
cout << "Name " << i << ": " << s_names[i] << endl;
}
string* delete_name(string* s_names, int& size)
{
string name;


cout << "Enter the name of student to delete from the list: ";
cin >> name;//take the name to be deleted as input

int i=0;
for(i = 0;i<size;i++)
{
if(s_names[i] == name)//loop through the array to find the index of name in the array
break;
}
if(i==size)//if end of array reached, it means name not present in the list
{
cout << "Name not present in the list.\n";
return s_names;
}
string* temp = new string[size-1];//declare an array of size 1 less than the initial array
for(int j = 0; j<i; j++)
{
temp[j] = s_names[j];//copy the names till before of ith name.
}
for(int j = i+1; j<size; j++)
{
temp[j-1] = s_names[j];//copy the name after ith name.
}
delete[] s_names;//delete initial array
size--;//decrease size by 1
return temp;//return new array
}

int main()
{
int size = 3;
string* students_names = new string[size];
cout << "Enter " << size << " students names." <<endl;
for(int i=0;i<size;i++)
cin >> students_names[i];

students_names = delete_name(students_names, size);//deletes the name from array
display_names(students_names, size);//display names after deletion.
}

Refer to image below for better understanding:

÷include #include using namespace std; void display names (string* s names, int size) <iostream> <string> 3 4 for (int is0;i<

#include include <iostream> <string> I 3 using namespace std; 4 void display_names (string* s_names, int size) cout << Name

26 27 cout << Name not present in the list.\n return s names 29 30 31 32 string* temp = new string [size-11://declare an ar

Sample Output:

Enter 3 students names. John David Mike Enter the name of student to delete from the list: David Name e: John Name 1: Mike Pr

You can use the delete_name function written above in your program along with the other functions. I have provided the implementation and test case for the delete_name function. Please give this solution a thumbs up if you find it helpful and comment if you have any doubts in it.

Add a comment
Know the answer?
Add Answer to:
using the code above my instructor just want me to delete name from a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • What did I do wrong with this C++ code? Assume that we don't need to ask...

    What did I do wrong with this C++ code? Assume that we don't need to ask users for the number of students. #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size; int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total; // average grade and total grades //**************start your code here************************ cout<<"How many student grades...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how

    This is what I have as of right now. I am lost on what to do next#include <stdio.h>#include <iostream>using namespace std;int main(){    double scores, numOfStudents, average = 0, highscore, sum = 0;    string names;    cout << "Hello, please enter the number of students" << endl;    cin >> numOfStudents;    do {        cout << "Please enter the student's name:\n";        cin >> names;        cout << "Please enter the student's...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • What changes should you make to the following code if you delete the “using namespace std;”...

    What changes should you make to the following code if you delete the “using namespace std;” cout << "Who is there ? "; cin >> name; cout << "Hello " << name << "!" << endl

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT