Removing duplicate indices within a vector, then printing the new vect

Write the following function, including a main() function, in index.cp

Page 1 of 1

1 Replies - 1114 Views - Last Post: 11 June 2008 - 12:35 PM Rate Topic: -----

#1 ProgrammingisMyEnemy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-June 08

Removing duplicate indices within a vector, then printing the new vect

Posted 11 June 2008 - 09:29 AM

Hi, i was wondering if I could get some help with this problem.
Ive been at it for quite a while...possibly because my understand of C++ is not very good or maybe i just suck at it.
Well here it goes,

Problem:

Write the following function, including a main() function, in index.cpp:
vector<int> noDupeIndex(vector<int> indexlist)
which function returns the integers in indexlist, with duplicate indices removed.
For example, if the vector indexlist is: 1 4 9 16 9 7 4 9 11, then the
function noDupeIndex would return the vector: 1 4 9 16 7 11.

Solution:
This is what i have so far...

#include <iostream>
#include <vector>

using namespace std;


vector<int> noDupeIndex(vector<int> indexlist)
{	
vector<int> b(indexlist.size());				   //Defines the size of the new vector 
	int i = 0;									 //Variable used to count the entries in vector indexlist								 
	int j = 0;									 //Variable used to count the entries in vector B

	//While i is less than the total number of entries in vector indexlist the following loop will assign entry n from vector indexlist to entry n of
	//vector B. Each time it does this it addes 1 to the entry counters for the respective vectors indexlist and B.
	for (int i = 0; i < indexlist.size(); i++)
	{
		b[j] = indexlist[i];
		j++;
	}

return b;
}


int main()
{
   //This variable is a counting variable used later to manage a loop.
	int x;

	//Defining vector size and entries for indexlist
	vector<int>  indexlist(9);
	indexlist[0] = 1;
	indexlist[1] = 4;
	indexlist[2] = 9;
	indexlist[3] = 16;
	indexlist[4] = 9;
	indexlist[5] = 7;
	indexlist[6] = 4;
	indexlist[7] = 9;
	indexlist[8] = 11;	

	vector<int> b = noDupeIndex(indexlist);

	cout << "Result of no duplicate index is:  ";

 //  While the counting variable is less than the total length of vector B, the following loop displays that corresponding entry. 
	//Everytime the loop is run, a integer of 1 is added to the counter. Thus each proceeding time, a new corresponding entry is displayed 
	//until there are no more entries left.
   for (x = 0; x < b.size(); x++)
	   
	  cout << b[x] << " ";

   cout << "\n";



   return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Removing duplicate indices within a vector, then printing the new vect

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Removing duplicate indices within a vector, then printing the new vect

Posted 11 June 2008 - 12:35 PM

A hint:

	//While i is less than the total number of entries in vector indexlist the following loop will assign entry n from vector indexlist to entry n of
	//vector B. Each time it does this it addes 1 to the entry counters for the respective vectors indexlist and B.
	for (int i = 0; i < indexlist.size(); i++)
	{
		bool exists = false;

		// write code here to check if item indexlist[i] already exists in vector b[ ]
		// if so, make variable exists true.
		// you can use a for loop for this

		if (exists == false)
		{
			b[j] = indexlist[i];
			j++;
		}
	}


This post has been edited by joske: 11 June 2008 - 03:18 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1