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();
}

Recommended Answers

All 5 Replies

Where does the problem occur?

Im not shure thats what im haveing trouble with.

This is the original working code that im trying to convert using dynamic managemnt of memeory if it helps answer a question. Thats the toruble im haveing is trying to conver it.

#include <iostream>
#include <fstream>
using namespace std;

const int SIZE = 20;    // Maximum number of values in each array

// Function Prototypes
void getArrays(int [], int &, int [], int &, int);
int findIntersection(int [], int, int [], int, int [], int);
void displayIntArray(int [], int);
void saveArray(int [], int);


int main(){
      
   //Sets A ,B, C and the atual corresponding size 
   int setA[SIZE], setB[SIZE], setC[SIZE], sizeA=0, sizeB=0, sizeC=0;

   // Get values for the sets A and B from A.data and B.data.
   getArrays(setA, sizeA, setB, sizeB, SIZE);
   
   // Find the intersection of the two sets A and B
   sizeC = findIntersection(setA, sizeA, setB, sizeB, setC, SIZE);
   
   // Display setC (intersecting values of setA and setB) and save it in C.data
   saveArray(setC, sizeC);
   displayIntArray(setC, sizeC);
   
   system("pause");
   return 0;
}


void getArrays(int A[], int &sizeA, int B[], int &sizeB , 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();
     displayIntArray(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();
     displayIntArray(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;
    
//TODO

}


void displayIntArray(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;
     
     }

//TODO

}


void saveArray(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();
}

That's what you said before... but WHERE/WHAT is the trouble? Are there compiler errors? Does it crash? etc.

I dont know were the trouble is. The compiler doesn't give me errors. When it runs the .exe it crashes.

You should step through the code with a debugger. If you don't want to do that, you should sprinkle output statements throughout the code. The goal is to figure out where the code is crashing.

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.