Hi,

I have a problem with the use of vectors.
As you can see, the program asks the user for a row of positive whole numbers. These numbers will by put in a vector called "getallen".
Than the numbers in "getallen" will be sorted by the function "sorteerint" and number of occurence is counted and printed on the screen.

By testing some few things, I discovered that the numbers of vector "getallen" aren't copied to vector "v1"..

Could someone give me a hint about what I should change or add ?

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;


void drukintro ();
void geef_rij( vector<int> getallen );
void sorteerint( vector<int> getallen );
void voorkomentellen ( vector<int> getallen , vector<int> getelde_rij, vector<int> voorkomen );
void printintrij( vector<int> getelde_rij, vector<int> voorkomen );

void drukintro ()
{
	int i;
	cout << "Dit is een aangepaste versie van Lab9Opgave5 waarbij" << endl
		 << "de arrays vervangen zijn door vectors" << endl << endl
		 << "Dit is een programma dat een rij van getallen sorteert" << endl
		 << "en het voorkomen van elk getal telt," << endl 
		 << "en daarna van hoog naar laag op het scherm drukt." << endl;
	for ( i=0; i<=54; i++)
	{
		cout << "-";
	}
	cout << endl << endl;

}

void geef_rij( vector<int> getallen )
{
	int getal;

	cout << "Enter a row of positive whole numbers followed by a negatieve whole number"<< endl;
	
	cin >> getal;
	
	while (getal > 0)
	{
		getallen.push_back(getal);
		cin >> getal;
	}
}

void sorteerint( vector<int> getallen )
{	
	unsigned int i, j, temp;

	for (i=0; i<getallen.size( ); i++)
		for (j=getallen.size( )-1; j>i; j--)
		{
			cout << getallen[j] << endl;
			if (getallen[j-1]>getallen[j]) 
			{	
				temp=getallen[j];
				getallen[j]=getallen[j-1];
				getallen[j-1]=temp;
				cout << getallen[i];
				cout << temp;
			}
		}
}

void voorkomentellen ( vector<int> getallen , vector<int> getelde_rij, vector<int> voorkomen )
{
	unsigned int i, j=0, getal1, getal2, count=1;
	for (i=0; i<getallen.size( ); i++)
	{
		getal1 = getallen[i];
		cout << getal1;
		getal2 = getallen[i+1];
		cout << getal2;

		if (getal1 == getal2)
		{
			count++;
		}
		else
		{
			getelde_rij[j] = getal1;
			voorkomen[j] = count;
			count=1;
			j++;
		}
	}
}
	

void printintrij( vector<int> getelde_rij, vector<int> voorkomen )
{
	unsigned int i;
	cout.setf(ios::left);
	cout << "\nN    Voorkomen\n";
	for (i=0; i<getelde_rij.size( ); i++)
		cout << setw(5) << getelde_rij[i] << voorkomen[i] << endl;
	cout << endl;
}
	

int main ()
{
	vector<int> v1, v2, v3;

	drukintro ();
	
	geef_rij ( v1 );
	cout << endl;

	
	sorteerint ( v1);
	voorkomentellen ( v1, v2 , v3);
	printintrij( v2 , v3);
}

Not sure how far along you are in your C++ studies, but this terminology suggests that you are confused about what happens when you pass a parameter to a function.

By testing some few things, I discovered that the numbers of vector "getallen" aren't copied to vector "v1"..

Note the bold red emphasis above. Parameters are either passed by value or by reference. In your case, you passed by value, which I imagine you didn't intend to. When you do that, a new vector called getallen is created. It is separate from the original vector called v1 which you passed from main. Any "copying" that is done will be done BEFORE you ask the user to input numbers into getallen. The copying will be the contents of v1 into getallen . There will never be any copying done from getallen into v1 . getallen simply goes out of scope when the function ends. Its contents are lost and have no effect on v1 .

You need to put in debugging statements that either display the contents of the vector(s) and/or the addresses of the vector(s) at different stages to get a feel of what is happening. My guess is that you intend to pass by reference, not by value. Is there a need to create two vectors and copy back and forth? If not, just use one vector. Regardless, if you want to use the altered getallen vector after the function ends, you must either return it from the function or pass it by reference so it does not go out of scope. I suggest passing it by reference.

you need to pass your vector by reference

Chris

You need to pass references to the vectors you are expecting to be updated.

Otherwise, it's just

void foo ( int a ) {
  a = 3;
}
int main ( ) {
  int b = 0;
  foo( b );
  // b still 0 here, not 3
}

Edit: too slow

OK, that was really stupid .. Just forgotten about the call by reference..

The program works, there's only one problem.. And I know why but I can't get it fixed..

In the function "voorkomentellen", it counts the number of occurence of each number and puts each different number in the vector "getelde_rij" and the number of occurence in the vector "voorkomen"..

But when the function "printint" prints each number together with its number of occurence, it doesn't give the 2 largest numbers..
I know why : its because of the "getallen.size( )-1" in the for-loop and the "getelde_rij.size( )-1" in the for-loop.. Actually they should be their without the "-1", but if I do that, the program says that the vector is out of range..

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;


void drukintro ();
void sorteerint( vector<int>& getallen );
void voorkomentellen ( vector<int> getallen , vector<int>& getelde_rij, vector<int>& voorkomen );
void printintrij( vector<int> getelde_rij, vector<int> voorkomen );

void drukintro ()
{
	int i;
	cout << "Dit is een aangepaste versie van Lab9Opgave5 waarbij" << endl
		 << "de arrays vervangen zijn door vectors" << endl << endl
		 << "Dit is een programma dat een rij van getallen sorteert" << endl
		 << "en het voorkomen van elk getal telt," << endl 
		 << "en daarna van hoog naar laag op het scherm drukt." << endl;
	for ( i=0; i<=54; i++)
	{
		cout << "-";
	}
	cout << endl << endl;

}

void sorteerint( vector<int>& getallen)
{	
	unsigned int i, j, temp;

	for (i=0; i<getallen.size( ); i++)
		for (j=getallen.size( )-1; j>i; j--)
		{
			if (getallen[j-1]>getallen[j]) 
			{	
				temp=getallen[j];

				getallen[j]=getallen[j-1];
				getallen[j-1]=temp;
			}
		}
}

void voorkomentellen ( vector<int> getallen , vector<int>& getelde_rij, vector<int>& voorkomen )
{
	unsigned int i, teller; 
	int getal1, getal2, count=1;
	for (i = 0; i < getallen.size( )-1; i++)
	{
		getal1 = getallen[i];
		getal2 = getallen[i+1];

		if (getal1 == getal2)
		{
			count++;
		}
		else
		{
			getelde_rij.push_back(getal1);
			voorkomen.push_back(count);
			count=1;
		}
	}
}
	

void printintrij( vector<int> getelde_rij, vector<int> voorkomen )
{
	unsigned int i;
	cout.setf(ios::left);
	cout << "\nN    Voorkomen\n";
	for (i=0; i < getelde_rij.size( )-1; i++)
		cout << setw(5) << getelde_rij[i] << voorkomen[i] << endl;
	cout << endl;
}
	

int main ()
{
	vector<int> v1, v2, v3;

	drukintro ();

	cout << "Geef een reeks positieve gehele getallen in," << endl
		 << "gevolgd door een negatief getal om de reeks af te sluiten : " << endl;
	
	int getal;
	cin >> getal;
		
	while (getal > 0)
	{
		v1.push_back(getal);
		cin >> getal;
	}
	sorteerint ( v1);
	voorkomentellen ( v1, v2, v3);
	for (unsigned int i=0; i<v2.size( ); i++)
		cout << v2[i] << ' ';
	
	printintrij( v2 , v3);
}

Try a slightly different algorithm

void voorkomentellen ( vector<int> getallen , vector<int>& getelde_rij, vector<int>& voorkomen )
{
	unsigned int i, teller; 
	int getal1, getal2, count=1;
	getal1 = getallen[0];
	for (i = 1; i < getallen.size( ); i++)
	{
		getal2 = getallen[i];
		if (getal1 == getal2)
		{
			count++;
		}
		else
		{
			getelde_rij.push_back(getal1);
			voorkomen.push_back(count);
			count=1;
                           getal1 = getal2;                       
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.