Hello,
I'm taking a c++ class and I'm very new to this. I have a project due where I'm to change a program from static memory to dynamic memory an help would greatly be appreciated it.

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



// 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;
}

//******************************************************************************
// Definition of function getArrays                      
// This function accepts two int arrays as arguments.    
// It prompts the user to enter 10 values for each array 
//******************************************************************************  
void getArrays(int A[], int &sizeA, int B[], int &sizeB){

   // Open A.data     
   ifstream file_A;
   file_A.open("A.data");
   if (!file_A) {cout<<"Error: opening A.data\n"; system("pause"); exit(0);}
   file_A>>sizeA; 
  // if (sizeA>=maxSize) {cout<<"Error: Too much data in A.data"; exit(0);};    

   // Get values for first array.        
   cout << "Reading data from A.data:\n";
   for (int index = 0; index < sizeA; index++) file_A>> A[index];   
   file_A.close(); 
   displayIntArray(A, sizeA);


    // Open B.data  
   ifstream file_B;
   file_B.open("B.data");
   if (!file_B) {cout<<"Error: opening B.data\n"; system("pause"); exit(0);};
   file_B>>sizeB; 
   //if (sizeB>=maxSize) {cout<<"Error: Too much data in B.data"; exit(0);};    

   // Get values for second array.
   cout << "Reading data from B.data:\n";
   for (int index = 0; index < sizeB; index++) file_B>> B[index];     
   file_B.close(); 
   displayIntArray(B, sizeB);

}


//******************************************************************************
// Definition of function findIntersection                   
// This functon accepts three arrays as arguments.           
// The first two arrays (first and second) are scanned, and all values appearing
// in both are stored in the third array (intersect).
// The number of values that appear in both arrays is returned.                               
//******************************************************************************
int findIntersection(int A[], int sizeA, int B[], int sizeB, int C[], int size){

   int  index3 = 0;    // Subscript variable for intersect array

   for (int index1 = 0; index1 < sizeA; index1++)
      for(int index2 = 0; index2 < sizeB; index2++)
         if (A[index1]==B[index2]) C[index3++]=A[index1];

   return index3;  // Return the number of intersecting values.
}

//******************************************************************************
// Definition of function displayIntArray              
// This function acepts two arguments: an array of ints 
// and an int. The second argument is the number of     
// valid elements contained in the array.               
// These values are displayed, if there are any.        
//******************************************************************************
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;
   }
}


//******************************************************************************
// Definition of function saveArray             
// This function save an array "a" of size "size" in the textFile C.data                        
//******************************************************************************
void saveArray(int a[], int size){

   // Write file C.data    
   ofstream file_C;
   file_C.open("C.data");
   if (!file_C) {cout<<"Error: opening C.data\n"; system("pause"); exit(0);}
   cout << "Saving Intersection(A,B) in C.data:\n";
   file_C<<size<<endl;     
   for (int index=0; index<size; index++) file_C<<a[index]<<endl;   
   file_C.close();     
}

Recommended Answers

All 4 Replies

I know i have to use pointers. I'm new to this and was to told to get rid of the SIZE on the code and replace with pointers.

Sorry to be bothersome but its due today by 11:00, thanks for your assistance

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



// Function Prototypes
void getArrays(int *&, int &, int [], int &);
int findIntersection(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, *setB, *setC, 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);

   // Find the intersection of the two sets A and B
   sizeC = findIntersection(setA, sizeA, setB, sizeB, setC);

   // Display setC (intersecting values of setA and setB) and save it in C.data
   saveArray(setC, sizeC);
   displayIntArray(setC, sizeC);

   system("pause");

   return 0;
}

//******************************************************************************
// Definition of function getArrays                      
// This function accepts two int arrays as arguments.    
// It prompts the user to enter 10 values for each array 
//******************************************************************************  
void getArrays(int *&a, int &sizeA, int *&b, int &sizeB){

int * setA;
int * setB;
int * setC;

setA = new (nothrow) int [5];
setB = new (nothrow) int [5];
setC = new (nothrow) int [5];






 //open A.data



 ifstream file_A;
   file_A.open("A.data");
   if (!file_A) {cout << "Error: opening A.data\n"; system("pause"); exit(0);};
   file_A>>sizeA;
  a=new int[sizeA];


   //if (sizeA>=maxSize) {cout<<"Error: Too much data in A.data"; exit(0);};

//Get values for first  array.

      cout<< "Reading data from A.data:\n";
      for (int index = 0; index < sizeA; index++) file_A>> A[index];
      file_A.close();
      displayIntArray(A, sizeA);


//Open B.data

      ifstream file_B;
      file_B.open("B.data");
      if (!file_B) {cout<<"Error: opening B.data\n"; system("pause"); exit (0);};
      file_B>>sizeB;
      b=new int[sizeB];

      //if (sizeB>=maxSize) {cout<<"Error: Too much data in B.data"; exit(0);};

//Get values for second array.
      cout<< "Reading data from B.data:\n";
      for (int index = 0; index < sizeB; index++) file_B>> B[index];
      file_B.close();
      displayIntArray(B, sizeB);

}

Okay this is what I have now. The program compiles but now the program shuts off when finding the intersection.

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



// Function Prototypes
void getArrays(int *&, int &, int *&, int &);
int findIntersection(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, *setB, *setC, 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);

   // Find the intersection of the two sets A and B
   sizeC = findIntersection(setA, sizeA, setB, sizeB, setC);

   // Display setC (intersecting values of setA and setB) and save it in C.data
   saveArray(setC, sizeC);
   displayIntArray(setC, sizeC);

   system("pause");

   delete [] setA;
   delete [] setB;
   delete [] setC;  



   return (0);
}

//******************************************************************************
// Definition of function getArrays                      
// This function accepts two int arrays as arguments.    
// It prompts the user to enter 10 values for each array 
//******************************************************************************  
void getArrays(int *&a, int &sizeA, int *&b, int &sizeB){

int * setA;
int * setB;
int * setC;

setA = new (nothrow) int [sizeA];
setB = new (nothrow) int [sizeB];
setC = new (nothrow) int [20];






 //open A.data



 ifstream file_A;
   file_A.open("A.data");
   if (!file_A) {cout << "Error: opening A.data\n"; system("pause"); exit(0);};
   file_A>>sizeA;
  a=new int[sizeA];




//Get values for first  array.

      cout<< "Reading data from A.data:\n";
      for (int index = 0; index < sizeA; index++) file_A>> a[index];
      file_A.close();
      displayIntArray(a, sizeA);


//Open B.data

      ifstream file_B;
      file_B.open("B.data");
      if (!file_B) {cout<<"Error: opening B.data\n"; system("pause"); exit (0);};
      file_B>>sizeB;
      b=new int[sizeB];



//Get values for second array.
      cout<< "Reading data from B.data:\n";
      for (int index = 0; index < sizeB; index++) file_B>> b[index];
      file_B.close();
      displayIntArray(b, sizeB);

}




//******************************************************************************
// Definition of function findIntersection                   
// This functon accepts three arrays as arguments.           
// The first two arrays (first and second) are scanned, and all values appearing
// in both are stored in the third array (intersect).
// The number of values that appear in both arrays is returned.                               
//******************************************************************************
int findIntersection(int *&a, int sizeA, int *&b, int sizeB, int *&c){



    int  index3 = 0;    // Subscript variable for intersect array

   for (int index1 = 0; index1 < sizeA; index1++)
      for(int index2 = 0; index2 < sizeB; index2++)
         if (a[index1] == b[index2]) c[index3++]=a[index1];


   return index3; //Return the number of intersecting values.

}

//******************************************************************************
// Definition of function displayIntArray              
// This function acepts two arguments: an array of ints 
// and an int. The second argument is the number of     
// valid elements contained in the array.               
// These values are displayed, if there are any.        
//******************************************************************************
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;
      }
}   


//******************************************************************************
// Definition of function saveArray             
// This function save an array "a" of size "size" in the textFile C.data                        
//******************************************************************************
void saveArray(int a[], int size){

//Write file C.data
ofstream file_C;
file_C.open("C.data");
if (!file_C) {cout<<"Error: Opening C.data\n"; system ("pause"); exit(0);}
cout << "Saving Intersection (A, B) in C.data:\n";
file_C<<size<<endl;
for (int index=0; index<size; index++) file_C<<a[index]<<endl;
file_C.close();

}
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.