Removing element from array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Removing element from array

 
0
  #1
Sep 2nd, 2009
Ok, so I am working on a function that remove a element from the array if the name entered by a user is = to a element in the array

It clears of the memory of said element but i need to re order the array

I searched on here but nothing seemed to cover it for an array of structs

  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title.clear();
  6. songs[i].artist.clear();
  7. songs[i].genre.clear();
  8. songs[i].time = 0;
  9.  
  10. songs[i-1].title = songs[i].title;
  11. songs[i-1].artist = songs[i].artist;
  12. songs[i-1].genre = songs[i].genre;
  13. songs[i-1].time = songs[i].time;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Removing element from array

 
0
  #2
Sep 2nd, 2009
Originally Posted by power_computer View Post
Ok, so I am working on a function that remove a element from the array if the name entered by a user is = to a element in the array

It clears of the memory of said element but i need to re order the array

I searched on here but nothing seemed to cover it for an array of structs

  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title.clear();
  6. songs[i].artist.clear();
  7. songs[i].genre.clear();
  8. songs[i].time = 0;
  9.  
  10. songs[i-1].title = songs[i].title;
  11. songs[i-1].artist = songs[i].artist;
  12. songs[i-1].genre = songs[i].genre;
  13. songs[i-1].time = songs[i].time;
  14. }

Close, but not quite. You just overwrote songs[i-1], which presumably may NOT be a duplicate. I assume you want to "delete" songs[i], by overwriting it with songs[i+1], like this:

  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title = songs[i+1].title;
  6. songs[i].artist = songs[i+1].artist;
  7. songs[i].genre = songs[i+1].genre;
  8. songs[i].time = songs[i+1].time;
  9. }
  10. }

This isn't quite the solution. There are still problems with it and thus you need to do more, but you're no longer overwriting a song that wasn't a duplicate, so it's a start.

Note that the word "delete" is in quotes on purpose. Depending on how technical one wants to be, you're not really "deleting" or "removing" anything from the array. The array stays the same size. But you're changing the array in an organized way such that you have one fewer element that you care about (i.e. a value may still be there, but you couldn't care less what it is), so in effect you're deleting it.

Not sure if the above paragraph adds or detracts from the discussion. If it detracts, just ignore it for now.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Re: Removing element from array

 
0
  #3
Sep 2nd, 2009
Well in this particular function, as seen in my previous post, you can recall the code

I call this function to ask the user to input a song title, then I sort through the array to see if there is a corresponding element in the array and remove that item from the array, then I "shorten" the array so It can print out the array properly when I call my print function, hence why I used

  1. songs[i].title.clear();
  2. songs[i].artist.clear();
  3. songs[i].genre.clear();
  4. songs[i].time = 0;

I thought it would clear out the memory, which I did, but I failed to shift the array.

However, you code make much more sense to simply shift the index of the array over 1 so that value gets overwrited if the user inputs a title that corresponds to an element in the array

Just as an FYI the user input is...asking the user to input the title of a song only.

As you said its it not the final solution because

(1,2,3,4) example of output

now after said "removal"

(1,2,4,4)

I just need to learn how to remove that duplicated element which seems to be the following element after the match
in titles

And I think I fixed it, I am bit abrupt on explaining my meaning but I did this to the "remove" function

  1. int remove(music songs[10], int lCount)
  2. {
  3. string name;
  4. int i =0;
  5.  
  6. cout<<"Enter title: ";
  7. cin.ignore();
  8. getline(cin,name);
  9.  
  10. for(int i = 0; i < lCount; i++)
  11. {
  12. if(name == songs[i].title)
  13. {
  14.  
  15. songs[i].title = songs[i+1].title;
  16. songs[i].artist = songs[i+1].artist;
  17. songs[i].genre = songs[i+1].genre;
  18. songs[i].time = songs[i+1].time;
  19. }
  20. }
  21.  
  22. return (lCount - 1);
  23. }

and when I call this function I simple did

lCount = remove(songs, lCount);

So the iterator of "lCount"; which the current arrays size depending on the last file read gets reduced by 1

Not gonna lie it is a buggy in some instances, the remove and then print correct amount of array works some times the value does repeat
Last edited by power_computer; Sep 2nd, 2009 at 1:45 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,689
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 481
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Removing element from array

 
0
  #4
Sep 2nd, 2009
struct can be easily copied using an assingment operator.
  1. songs[0]=songs[1];
You can't remove an element from the an array. However you may assign an empty struct,
  1. music empty={"","","",0};
  2. songs[i]=empty;
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Removing element from array

 
1
  #5
Sep 2nd, 2009
You're on the right track, but not really moving all the songs following the deleted item up. You only move the i+1 element to the i postion when the match was found. Everything past that will remain where it was.
That is, if the list is 1 2 3 4 5 6 and you want to remove 3, you'll get 1 2 4 4 5 6, but with the reduced count, item 6 won't be seen.

Better method is to iterate through the array till you find the item to remove, or run out of array (it's not found). When you find it, begin a loop that moves every remaining item up one position, starting from the location item was found to the end of array.

Oh - watch for reading past end of array. If your loop counter i points to the last element, what's at i+1?

Or, using the code you've written, use the comparison to set a flag if match is found, and use that flag to indicate if an item should be moved up.
  1. bool move = false;
  2. for(int i = 0; i < lCount; i++)
  3. {
  4. if(name == songs[i].title)
  5. {
  6. move = true; //never gets reset to false
  7. continue; //we'll update on the next loop pass
  8. }
  9.  
  10. if( move )
  11. {
  12.  
  13. songs[i-1].title = songs[i].title;
  14. songs[i-1].artist = songs[i].artist;
  15. songs[i-1].genre = songs[i].genre;
  16. songs[i-1].time = songs[i].time;
  17. }
  18. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Re: Removing element from array

 
0
  #6
Sep 2nd, 2009
Any ideas to solve my last buggy issue where in some instances when I "remove" the first element in the array, the second element is copied into the cell block [0],[1]
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Re: Removing element from array

 
0
  #7
Sep 2nd, 2009
Your method works when i+1, i-1 doesnt seem work as well, however, as stated i+1 is still a bit buggy in some circumstances, it seems the boolean is checking well, because when enter anyname the next value in the array is removed and not displayed, hmmm
Last edited by power_computer; Sep 2nd, 2009 at 2:08 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Removing element from array

 
0
  #8
Sep 2nd, 2009
Originally Posted by power_computer View Post
Your method works when i+1, i-1 doesnt seem work as well, however, as stated i+1 is still a bit buggy in some circumstances
Look at it closely, be sure you have the continue statement as shown.

Also, your decrementing of the count that's returned should only occur when the flag "move" is true. Don't modify it if the target song was not found.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Re: Removing element from array

 
0
  #9
Sep 2nd, 2009
It is still buggy, not removing properly when I imply your method

Here is my entire code

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct music
  8. {
  9. string title;
  10. string artist;
  11. string genre;
  12. int time ;
  13. };
  14.  
  15. int read(char* file, music songs[10] , int lCount = 0);
  16. void print(music songs[10], int lCount);
  17. int remove(music songs[10], int lCount);
  18. int main()
  19. {
  20. music songs[10];
  21. int j = 0, lCount = 0, choice = 0;
  22. char file[256];
  23.  
  24. lCount = read("song.dat",songs);
  25.  
  26. do
  27. {
  28. cout<<endl;
  29. cout<<"Enter choice: ";
  30. cin>>choice;
  31.  
  32. switch(choice)
  33. {
  34. case 1:
  35. cout<<"Enter new file: ";
  36. cin>>file;
  37. lCount = read(file, songs, lCount);
  38. break;
  39. case 2:
  40. print(songs, lCount);
  41. break;
  42. case 3:
  43. lCount = remove(songs, lCount);
  44. break;
  45. default:
  46. cout<<" ";
  47. break;
  48. }
  49. }while(choice != 100);
  50.  
  51.  
  52.  
  53. system("Pause");
  54. return 0;
  55. }
  56.  
  57. int read(char *file, music songs[10], int lCount)
  58. {
  59.  
  60.  
  61. ifstream fin;
  62. string string;
  63. int i = 0;
  64.  
  65. for(int j=0; j < lCount; j++)
  66. {
  67. songs[i].title.clear();
  68. songs[i].artist.clear();
  69. songs[i].genre.clear();
  70. songs[i].time = 0;
  71. }
  72.  
  73.  
  74. fin.open(file);
  75.  
  76. while(!fin.eof() && i < 10)
  77. {
  78. getline(fin,songs[i].title);
  79. getline(fin,songs[i].artist);
  80. getline(fin,songs[i].genre);
  81. getline(fin,string);
  82. songs[i].time = atoi(string.c_str());
  83. i++;
  84. }
  85.  
  86.  
  87. fin.close();
  88. return(i);
  89. }
  90.  
  91. void print(music songs[10], int lCount)
  92. {
  93.  
  94. for(int j = 0; j < lCount; j++)
  95. {
  96. cout<<songs[j].title
  97. <<songs[j].artist
  98. <<songs[j].genre
  99. <<songs[j].time<<endl;
  100.  
  101. }
  102. }
  103. int remove(music songs[10], int lCount)
  104. {
  105. string name;
  106. int i =0;
  107. bool move = false;
  108.  
  109. cout<<"Enter title: ";
  110. cin.ignore();
  111. getline(cin,name);
  112.  
  113. for(int i = 0; i < lCount; i++)
  114. {
  115. if(name == songs[i].title)
  116. {
  117. move = true;
  118. continue;
  119. }
  120. if(move)
  121. {
  122. songs[i].title = songs[i-1].title;
  123. songs[i].artist = songs[i-1].artist;
  124. songs[i].genre = songs[i-1].genre;
  125. songs[i].time = songs[i-1].time;
  126. }
  127. }
  128.  
  129.  
  130.  
  131. return (lCount);
  132. }

The read and print functions work fine as far I tested them with 3 different files, and also the read function as to read from a song.dat file upon start up just wanted to start that
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Removing element from array

 
0
  #10
Sep 2nd, 2009
You got the assignment backwards, should be
  1. songs[i-1].title = songs[i].title;
  2. //etc.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC