Hi, Im trying to run this program using dynamic memory i cant seem to get it to keep running it crashes on me.
I have the working program so the code is correct im just haveing trouble trying to convert it correctly.
Thank You,
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE = 20;
void getVector(int *&, int *&, int);
int findIntersection(int [], int, int [], int, int *&, int);
void displayIntVector(int [], int);
void saveVector(int [], int);
int main(){
int *A=NULL;
int *B;
int *C;
int sizeA=0;
int sizeB=0;
int sizeC=0;
getVector(A, sizeA, B, sizeB, SIZE);
sizeC = findIntersection(A, sizeA, B, sizeB, C, SIZE);
saveVector(C, sizeC);
displayIntVector(C, sizeC);
system("pause");
return 0;
}
void getVector(int *&A, int *&B, int maxSize){
ifstream fileA;
fileA.open ("A.data");
if(!fileA){cout<<"error: opening a.data\n"; system("pause"); exit(0);}
fileA>>sizeA;
if(sizeA>=maxSize){cout<<"error: Too much data in A.data"; exit(0);};
cout<<"Reading data from a.data\n";
for(int index = 0; index < sizeA; index++) fileA>> A [index];
fileA.close();
displayIntVector(A, sizeA);
ifstream fileB;
fileB.open ("B.data");
if(!fileB){cout<<"error: opening a.data\n"; system("pause"); exit(0);}
fileB>>sizeB;
if(sizeB>=maxSize){cout<<"error: Too much data in A.data"; exit(0);};
cout<<"Reading data from a.data\n";
for(int index = 0; index < sizeB; index++) fileB>> B [index];
fileA.close();
displayIntVector(B, sizeB);
}
int findIntersection(int A[], int sizeA, int B[], int sizeB, int *&C, int size){
int index3 = 0;
for(int index1 = 0; index1 < sizeA; index1++)
for(int index2 = 0; index2 < sizeA; index2++)
if(A[index1] == B[index2]) C[index3++] = A[index1];
return index3;
}
void displayIntVector(int a[], int num){
if(!num) cout<<"There are no elements.\n";
else{
for (int index = 0; index < num; index++) cout << a[index] <<" ";
cout <<endl<< endl;
}
}
void saveVector(int a[], int size){
ofstream fileC;
fileC.open("C.data");
if(!fileC){cout<<"eror: opening c.data\n"; system("pause"); exit(0);}
cout<<"Saving Intersection(A,B) in c.data\n";
fileC<<size<<endl;
for(int index = 0; index < size; index++) fileC<<a[index]<<endl;
fileC.close();
}