DISTINCT INTEGERS

My program creates a vector of distinct random integers and prints the pairs that add up to user inputted size. However, the user should be able to enter a DIST_INT number of distinct integers used, for example, if DIST_INT=5 and SUM =9,

Your output could be:

Vector: 5 3 4 7 2 0 0 0 0 0 0 0 0 0 0

Print pairs
4--5
2--7

The rest of the numbers should be 0, How can I do this?
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
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;
unsigned int seed = (unsigned)time(0);
const int SIZE =10;
const int DIST_INT = 8;
void printPairs(vector <int> &v, int sum);
double random(unsigned int &seed);
void initialize_vector(vector<int> &v);
void print_vector (vector<int> :: iterator b,
                   vector<int> :: iterator e );
void unique(vector<int> &v);
int main()
{
    vector <int> v;
    initialize_vector(v);
    unique(v);
    print_vector(v.begin(),v.end());
    int sum = 6;
    printPairs(v,sum);
    return 0;
}
void printPairs(vector <int> &v, int sum)
{
    for (int i = 0; i < SIZE; i++)
        for (int j = i + 1; j < SIZE; j++)
            if (v[i] + v[j] == sum)
                cout << "(" << v[i] << ", " << v[j] << ")" << endl;
}
void initialize_vector(vector<int> &v)
{
    for (int i=0; i<SIZE; i++)
        v.push_back(random(seed)*11);
}
double random(unsigned int &seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
}

void print_vector (vector<int> :: iterator b,
                   vector<int> :: iterator e )
{
    vector<int> :: iterator p =b;
    cout << "Distinct integer in vector are\n";
    while(p<e)
        cout << setw(3) << (*p++);
    cout << endl;
}

void unique(vector<int> &v)
{
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
Hello cash,

First off by putting the green check on your question/topic you are telling everyone that you have an answer and do not need any help. I believe that you can edit your post and change to something else that is more appropriate.

When I loaded up your program I found a couple of problems:

In the function "unique" you call sort. Since you use using namespace std; this call std::sort from the "algorithm" header file which you did not include in your program.

In the function"printPairs" your for loops check "SIZE" to know when to end. This is a problem because chances are by the time you get to this function you have used the "unique" function to sort and erase the duplicate numbers. This could leave your vector with less than 10 elements, so when you use the for loops based on "SIZE" you are most likely to exceed the boundary of the vector causing the program to crash. See the changes in the following code.

In the function "random" you pass "seed" to the function. Since you defined "seed" as a global variable with file scope there is no need to pass this variable to a function because the entire, including all functions in the file, have access to this variable. By passing the variable to a function you make the variable local to the function. Thus the need to pass by reference.

Note: a few judiciously placed blank lines makes a big difference in readability.

I offer the following code as an example of what could be done. It could still use some refinement.
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <ctime>
#include <iostream>
#include <iomanip>
#include <string>  // <--- Added.
#include <vector>

#include <algorithm>  // <--- Added.

using namespace std;  // <--- Best not to use.

unsigned int seed = (unsigned)time(0);

const int SIZE = 10;
const int DIST_INT = 8;

void printPairs(vector <int> &v, int sum);
double random(unsigned int &seed);
void initialize_vector(vector<int> &v);
//void print_vector(vector<int> ::iterator b, vector<int> ::iterator e);
void print_vector(vector<int> v, std::string msg);  // <--- Changed.
void unique(vector<int> &v);

int main()
{
	vector <int> v;

	initialize_vector(v);

        // <--- Remove or comment this one if you do not want it. Used mostly for testing the program.
	print_vector(v, "Original vector\n");  // <--- Changed. Added this one to what the original vector holds.

	unique(v);

	print_vector(v, "Distinct integer in vector are\n");

	int sum = 6;

	printPairs(v, sum);

	return 0;
}

void printPairs(vector <int> &v, int sum)
{
	std::cout << std::endl;

	for (size_t i = 0; i < v.size(); i++)  // <--- Changed.
		for (size_t j = i + 1; j < v.size(); j++)  // <--- Changed.
			if (v[i] + v[j] == sum)
				cout << "(" << v[i] << ", " << v[j] << ")" << endl;
}

void initialize_vector(vector<int> &v)
{
	for (int i = 0; i < SIZE; i++)
		v.push_back(random(seed) * 11);
}

double random(unsigned int &seed)
{
	const int MODULUS = 15749;
	const int MULTIPLIER = 69069;
	const int INCREMENT = 1;
	seed = ((MULTIPLIER*seed) + INCREMENT) % MODULUS;
	return double(seed) / MODULUS;
}

//void print_vector(vector<int> ::iterator b,
//	vector<int> ::iterator e)
//{
//	vector<int> ::iterator p = b;
//	cout << "Distinct integer in vector are\n";
//	while (p < e)
//		cout << setw(3) << (*p++);
//	cout << endl;
//}

void print_vector(vector<int> v, std::string msg)
{
	cout << msg;

	for (auto& p : v)
		cout << setw(3) << p;

	std::cout << '\n';

	// <--- If you are not up to the range based for loop.
	//for (std::vector<int>::iterator it = v.begin(); it < v.end(); it++)
	//{
	//	cout << setw(3) << *itI changed the function "print" to b ;
	//}
	//std::cout << std::endl;
}

void unique(vector<int> &v)
{
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
}

For the header files I found by accident that putting them in alphabetical order helps to realize when one is missing. The blank line before "algorithm" is to separate the more common header files from the extras that are needed for the program.

using namespace std; in the global scope of the file is a bad idea. There have been many posts here about this and maybe even an article or two. Do a search if to read about this.

In "main" I added the extra "print" function for testing. I also made the function more generic. Passing the two iterators is not necessary. All you need is to pass the vector and you can get the iterators in the function. I added the parameter "msg" so that the function call and function can be used in more than one way. Keep this if you want, but at lease remember it for the future.

In "printPairs" the changes are noted.

In "initialize_vector" "seed" is a global variable and does not need to be passed to "random".

In "random" "seed" does not need to be a parameter of the function just used.

I changed the function "print_vector" to be more generic. you are not required to use the second parameter, but passing the vector is a must. If you can use the range based for loop it makes things easier. If not then there is a regular for loop that does the same thing.

Note: in your original "print_vector" creating the variable "p" is not really needed. Since the two iterators are passed by value they are only a copy of the original and when the function ends so do these variables. Incrementing a copy of "b" will not change the value of "b" in the calling function as it is a local variable.

In the function "unique" the "sort" becomes "std::sort" because of "using namespace std;". With out the header file "algorithm" the compiler can not fine any reference to this function.

Hope that helps,

Andy
Hello cash,

Sorry I almost missed this. If you want your output to look like this:

Vector: 5 3 4 7 2 0 0 0 0 0 0 0 0 0 0


You will have to add the zeros at the end because they will not be in the vector.

A for loop in the 'print_vector "function should handle this.

Andy
Topic archived. No new replies allowed.