How to pass an array to a class? - c++

#include <iostream>
#include <string>
using namespace std;
class Term;
class Polynomial;
class Term {
public:
friend class Polynomial;
void set_term(string s);
int get_pow();
int get_coefficient();
private:
int coefficient;
int pow;
};
class Polynomial {
public:
friend class Term;
Polynomial(int s, Term t[]);
int get_size();
Term *myterm;
private:
int P_size;
};
void Term::set_term(string s) {
coefficient = stoi(s.substr(1, 1));
pow = stoi(s.substr(4, 1));
if (s[0] == '-') {
coefficient = -coefficient;
}
}
int Term::get_coefficient() { return coefficient; }
int Term::get_pow() { return pow; }
Polynomial::Polynomial(int s, Term t[]) {
P_size = s;
myterm = new Term[s];
for (int i = 0; i < s; i++) {
myterm[i].coefficient = t[i].coefficient;
cout << i << " Term " << t[i].coefficient << endl;
cout << i << " Polynomial " << myterm[i].coefficient << endl;
myterm[i].pow = t[i].pow;
}
}
Polynomial::get_size() { return P_size; }
int main() {
string x1;
cin >> x1;
int size_x1 = x1.size();
Term term1[size_x1];
for (int i = 0; i < size_x1; i += 5) {
term1[i].set_term(x1.substr(i, 5));
}
Polynomial p1(size_x1 / 5, term1);
for (int i = 0; i < size_x1; i += 5) {
cout << term1[i].get_coefficient() << "x^";
cout << term1[i].get_pow() << endl;
}
cout << "------------------" << endl;
for (int i = 0; i < p1.get_size(); i++) {
if (p1.myterm[i].get_coefficient() > 0)
cout << "+";
cout << p1.myterm[i].get_coefficient();
cout << "x^";
cout << p1.myterm[i].get_pow() << endl;
}
return 0;
}
term1 in main is working right but when I pass it to p1(Polynomial) just t[0] and, myterm[0]
is true I mean I pass term1 to p1 as t so t[0] = term1[0] and myterm[0] = t[0]
Polynomial::Polynomial(int s, Term t[]) {
P_size = s;
myterm = new Term[s];
for (int i = 0; i < s; i++) {
myterm[i].coefficient = t[i].coefficient;
cout << i << " Term " << t[i].coefficient << endl;
cout << i << " Polynomial " << myterm[i].coefficient << endl;
myterm[i].pow = t[i].pow;
}
}
in Polynomial constructor both t[1].coeffient and, myterm[1].coefficient are 4941660(for instance)
however, t is term1 and in term1 term1[1] is the 4(for instance)
if term1[1] is 4 so t[1] should be 4 but it's not so myterm[1], is not 4 too.

There's a lot of confusion over sizes here
string x1;
cin >> x1;
int size_x1 = x1.size();
Term term1[size_x1];
for (int i = 0; i < size_x1; i += 5) {
term1[i].set_term(x1.substr(i, 5));
}
Polynomial p1(size_x1 / 5, term1);
You didn't say what your input is, but suppose it is "1234567890" then size_x1 equals 10, so term1 will also have size 10. Then your for loop will execute twice, with i equal to 0 and i equal to 5, so you will assign 12345 to term1[0] and 67890 to term1[5]. That makes no sense to me. term1 has size 10 but the only elements assigned to are zero and five.
Then you create p1 with size size_x1/5 which equals 2. So the constructor for p1 will look at term1[0] and term1[1], but in the for loop you assigned to term1[0] and term1[5].
You have to think carefully about what the code you are writing is actually doing. The compiler will do exactly what you tell it to. It's not like talking to a person who can understand what you really meant. So you have to be very careful. This is I think you really meant (but I could be wrong)
string x1;
cin >> x1;
int size_x1 = x1.size();
int size_term1 = size_x1/5;
Term term1[size_term1];
for (int i = 0; i < size_term1 ; ++i) {
term1[i].set_term(x1.substr(5*i, 5));
}
Polynomial p1(size_term1, term1);
I created a new variable size_term1 with the size of the term array, which is now the sizeof the input divided by 5. And I changed the for loop so that it assigns to term1[0] and term1[1].
So your problem has nothing to do with passing an array to a class, which you did perfectly correctly. It was not thinking clearly about the code you were writing, and making mistakes in the logic.

Related

Windows C++ char not displaying properly, just some unknkown character displaying

I have written the code for RSA in C++ on Ubuntu. It was working fine on that, it's working fine on Windows Dev C++ as well, but it doesn't show the character properly.
Here is the code :
#include<iostream>
#include<stdlib.h> // for rand()
#include<math.h> // for floor function
#include<string.h>
using namespace std;
//function to check whether a number is prime or not
int check_prime(int number)
{
int count = 0;
for(int i = 2; i<number + 1; i++)
{
if(number%i == 0)
{
count++;
}
}
if(count>2)
{
return 0;
}
else
{
return 1;
}
}
//function to generate a random prime number
int generate_random_prime()
{
int temp;
while(1)
{
temp = rand() % 50;
if(check_prime(temp) == 1)
{
return temp;
}
}
}
int gcd(int a, int b)
{
int temp;
while(b != 0)
{
temp = b;
b = a%b;
a = temp;
}
return a;
}
// Extended Euclid GCD to find d such de congruent to 1
int extended_gcd(int a, int b)
{
int d, x, y, r, q;
if(b == 0)
{
d = a;
x = 1;
y = 0;
cout << "\n d= " << d << " x= " << x << " y= " << y << "\n";
}
int x2, x1, y2, y1;
x2 = 1;
x1 = 0;
y2 = 0;
y1 = 1;
while(b > 0)
{
q = floor(a / b);
r = a - q*b;
x = x2 - q*x1;
y = y2 - q*y1;
a = b;
b = r;
x2 = x1;
x1 = x;
y2 = y1;
y1 = y;
}
d = a;
x = x2;
y = y2;
return x2;
}
//returns a^b mod n using square and multiply method
int modular_exponentiation(int a, int b, int n)
{
if(a == 1)
{
return 0;
}
int c = 1;
for(int i = 1; i < b + 1; i++)
{
c = (c*a) % n;
}
return c;
}
//cipher text = (message^e) %n
int cipher_text(int m, int e, int n)
{
return modular_exponentiation(m, e, n);
}
//decrypted_text= (cipher^d)%n
int decrypt_cipher(int c, int d, int n)
{
return modular_exponentiation(c, d, n);
}
int main()
{
// generating two random prime p and q
int p = generate_random_prime();
int q = generate_random_prime();
cout << "Prime p : " << p << "and q : " << q << "\n";
int n = p*q;
cout << "n=p*q = " << n << "\n";
//calculating Euler Totient for prime p and q
int euler_phi = (p - 1)*(q - 1);
cout << "Euler totient is : " << euler_phi << "\n";
int d, e;
// calculating e such that 1<e<euler_phi and gcd(n,euler_phi)=1
while(1)
{
e = rand() % (euler_phi - 1 + 1) + 1;
if(gcd(euler_phi, e) == 1)
{
break;
}
}
cout << "e value is : " << e << "\n";
//calculating d such that ed congruent 1, ed=1
d = extended_gcd(e, euler_phi);
//d=5;
cout << "d value is : " << d << "\n";
//storing the message to be encrypted as char array and encrypting each char element
char message[20];
int cipher[20];
cout << "Enter the message to be encrypted : ";
cin >> message;
cout << "Message to be encrypted is : " << message << "\n";
int size = strlen(message);
//calculating cipher text c
for(int i = 0; i < size; i++)
{
cipher[i] = cipher_text(int(message[i]), e, n);
}
cout << "Cipher text is : ";
for(int i = 0; i < size; i++)
{
cout << cipher[i] << " ";
}
char message_decrypted[size];
//decrypting cipher text
for(int i = 0; i < size; i++)
{
message_decrypted[i] = decrypt_cipher(cipher[i], d, n);
}
cout << "\nDecrypted message is : ";
for(int i = 0; i < size; i++)
{
cout << message_decrypted[i];
}
cout << "\n";
return 0;
}
I have tried the code on DevC++ and using g++.
Check the images :
Image using g++ compiler
I need a way to print the char to be displayed properly.
I think that message_decrypted[i]=decrypt_cipher(cipher[i],d,n); needs to be changed to print the character properly in Devcpp
Here is the link to the code in online IDE where it works fine https://repl.it/#shubhamjohar/RSA
When your main routine invokes
decrypt_cipher(cipher[i], d, n);
cipher[0] is 386 as matching your output above. d is -179. And n is 697
The corresponding call into modular_exponentiation(a=386, b=-179, n=697) results in this for-loop getting skipped:
for (int i = 1; i<b + 1; i++) {
c = (c*a) % n;
}
Because i < (b + 1) evaluates to (1 < -178), which evaluates to false.
Therefore, your modular_exponentiation returns 1, which is an unprintable character.
Same applies for the subsequent calls to decrypt_cipher from main.
I don't know enough about the RSA algorithm to know if your implementation is correct. But when d is negative, that for-loop isn't going to do any loops.
Maybe it is incurred by the following expression in your program:
char message_decrypted[size];
There is some standard change related to this usage. please read the following page for more details.
https://www.geeksforgeeks.org/variable-length-arrays-in-c-and-c/
Or try to use something like new char[size] to allocate memory dynamically.

C++ Dynamic Arrays and Pointers in Polynomial program causes exit error 255

I'm currently doing a program that read in a degree, and coefficients and creates a polynomial struct. The program can add and multiply the polynomials, then output the sum or product. The program runs, outputs the correct answer, and then gets a windows error:
poly.exe has stopped working
A problem cause the program to stop working correctly. Windows will close the program and notify you if a solution is available.
then scite shows an exit code: 255
I think it could be something in the double for loop I am using for the multiply poly, or with the initialization of the pointer to the array of coefficients. But I cannot figure out what. The could I am using for it is as follows:
#include <iostream>
#include <stdio.h>
using namespace std;
struct Poly {
int degree;
int *coeff; //array of coefficients from lowest degree to highest degree
};
//Reads the coefficients of a polynomial from standard input
//Creates a poly struct and returns a pointer to the poly
Poly* readPoly();
//Outputs a polynomial to standard output with the variable x
void outputPoly(const Poly* p, char x);
//Computes the sum of two polynomials and returns
//a pointer to the new poly struct which is their sum
Poly* addPoly(const Poly* a, const Poly* b);
//Computes the product of two polynomials and returns
//a pointer to the new poly struct which is their product
Poly* multPoly(const Poly* a, const Poly* b);
//Returns to the heap the memory allocated for the polynomial
//and sets p to the nullptr
void deletePoly(Poly* &p);
Poly* readPoly() {
int deg;
//Read the highest degree
cout << "Input the degree: ";
cin >> deg;
//Handles when the degree is == 0
if(deg == 0) {
int *C = new int[deg+1];
Poly *p;
p = new Poly;
p->degree = deg;
p->coeff = C;
return p;
}
int *C = new int[deg+1];
//Read the coefficients
cout << "Input the coefficients: ";
for(int i = 0; i <= deg; i++) {
cin >> C[i];
}
//Create a new poly structure, assign its fields
//and retun a pointer to the new structure
Poly *p;
p = new Poly;
p->degree = deg;
p->coeff = C;
return p;
}
void outputPoly(const Poly* p, char x) {
//Set the degree and cooefficients to be used in the loop
int d = p->degree;
int *C = p->coeff;
//Output the polynomial, depending on the degree/coeff
for(int i = 0; i <= d; i++) {
//if the coeff is zero, and the degree is > 1
if(C[i] == 0 && i > 0)
continue; //Skip the +
//if the degree is 0, and the coeff is 0
else if(i == 0 && C[i] == 0) {
cout << C[i];
continue; //Skip the +
}
//if the degree is 0, and the coeff is not 0
else if(i == 0 && C[i] != 0)
cout << C[i];
//if the degree is 1, and the coeff is 0
else if(C[i] == 0 && i == 1)
cout << x;
//if the degree is 1, and the coeff is 1
else if(C[i] == 1 && i == 1)
cout << x;
//if the degree is 0, and the coeff is > 0
else if(C[i] > 0 && i == 0)
cout << C[i] << "*" << x;
//if the coefficient is 1
else if(C[i] == 1)
cout << x << "^" << i;
//if the degree is 1
else if(i == 1)
cout << C[i] << "*" << x;
//any other circumstance
else
cout << C[i] << "*" << x << "^" << i;
//Print a +, as long as it's not the last term
if(i != d)
cout << " + ";
}
}
void deletePoly(Poly* &p) {
delete[] p->coeff; //Delete the array first
delete p;
p = nullptr;
}
const Poly* getLargest(const Poly* a, const Poly* b) {
//Helper function to get the larger polynomial, given two
if(a->degree > b->degree)
return a;
else
return b;
}
const Poly* getSmallest(const Poly* a, const Poly* b) {
//Helper function to get the smaller polynomial, given two
if(a->degree < b->degree)
return a;
else
return b;
}
Poly* addPoly(const Poly* a, const Poly* b){
int i, j;
int *polyOneC = a->coeff;
int *polyTwoC = b->coeff;
//The new polynomials degree is the size of the polynomial that is the largest
int polyThreeD = getLargest(a, b)->degree;
int *polyThreeC = new int[polyThreeD];
for(i = 0, j = 0; j <= polyThreeD; i++, j++) {
//If the polynomials are of different size,
//then any coefficent term over the size
//of the smaller polynomial degree stays the same
if(i > getSmallest(a, b)->degree)
polyThreeC[i] = getLargest(a, b)->coeff[i];
else
//Otherwise, just add them
polyThreeC[i] = polyOneC[j] + polyTwoC[j];
//"Shifts" if it's equal to zero
if(polyThreeC[i] == 0)
i--;
}
//Ensures the remaining terms have a coefficient value(0)
while(i <= polyThreeD) {
polyThreeC[i] = 0;
i++;
}
Poly *sumPoly;
sumPoly = new Poly;
sumPoly->degree = polyThreeD;
sumPoly->coeff = polyThreeC;
return sumPoly;
}
Poly* multPoly(const Poly* a, const Poly* b) {
//Get the degrees and arrays of coefficients
int polyOneD = a->degree;
int polyTwoD = b->degree;
int *polyOneC = a-> coeff;
int *polyTwoC = b-> coeff;
int polyThreeD = polyOneD + polyTwoD;
int *polyThreeC = new int[polyThreeD + 1];
//Initialize the array of coefficients
for(int i = 0; i <= polyThreeD; i++)
polyThreeC[i] = 0;
//Multiple the coeffients and add to the position of the degree
for(int i = 0; i <= polyOneD; i++) {
for(int j = 0; j <= polyTwoD; j++) {
polyThreeC[i + j] += (polyOneC[i] * polyTwoC[j]);
}
}
//Create a new polynomial pointer and return it
Poly *productPoly;
productPoly = new Poly;
productPoly->degree = polyThreeD;
productPoly->coeff = polyThreeC;
return productPoly;
}
int main() {
Poly *x = readPoly();
Poly *y = readPoly();
//Test the add poly function
cout << "(";
outputPoly(x, 'x');
cout << ")";
cout << " + ";
cout << "(";
outputPoly(y, 'x');
cout << ")";
cout << endl;
//Call addPoly
Poly *z = addPoly(x, y);
cout << "= ";
cout << "(";
outputPoly(z, 'x');
cout << ")";
cout << endl;
cout << endl;
//Test the multiply poly function
cout << "(";
outputPoly(x, 'x');
cout << ")";
cout << " * ";
cout << "(";
outputPoly(y, 'x');
cout << ")";
cout << endl;
//Call multPoly
z = multPoly(x, y);
cout << "= ";
cout << "(";
outputPoly(z, 'x');
cout << ")";
cout << endl;
//Delete the polynomials now that we are done with them
deletePoly(x);
deletePoly(y);
deletePoly(z);
}
one other thing is that the error doesn't occur everytime it's run, so far I've witnessed it when I enter a polynomial with a degree of say 4. As opposed to a polynomial with a degree of 2, it works fine.
Can anyone help me!
You allocated coefficient array in addPoly (polyThreeC) isn't big enough. You forgot to add one to the degree when allocating the array.

Debugging a merge sort

void CensusData::mergeSort(int type) {
if(type == 0)
MERGE_SORT(type, 0, data.size());
}
void CensusData::MERGE_SORT(int type, int p, int r){
//int q;
//cout << "data size " << data.size() << endl;
std::cout << "MERGE_SORT START ///("<< p << ", " << r << ")" <<std::endl;
if(p < r)
{
int q = (p + r)/2;
MERGE_SORT(type, p, q);
MERGE_SORT(type, q + 1, r);
MERGE(type, p, q ,r);
}
}
void CensusData::MERGE(int type, int p, int q, int r){
if(type == 0)
{
std::cout << "MERGING" << std::endl;
//int n1;
//int n2;
int n1 = q - p + 1;
int n2 = r - q;
int L[n1 + 1];
int R[n2 + 1];
for(int i = 1; i < n1; i++)
{
cout << "filling Left Array" << endl;
L[i] = data[p + i - 1]->population;
}
for(int j = 1; j < n2; j++)
{
cout << "filling Right Array" << endl;
R[j] = data[q + j]->population;
}
int i = 1;
int j = 1;
for(int k = p; p < r; p++)
{
cout << "for loop: " << endl;
if(L[i] <= R[j])
{
cout << "TRUE" << endl;
data[k]->population = L[j];
i = i + 1;
}
/*else if(data[k]->population == R[j])
{
cout << "FALSE" << endl;
j = j + 1;
}*/
else
{
data[k]->population = R[j];
j = j + 1;
}
}
}
}
do not worry about type, it wont effect this program at all. basically i am trying to make a merge sort that will take a vector containing an integer, the vector looks like this:
class Record { // declaration of a Record
public:
std::string* city;
std::string* state;
int population;
Record(std::string&, std::string&, int);
~Record();
};
std::vector<Record*> data;
basically i have been trying to get it to actually sort, but it doesn't seem to work at all, i have even seen garbage in the program.
example input:
237 812826 68642
output:
4484540 812826 68642
Note: all of the rest of the program works fine (tested it with an insertion sort) only this part is not working.
Take a look at lecture 15 of the excellent Stanford Universities course Programming Abstractions. It covers all kinds of sorts including merge:
http://see.stanford.edu/see/lecturelist.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e
You can even get the source code from SourceForge:
http://sourceforge.net/projects/progabstrlib/files/

Merge sort code debugging

I am trying to write a code for merge sort. I am not getting the correct output. I am following this pseudocode link Following is my code. I pass my unsorted array into merge_sort function and call merge function recursively to sort and combine the sub arrays.I know there are more simpler and efficient ways to write code for merge sort but I want to try on my own otherwise I won't learn. Thanks in advance.
int* merge_sort(int* a,int size)
{
//cout<<size;
//cout<<"hi";
if(size == 1)
{
//cout<<"less";
//cout<<a[0];
return a;
}
int* left;
int* right;
int middle = ceil(size/2);
left = new int(middle);
right = new int(middle);
for(int i=0;i<middle;i++)
{
left[i]=a[i];
//cout<<left[i];
}
cout<<"\t";
for(int j=middle;j<size;j++)
{
right[j]=a[j];
//cout<<right[j];
}
cout<<"\t";
left = merge_sort(left,middle);
//if(size==2)
//cout<<left[0];
right = merge_sort(right,middle);
//if(size==2)
//cout<<right[0];
return merge(left,right,middle);
}
int* merge(int* l,int* r,int m)
{
int* result;
result = new int(2*m); //to store the output
int lsize=m; // to keep track of left sub list
int rsize=m; // to keep track of right sub list
int counter = 0; // will use to index result
//cout<<m;
while(lsize>0 || rsize>0)
{
if(lsize>0 && rsize>0)
{
if(l[0]<=r[0])
{
result[counter]=l[0];
counter++; //to store next value in result
lsize--;
l=&l[1]; //decrementing the size of left array
}
else
{
result[counter]=r[0];
counter++;
rsize--;
r=&r[1]; //dec. size of right array
}
}
else if(lsize>0)
{
result[counter]=l[0];
counter++;
lsize--;
l=&l[1];
}
else if(rsize>0)
{
result[counter]=l[0];
counter++;
lsize--;
l=&l[1];
}
}
return result;
}
Your code:
int *left = new int(middle);
allocates a single integer initialized to middle. You need:
int *left = new int [middle];
which allocates an array of middle integers. Rinse and repeat for int *right. Actually, you need to use:
int *right = new int [size - middle];
This gets the correct size for the right array. You then have to modify the recursive call to merge_sort() for the right sub-array:
merge_sort(right, size - middle);
Finally, you have to rewrite merge() to take the size of the left array and the size of the right array independently, because they may be of different sizes. For example, if you sort 10 elements,
you then end up with a call to merge two arrays of 5 (which is fine), but at the next level you need to merge an array of 2 and an array of 3 elements (and you're hosed).
The allocation of result also has the () vs [] allocation problem. And there are some other as yet unresolved problems. But these are important steps in the right direction.
As mentioned in a comment to the question, you have a monumental memory leakage problem, too. What's more, it is not trivial to fix because merge_sort() does an early exit without allocating new memory, so it isn't as simple as 'delete the memory returned by merge_sort()'.
Copy and paste is wonderful until you forget to edit the pasted copy correctly:
else if (lsize > 0)
{
result[counter] = l[0];
counter++;
lsize--;
l = &l[1];
}
else if (rsize > 0)
{
result[counter] = l[0];
counter++;
lsize--;
l = &l[1];
}
Methinks you should be using r and rsize in the second of these blocks.
This still isn't the whole story...
And the residual problem (apart from memory management, which is still 100% leaky and problematic) is:
for(int j=middle;j<size;j++)
{
right[j]=a[j];
//cout<<right[j];
}
You're copying into parts of right that you've not allocated. You need something more like:
for(int j = 0; j < size - middle; j++)
{
right[j] = a[j + middle];
//cout<<right[j];
}
This code works as long as you always sort at least two items at the top level (you crash freeing unallocated space if you sort 1 item — that's part of the memory management problem).
#include <iostream>
using namespace std;
namespace {
int *merge(int *l, int m, int *r, int n);
void dump_array(int *a, int size)
{
int i;
cout << size << ": ";
for (i = 0; i < size; i++)
{
cout << ' ' << a[i];
if (i % 10 == 9)
cout << '\n';
}
if (i % 10 != 0)
cout << '\n';
}
};
int *merge_sort(int *a, int size)
{
cout << "-->> merge_sort:\n";
dump_array(a, size);
if (size <= 1)
{
cout << "<<-- merge_sort: early return\n";
return a;
}
int middle = size/2;
int *left = new int[middle];
int *right = new int[size - middle];
cout << middle << ": ";
for (int i = 0; i < middle; i++)
{
left[i] = a[i];
cout << ' ' << left[i];
}
cout << "\n";
cout << (size - middle) << ": ";
for (int j = 0; j < size - middle; j++)
{
right[j] = a[j + middle];
cout << ' ' << right[j];
}
cout << "\n";
cout << "MSL:\n";
int *nleft = merge_sort(left, middle);
cout << "NL: ";
dump_array(nleft, middle);
cout << "OL: ";
dump_array(left, middle);
cout << "OR: ";
dump_array(right, size - middle);
cout << "MSR:\n";
int *nright = merge_sort(right, size - middle);
cout << "NR: ";
dump_array(nright, size - middle);
cout << "NL: ";
dump_array(nleft, middle);
cout << "OL: ";
dump_array(left, middle);
cout << "OR: ";
dump_array(right, size - middle);
int *result = merge(nleft, middle, nright, size - middle);
cout << "<<-- merge_sort:\n";
dump_array(result, size);
return result;
}
namespace {
int *merge(int *l, int m, int *r, int n)
{
int *result = new int[m + n];
int lsize = m;
int rsize = n;
int counter = 0;
cout << "-->> merge: (" << m << "," << n << ")\n";
dump_array(l, m);
dump_array(r, n);
while (lsize > 0 || rsize > 0)
{
if (lsize > 0 && rsize > 0)
{
if (l[0] <= r[0])
{
result[counter] = l[0];
cout << "C: " << counter << "; L = " << l[0] << "; LS = " << lsize << '\n';
counter++;
lsize--;
l++;
}
else
{
result[counter] = r[0];
cout << "C: " << counter << "; R = " << r[0] << "; RS = " << rsize << '\n';
counter++;
rsize--;
r++;
}
}
else if (lsize > 0)
{
result[counter] = l[0];
cout << "C: " << counter << "; L = " << l[0] << "; LS = " << lsize << '\n';
counter++;
lsize--;
l++;
}
else if (rsize > 0)
{
result[counter] = r[0];
cout << "C: " << counter << "; R = " << r[0] << "; RS = " << rsize << '\n';
counter++;
rsize--;
r++;
}
}
cout << "<<-- merge:\n";
dump_array(result, m+n);
return result;
}
};
int main()
{
for (int i = 2; i <= 10; i++)
{
int array1[] = { 9, 3, 5, 7, 1, 8, 0, 6, 2, 4 };
cout << "\nMerge array of size " << i << "\n\n";
int *result = merge_sort(array1, i);
delete[] result;
}
return 0;
}
This is the debug-laden code. It's the level to which I went to get the result. I could perhaps have used a debugger. Were I on a machine where valgrind works, it might have helped too (but it does not work on Mac OS X 10.8.x, sadly).
There are still many, many ways to improve the code — including the memory management. You'd probably find it easiest to pass the input array to merge() for use as the result array (avoiding the memory allocation in that code). This would reduce the memory management burden.
When you remove the debug code, you'll need to call the dump_array() function in the main() program to get the before and after sorting array images.
Code converted to template functions and leak-free
I've simplified the code a fair bit, especially in the merge() function. Also, more as a matter of curiosity than anything else, converted it to a set of template functions, and then used them with 4 different array types (int, double, std::string, char). The amount of debugging has been dramatically reduced, and the main debugging is conditional on being compiled with -DTRACE_ENABLED now.
The code is now leak-free; valgrind on a Linux box (virtual machine) gives it a clean bill of health when there are no exceptions. It is not guaranteed exception-safe, though. In fact, given the naked uses of new and delete, it is pretty much guaranteed not to be exception-safe. I've left the namespace control in place, but I'm far from convinced it is really correct — indeed, I'd lay odds on it not being good. (I'm also curious if anyone has any views on how to layout code within a namespace { … }; block; it seems odd not indenting everything inside a set of braces, but …)
#include <iostream>
using namespace std;
namespace {
#if !defined(TRACE_ENABLED)
#define TRACE_ENABLED 0
#endif
enum { ENABLE_TRACE = TRACE_ENABLED };
template <typename T>
void merge(T *l, int m, T *r, int n, T *result);
template <typename T>
void dump_array(const char *tag, T *a, int size)
{
int i;
cout << tag << ": (" << size << ") ";
for (i = 0; i < size; i++)
{
cout << " " << a[i];
if (i % 10 == 9)
cout << '\n';
}
if (i % 10 != 0)
cout << '\n';
}
};
template <typename T>
void merge_sort(T *a, int size)
{
if (size <= 1)
return;
if (ENABLE_TRACE)
dump_array("-->> merge_sort", a, size);
int middle = size/2;
T *left = new T[middle];
T *right = new T[size - middle];
for (int i = 0; i < middle; i++)
left[i] = a[i];
for (int j = 0; j < size - middle; j++)
right[j] = a[j + middle];
merge_sort(left, middle);
merge_sort(right, size - middle);
merge(left, middle, right, size - middle, a);
delete [] left;
delete [] right;
if (ENABLE_TRACE)
dump_array("<<-- merge_sort", a, size);
}
namespace {
template <typename T>
void merge(T *l, int m, T *r, int n, T *result)
{
T *l_end = l + m;
T *r_end = r + n;
T *out = result;
if (ENABLE_TRACE)
{
cout << "-->> merge: (" << m << "," << n << ")\n";
dump_array("L", l, m);
dump_array("R", r, n);
}
while (l < l_end && r < r_end)
{
if (*l <= *r)
*out++ = *l++;
else
*out++ = *r++;
}
while (l < l_end)
*out++ = *l++;
while (r < r_end)
*out++ = *r++;
if (ENABLE_TRACE)
dump_array("<<-- merge", result, m+n);
}
};
#include <string>
int main()
{
for (size_t i = 1; i <= 10; i++)
{
int array1[] = { 9, 3, 5, 7, 1, 8, 0, 6, 2, 4 };
if (i <= sizeof(array1)/sizeof(array1[0]))
{
cout << "\nMerge array of type int of size " << i << "\n\n";
dump_array("Original", array1, i);
merge_sort(array1, i);
dump_array("PostSort", array1, i);
}
}
for (size_t i = 1; i <= 10; i++)
{
double array2[] = { 9.9, 3.1, 5.2, 7.3, 1.4, 8.5, 0.6, 6.7, 2.8, 4.9 };
if (i <= sizeof(array2)/sizeof(array2[0]))
{
cout << "\nMerge array of type double of size " << i << "\n\n";
dump_array("Original", array2, i);
merge_sort(array2, i);
dump_array("PostSort", array2, i);
}
}
for (size_t i = 1; i <= 10; i++)
{
std::string array3[] = { "nine", "three", "five", "seven", "one", "eight", "zero", "six", "two", "four" };
if (i <= sizeof(array3)/sizeof(array3[0]))
{
cout << "\nMerge array type std::string of size " << i << "\n\n";
dump_array("Original", array3, i);
merge_sort(array3, i);
dump_array("PostSort", array3, i);
}
}
for (size_t i = 1; i <= 10; i++)
{
char array4[] = "jdfhbiagce";
if (i <= sizeof(array4)/sizeof(array4[0]))
{
cout << "\nMerge array type char of size " << i << "\n\n";
dump_array("Original", array4, i);
merge_sort(array4, i);
dump_array("PostSort", array4, i);
}
}
return 0;
}

intersection of sets in C++

i am trying to do simple sets intersection , it works correctly , but when output comes , it shows only memory address or garbage random value , help me ,i applied breakpoints , but it's not working . actually i am new in C++
#include<iostream>
using namespace std;
class set
{
private:
int size;
int *elem;
public:
set()
{
size = 0;
elem = NULL;
}
set(int s);
~set();
set(set &s);
set intersection(set A, int z);
void inputset();
void outputset();
};
set::set(int s)
{
size = s;
elem = new int[s];
}
void set::inputset()
{
int i;
cout << "Enter the set Element" << endl;
for(i = 0 ; i < size ; i++)
{
cin >> elem[i];
}
}
set set::intersection(set A, int z)
{
int i, j, k = 0;
set R(z);
for(i = 0; i < size; i++)
{
for(j = 0; j < A.size; j++)
{
if(elem[i] == A.elem[j])
{
R.elem[k] = A.elem[j];
k++;
break;
}
}
}
return R;
}
set::~set()
{
delete []elem;
}
set::set(set &s)
{
int i;
if(size > 0)
{
delete []elem;
}
size = s.size;
elem = new int[size];
for(i = 0; i < size; i++)
{
elem[i] = s.elem[i];
}
}
void set::outputset()
{
int i;
cout << "The elements of new set is : " << endl;
for(i = 0; i < size; i++)
{
cout << elem[i] << endl;
}
cout << endl;
}
int main()
{
int x, y, z;
char choice;
cout << "Enter sizeof set A" << endl;
cin >> x;
set S1(x);
S1.inputset();
S1.outputset();
cout << "Enter sizeof set B" << endl;
cin >> y;
set S2(y);
S2.inputset();
S2.outputset();
z = x + y;
set S3(z);
cout << "Enter I for intersection" << endl << "Enter U for union" << endl << "Enter D for difference" << endl;
cin >> choice;
switch(choice)
{
case'I':
S3 = S1.intersection(S2, z);
S3.outputset();
break;
default:
cout << "Invalid entry";
}
return 0;
}
There are several ways to check intersections between two sets, if your sets are in arrays, I'd suggest looping one of them and have a check if it's in between the other:
for (int i = 0; i < element1_size; i++) {
for (int u = 0; u < element2_size; u++) {
if (element1[i] == element2[u]) {
cout << "Intersection point : " << element1[i] << endl;
}
}
}
Your copy ctor should be set(const set &s); instead of set(set &s); What you have is a ctor that accepts a reference to set, which is not a copy ctor. So compiler generates default one for you and you have issue returning object by value from method intersection()
Also please fix your code indent, it is not easy to read it.
Also remove following lines from your "copy ctor":
if(size>0)
{
delete []elem;
}
You cannot delete that pointer, your object is not constructed yet.
How about using a map to keep track of the "intersected" elements in much more efficient way. Here is a snippet of what I have in mind:
std::map<int, int> m;
for(int i = 0; i < size; ++i)
m.insert( std::pair<int, int>(elem[i], 1) );
for(int i = 0; i < A.size; ++i)
if(m.count(A[i]) > 0)
R.elem[k++] = A[i]
This way you avoid the nested for-loops, and will be able to compute the intersection with logarithmic time complexity.