Hi, I'm having trouble displaying the numbers in the file on the screen. The numbers are in the .data files but i cant get them to display.

``````//******************************************************************************
// Exercise: Homework 07 Arrays and pointers
// This program reads two sets of integers from the text files A.data and B.data.
// The program finds the intersection of the two sets (which is the
// set of numbers contained in both sets). The intersecting
// values are displayed and saved in C.data with the same file format
// tha A.data and B.data, is that to say, one interger in each line of the file
// being the first line the number of elements in the set.
//******************************************************************************

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

//******************************************************************************
// 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 maxSize){
ifstream fileA;
fileA.open ("A.data");
for(int index = 0; index < sizeA; index++)
fileA>>A[index];
fileA.close();
displayIntArray(A, sizeA);

ifstream fileB;
fileB.open ("B.data");

// file sizeA;
//if (sizeA> = max size)(cout<<"eretor";exit(0);
for(int index = 0; index < sizeB; index++)
fileB>>B[index];
fileB.close();
displayIntArray(B, sizeB);
//TODO

}

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

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

}
}}
//TODO

}

//******************************************************************************
// 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){
for (int index = 0; index < num; index++)
cout << a[index] <<" A";
cout << endl;

//TODO

}

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

ofstream fileC;
fileC.open("C.data");
cout<<"Saving Intersection(a,B) in c.data";
fileC<<size;
for(int index = 0;  index < size; index++)
fileC<<a[index];
fileC.close();
}``````

## All 9 Replies

Is the problem with reading them from the file? Or displaying them to the screen? I would separate this problem.

1st program) read the file and inspect its contents in memory

2nd program) hard code values and output them to the screen

By doing this you will isolate the problem and allow us to help you more easily.

David

Is the problem with reading them from the file? Or displaying them to the screen? I would separate this problem.

1st program) read the file and inspect its contents in memory

2nd program) hard code values and output them to the screen

By doing this you will isolate the problem and allow us to help you more easily.

David

1st) I found out yesterday it doesn't get the data from the a.data and b.data it see's the .data file but its not going into memory correctly. It writs the c.data file but it doesn't contain the intersecting value.

``````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];
index3++;

}
}}
//TODO``````

Your findIntersection needs a return value

``````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];
index3++;

}
}}

return (size);
}``````

Line 27 of your original code sets the variable sizeA to 0. Line 30 calls the function getarrays, passing a reference to sizeA. Nowhere in the getarrays function do I see any code that ever changes the value of sizeA.

So on return from getarrays, sizeA is still 0. I doubt that's what you have in mind.

The same reasoning applies to sizeB.

I suggest you break this program into smaller pieces and test each piece before you try to combine them.

Thanks gpjacks that helped me with the c part.

I can now get something to show but it is just a bunch of random numbers i cant get it to read the correct information from the getarrays.

``````//******************************************************************************
// Exercise: Homework 07 Arrays and pointers
// This program reads two sets of integers from the text files A.data and B.data.
// The program finds the intersection of the two sets (which is the
// set of numbers contained in both sets). The intersecting
// values are displayed and saved in C.data with the same file format
// tha A.data and B.data, is that to say, one interger in each line of the file
// being the first line the number of elements in the set.
//******************************************************************************

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

//******************************************************************************
// 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 maxSize){
ifstream fileA;
fileA.open ("A.data");
for(int index = 0; index < maxSize; index++)
fileA>>sizeA;
fileA.close();
displayIntArray(A, sizeA);

ifstream fileB;
fileB.open ("B.data");

for(int index = 0; index < maxSize; index++)
fileB>>sizeB;
fileB.close();
displayIntArray(B, sizeB);
//TODO

}

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

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

}
}}
return(size);
//TODO

}

//******************************************************************************
// 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){
for (int index = 0; index < num; index++)
cout << a[index] <<" A";
cout << endl;

//TODO

}

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

ofstream fileC;
fileC.open("C.data");
cout<<"Saving Intersection(a,B) in c.data";
fileC<<size;
for(int index = 0;  index < size; index++)
fileC<<a[index];
fileC.close();
}``````

Do you know whats in A.data and B.data? Maybe if you post that I can help you. There are free file viewers that can read .dat files.

A.data has numbers 1 through 14 and B.data has 1 through 5.

Thanks Every body i got it solved.

Thank you very much again. :)

does anyone know how to change this program into Dynamic? Any help would be appreciated

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.