Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I've to write a code that will remove an element in the Partially filled array..

==> I've completed One code that will remove the element from where the order doesn't matter.
==> Now i just don't know what to do, ..!! to remove an element and keep the array ordered, i mean the removed element will be showed at the end.. Moreover I don't want an extra function to keep the array ordered . . .

Yes, I'm a student and I know the rules ..
Please give a suggestion as atleast i give a try ..
. . . . P L E A S E . . . .
C++
#include "stdafx.h"
#include <iostream>

using namespace std;

//FOR INSTANCE: Partially Filled ARRAY. !!
//PROBLEM: Remove an element at the index (User In)
void fill(int a[], int length)
{
	for (int i = 0; i < 8; i++)
	{
		cout << "Element #" << i+1 << endl;
		cin >> a[i];
	}
	cout << '\n' << endl; 
}

void print(int a[], int length)
{
	cout << "A R R A Y Of 10 Elements Are As." << endl;
	for (int j = 0; j < length; j++)
	{
		cout << a[j] << " , ";
	}
	cout << '\n' << endl;
}

void remove_unsorted(int a[], int length)
{
	
	cout << "Which Element You Want To Remove: "; 
	int index;
	cin >> index;
	index = index-1;	
	a[index] = a[length - 1];
}
void remove_sorted(int a[], int length) //Order OF The ARRAY Should Not Be Disturbed. . !!
{
	cout << "Which Element You Want To Remove: "; 
	int index;
	cin >> index;
	index = index-1;
	for (int i = index; i < length; i++)
	{ 
		? ? ?
	}
	
}

int main()
{
	const int length = 10;
	int test[length];
	fill(test, length);
	print(test, length);
	remove_unsorted(test, length);
	print(test, length);
	//remove_sorted(test, length);
	//print(test, length);
	system("pause");
	return 0;
}
Posted
Comments
Code-o-mat 9-Feb-13 8:03am    
Just asking: if you remove an item, shouldn't you be ending up with one less item rather than it getting replaced with the last item? This way, if you keep removing items you'll get an array full of the last value AND you can't remove the very last item no matter what. Did your assignment or task or whatever explicitly state this is what you need to do?
Usman Hunjra 9-Feb-13 8:21am    
Actually this is my lab test, The instructor gives us 4 questions i solve 3, And The fourth One is this .. Remove an element from the partially filled array in such a way that the order of the array should not be disturbed .. ?!

1 solution

Since this is homework, I'll give you no code.

But, removing an item is pretty easy. The only complication is if you want it ordered afterwards. If you have an unordered array of items, then it's very easy to remove an item:
1) Take the value of the last element, and use it to overwrite the value of the element to be removed.
2) Reduce the items count by one.

If you have an ordered array of items, then it is a little more complex, because you need a loop:
1) Set index to index of item to remove,
2) Loop while index < count of items minus one
2.1) Copy array[index + 1] into array[index]
2.2) Increment index by one
2.3) Repeat from (2)
3) Reduce the items count by one.


"hmm Got it .. I will try ..
by the way OriginalGriff are you a student .. ?!
Because the solution you posted is very much similar as in my book but i can't understand from it ..."


It's pretty simple.

If we have a sorted array of five integers:
1 3 5 7 9

And we want to remove the second number (3) we will end up with a sorted array for four integers:
1 5 7 9

So, you start with the index pointing at the element you want to remove:
      1 3 5 7 9
index---^
Then you copy the index + 1 element into it:
      1 5 5 7 9
index---^

Then you move the index:
      1 5 5 7 9
index-----^

And do it again:
      1 5 7 7 9
index-----^

      1 5 7 7 9
index-------^

You keep doing this until you run out of elements:
      1 5 7 9 9
index---------^

So you reduce the elements count by one and you have the result:
1 5 7 9
Simple if you draw a picture, isn't it?
 
Share this answer
 
v2
Comments
Usman Hunjra 9-Feb-13 10:24am    
hmm Got it .. I will try ..
by the way OriginalGriff are you a student .. ?!
Because the solution you posted is very much similar as in my book but i can't understand from it ...
OriginalGriff 9-Feb-13 10:44am    
Not since the 1980's! :laugh:
Answer updated
Usman Hunjra 9-Feb-13 11:32am    
WOW.. Now The Picture Is Clear ..
Respected OriginalGriff
If You Don't Mind, Can I Add You As A Friend In Fb, Twitter etc..
Because I Know I Can Learn Alot From You ..
P L E A S E..
Thank You For Your Consideration S I R.
OriginalGriff 9-Feb-13 11:36am    
Sorry - I don't tweet, and my FB account is just there as a place holder (to stop anyone else setting it up)
Usman Hunjra 9-Feb-13 11:39am    
At least An E-Mail ID SIR . . :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900