N idea

Pages: 123
That's too hard to understand.
Sorry, algorithm, not @OP code.

I compiled & tested the debug version. Both two solution work correctly. :)


But the difference between mine and others :
My solution (from the algorithm) hmm... :

| 0 | 1 | 2 | 3 | 4 | 5 |

and we want to remove the 2, we swap it with the last element:
| 0 | 1 | 5 | 4 | 3 | 2 |


And the easy one :

| 0 | 1 | 2 | 3 | 4 | 5 |

and we want to remove the 2, we swap it with the last element:
| 0 | 1 | 3 | 4 | 5 | 5 |


Which is better? Here, debug program :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 10;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
	int num; // Additional variable (Debug)
};


//Other solution
int remove( infoType tele[], int& size, int position_of_item_to_remove )
{
    for( int i = position_of_item_to_remove + 1 ; i < size ; ++i ) 
	tele[i-1] = tele[i] ;
    return --size;
}


int main()
{
int choice;
infoType tele[SIZE];
for(int i =0;i < SIZE;i++){tele[i].num = i+1; cout << i << " ";}

cout << "\nPlease enter the number you want to delete : ";
cin >> choice;choice-=1; //Because 1 2 3 not 0 1 2

//The first solution
		int size = SIZE;
		int nCount = 0;
		infoType temp[SIZE];
		for( i = choice; i < size;i++){temp[nCount] = tele[i];nCount++;}
		for( i = choice; i < size;i++){nCount--;tele[i] = temp[nCount];}
		size--;


//remove(tele,size,choice);



for(int j =0;j < size;j++){cout << tele[j].num << " ";}cout << "\n";}
Last edited on
//Jackson Marie's solution


You would be well advised to ignore this troll's posts.
You would be well advised to ignore this troll's posts.

....................

I stand corrected. You said (I'm a troll?) I'm not a troll.
Any my mistake please correct.... :)
Topic archived. No new replies allowed.
Pages: 123