I have the code bellow. Now I'm ask to display the number of elements in the arrays read from files. Files contain 200 random numbers between them. The problem is if I pres enter at the last element...


I have the code bellow.


Now I'm ask to display the number of elements in the arrays read from files. Files contain 200 random numbers between them. The problem is if I pres enter at the last element in the text file the count will return 201 elements when in reality there are 200 and the empty space at the end. if I put -1 in the count then the problem is if a file has 200 numbers and no extra blank it.


2. I need to Find out for example how many times number 9 apears in file/array 1 and how many times in file/array2



Code:


#include
#include
using namespace std;


class Multiplicity { //Class


private:


    //Inititate Values
    const static int size = 300;
    ifstream file1, file2;
    int array1[size], array2[size];


    void getData() {
        file1.open("comFile111.txt"); //Open File 1


        if (!file1.is_open()) { //Check is file was found


            cout < "file="" 1="" not="" found!"=""><>
            exit(1);
        }


        file2.open("comFile222.txt"); //Open File 2


        if (!file2.is_open()) { //Check is file was found
            cout < "file="" 2="" not="" found!"=""><>
            exit(2);
        }
    }


    void computeData() {
        //Get the size of the first array by populating the first array using file1.
        int size1 = readFile(array1, file1);


        //Get the size of the second array by populating the second array using file2.
        int size2 = readFile(array2, file2);


        file1.close(); //Closing file used for array1
        file2.close(); //Closing file used for array2


        //Assigning pointers
        int* ptr1 = array1; //Pointer for Array 1
        int* ptr2 = array2; //Pointer for Array 2


        compareSize(size1, size2);//Compare size of arrays if they are not the same a messaje will be display and program will end


        bubbleSort(ptr1, size1); //Bubble sort Object call - Fully functionanl
        cout < "the="" number="" of="" elements="" inside="" array="" 1:="" "="">< sizeof(array1)/sizeof(array1[0])="">< "\n"=""><>


        bubbleSort(ptr2, size2); //Bubble sort Object call - Fully functionanl
        cout < "the="" number="" of="" elements="" inside="" array="" 2:="" "="">< countelements(ptr2,="" size2)="">< "\n"=""><>

        compareElements(ptr1, ptr2, size1); //Call for object to compare elements in arrays usign pointers, and display the results of the comparasion
    }


    //Read files
    int readFile(int* array, ifstream& fin)
    {
        int count = 0;


        while (!fin.eof()) { //read file until end of file and passes values


            fin >> *(array + count);
            count++;
        }

        return count-1;
    }


    //Swap function used for bubble sort
    void swap(int* xp, int* yp) {


        int temp = *xp;
        *xp = *yp;
        *yp = temp;
    }


    // Bubble Sort Functional - First thought if quick sort was not posible
    void bubbleSort(int* array, int size) {


        for (int i = 0; i < size="" -="" 1;="" i++)="">
            for (int j = 0; j < size="" -="" i="" -="" 1;="" j++)="">
                if (*(array + j) > *(array + j + 1)) {
                    swap((array + j), (array + j + 1));
                }
            }
        }
    }


    //Compare Size of Arrays Object
    void compareSize(int size1, int size2) {


        if (size1 != size2) {
            cout < "the="" amount="" of="" elements="" in="" the="" arrays="" are="" not="" the="" same,="" multiplicity="" is="" not="" posible."=""><>
            exit(3);
        }
    }


    //Compare Elements of Arrays Object
    void compareElements(int* array1, int* array2, int s) {


        for (int i = 0; i < s;="" i++)="">
            if (*(array1 + i) != *(array2 + i)) {
                cout < "the="" elements="" in="" the="" arrays="" are="" not="" the="" same="" and="" multiplicity="" is="" not="" posible."=""><>
                return;
            }
        }
        cout < "the="" elements="" in="" the="" arrays="" are="" the="" same="" and="" multiplicity="" is="" posible."=""><>
        return;
    }


    //Print Array function for testing
    void printArray(int* arr, int size) {
        //int count = 0;
        for (int i = 0; i < size;="" i++)="">
            cout < *(arr="" +i="" )=""><>
            //count++;
        }
        //cout < "size="" of="" array:="" "="">< count=""><>
    }


    //Count Array Elements
    int countElements(int* arr, int size) {
        int count = 0;
        for (int i = 0; i < size;="" i++)="">
            if (*(arr + 1) != * (arr + size)) {
                count++;
            }
        }
        return count;
    }


public:


    //Constructor
    Multiplicity() {


        getData(); // Calls for get data object


        computeData(); //Calls for Compute data object for results
    }
};


int main()
{
    Multiplicity M; //Creats an instance of Class


    return 0;
}


Jun 02, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here