Click here to Skip to main content
13,054,779 members (70,754 online)
Rate this:
Please Sign up or sign in to 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 . . . .
#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);
	return 0;
Posted 9-Feb-13 1:18am
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Then you copy the index + 1 element into it:
      1 5 5 7 9

Then you move the index:
      1 5 5 7 9

And do it again:
      1 5 7 7 9

      1 5 7 7 9

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

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?
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 . . :/
OriginalGriff 9-Feb-13 11:49am
*Definitely* not! I already get between 200~300 emails every day that I have to at least read, I don't need any more. ;)
I am often here, and most of the other people here are just as willing to help a beginner. (Just don't use txtspk or ask for codez)
Usman Hunjra 9-Feb-13 11:52am
I PROMISE .. I will not ask you for codes .!!
Please SIR, Please
Usman Hunjra 9-Feb-13 11:53am
I want discussion not codes and also I will not send emails on daily basis..
I BEG .. P L E A S E
OriginalGriff 9-Feb-13 12:43pm
Never, ever, ever post your email to a public website, unless you really like spam! I have deleted your comment which included it...

I'm sorry, but there is only one of me, and (meaning no offence) hundreds of you! :laugh:
If I do this for you, what do I do the next time someone else wants the same thing? Tell him I can't because I already deal with you? Or tell you I can't any more as I'm dealing with him? I can't deal with all of you, I have to earn a living and have a life as well...
Just ask here; if I'm not here, there are plenty of others who know at least as much as me! :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100