## CRS representation of 4 dimensional array

We take input of four dimensional array then apply CRS(Compressed  row sparse ), then use some search, save print operation

Full code with explanation
``` #include #include #include #include #include #include ```

``` using namespace std; using std::cout; using std::cin; class CRS{ private: // Counter Variable int Totnon0, Rownon0; // Array 4 Dimensions int DN; int d1, d2, d3, d4; // Declare ArrayMD int ****arrayMD; // Declare Array RO KO JO CO VL int *arrayRO; int *arrayKO; int *arrayJO; int *arrayCO; int *arrayVL; public: // Array Iterators int i, j, k, l; CRS(){ Totnon0 = 0; } void allocateArray(); void assignArrayMD(); void showArrayMD(); void applyCRS(); void showCRSarrays(); void searchLoc(); void searchItem(); void saveArray(); float getFileSize(const char *filename); int printMenu(); void deallocateArray(); }; //********************************************// //******* Allocate 4D and other Arrays *******// //********************************************// void CRS::allocateArray() { DN = 4; cout<<"No. of Dimension [--- 2 <= Dimension <=4 ---]:" << DN <>DN; // Input for length of each dimension for(i = 1; i <= DN; i++) { cout<<"Length of Dimension"<< i <<" (d"<< i <>d1; if(i == 2) cin>>d2; if(i == 3) cin>>d3; if(i == 4) cin>>d4; } // Allocate Array CO & JO & KO & VL arrayRO = new int[d1+1]; arrayKO = new int[d1*d2*d3*d4]; arrayJO = new int[d1*d2*d3*d4]; arrayCO = new int[d1*d2*d3*d4]; arrayVL = new int[d1*d2*d3*d4]; //******* Allocate 4D Array *******// arrayMD = new int***[d1]; for(i = 0; i < d1; i++) { arrayMD[i] = new int**[d2]; for(j = 0; j < d2; j++) { arrayMD[i][j] = new int*[d3]; for(k = 0; k < d3; k++) { arrayMD[i][j][k] = new int[d4]; } } } } //************************************************// //************ Assign Array Elements ************// //************************************************// void CRS::assignArrayMD() { srand((unsigned)time(0)); int lowest = 0, highest = 10; int range=(highest-lowest); for(i = 0; i < d1; i++) { for(j = 0; j < d2; j++) { for(k = 0; k < d3; k++) { for(l = 0; l < d4; l++) { //arrayMD[i][j][k][l] = rand() % 2; arrayMD[i][j][k][l] = lowest+int(range*rand()/(RAND_MAX + 1.0)); } } } //cout << endl << endl; } } //************************************************// //************ Display Array Elements ************// //************************************************// void CRS::showArrayMD() { for(i = 0; i < d1; i++) { //cout << "[" << i << "]" <<endl; for(j = 0; j < d2; j++) { cout << endl << "[" << i << "]" << "[" << j << "]" << endl; for(k = 0; k < d3; k++) { cout << endl; for(l = 0; l < d4; l++) { cout << '\t' << arrayMD[i][j][k][l]; } } } cout << endl << endl; } } //************************************************// //************** Apply CRS Method ***************// //***********************************************// void CRS::applyCRS() { Totnon0 = 0; arrayRO[0] = 1; for(i = 0; i 0) { arrayRO[i] = arrayRO[i - 1] + Rownon0; } Rownon0 = 0; for(j = 0; j < d2; j++) { for(k = 0; k < d3; k++) { for(l = 0; l < d4; l++) { if(arrayMD[i][j][k][l] != 0) { arrayVL[Totnon0] = arrayMD[i][j][k][l]; arrayKO[Totnon0] = j; arrayJO[Totnon0] = k; arrayCO[Totnon0] = l; Totnon0++; Rownon0++; } } } } } arrayRO[d1] = arrayRO[d1 - 1] + Rownon0; } //************************************************// //************** Show Arrays of CRS *************// //************************************************// void CRS::showCRSarrays() { for(i = 0; i < d1+1; i++) { cout<< "arrayRO[" << i <<"]:" << arrayRO[i] << endl ; } cout<< endl << endl; for(i = 0; i < Totnon0; i++) { cout<< "arrayVL[" << i <<"]:" << arrayVL[i] << " " ; cout<< "arrayKO[" << i <<"]:" << arrayKO[i] << " " ; cout<< "arrayJO[" << i <<"]:" << arrayJO[i] << " " ; cout<< "arrayCO[" << i <<"]:" << arrayCO[i] << endl ; cout<< endl << endl; } } //************************************************// //************* Search by given Item ************// //************************************************// void CRS::searchLoc() { int Searchval, RO = 0, Row, count = 0; cout<< "---------- Search by given Item ------------"<< endl <<endl; cout<>Searchval; for(i = 0; i < Totnon0; i++) { if(arrayVL[i] == Searchval) { count++; RO = 0; for(j = 0; j i) { Row = j; break; } } cout<<count<<". value = "<< arrayVL[i]<<'\t'<<"ArrayMD["<< Row <<"]["<< arrayCO[i] <<"]"<< endl << endl; } } if(count == 0) { cout << Searchval << " - is not found" << endl; } } //************************************************// //********** Search by given Location ***********// //************************************************// void CRS::searchItem() { int dim1, dim2, dim3, dim4, found = 0; // RO, CO cout<< "------------ Search by given Location ------------"<< endl << endl; for(i = 1; i <= DN; i++) { cout<<"Dimension"<< i <<" (dim"<< i <>dim1; if(i == 2) cin>>dim2; if(i == 3) cin>>dim3; if(i == 4) cin>>dim4; } int nonZero = arrayRO[dim1 + 1] - arrayRO[dim1]; cout << "No. of Non Zero Elements in Row "<< dim1 <<": "<<nonZero << endl; for(i = (arrayRO[dim1] - 1); i < (arrayRO[dim1] - 1 + nonZero); i++) { if(arrayKO[i] == dim2) { //cout<< arrayKO[i]<<endl; for(j = i; j < (arrayRO[dim1] - 1 + nonZero); j++) { if(arrayKO[j] == dim2 && arrayJO[j] == dim3) { //cout<< arrayJO[j]<<endl; for(k = j; k < (arrayRO[dim1] - 1 + nonZero); k++) { if(arrayKO[k] == dim2 && arrayJO[k] == dim3 && arrayCO[k] == dim4) { //cout<< arrayKO[k]<<endl; //found = 1; cout<< "Value is: " << arrayVL[k] << endl << endl; //break; goto END; } } } } } } cout<< "Value is: 0" << endl; END : ;//exit Loop } //************************************************// //********* Calculating File Size **************// //************************************************// float CRS::getFileSize(const char *filename) { float result; FILE *fh = fopen(filename, "r"); fseek(fh, 0, SEEK_END); result = ftell(fh); fclose(fh); return result; } //************************************************// //***************** saveArray ******************// //************************************************// void CRS::saveArray() { ofstream outputMD("MDArray.txt"); ofstream outputCRS("CRSArray.txt"); for(i = 0; i < d1; i++) { for(j = 0; j < d2; j++) { for(k = 0; k < d3; k++) { for(l = 0; l < d4; l++) { outputMD << arrayMD[i][j][k][l]; } } } } for(i = 0; i < Totnon0; i++) { outputCRS<<arrayVL[i]; } } //************************************************// //***************** printMenu ******************// //************************************************// int CRS::printMenu() { //system ("cls"); cout << "-------------------------------------------------" << endl; cout << " Compress Row Storage(CRS) Implementation" << endl; cout << "-------------------------------------------------" << endl; cout << "\t\t 1. Create a Multi Dimensional Array" << endl; cout << "\t\t 2. Show All Records of the MD Array" << endl; cout << "\t\t 3. Apply CRS on the MD Array" << endl; cout << "\t\t 4. Show All Records of the CRS Arrays" << endl; cout << "\t\t 5. Search a Location of a given Record" << endl; cout << "\t\t 6. Search a Record for a given Location" << endl; cout << "\t\t 7. Save Arrays" << endl; cout << "\t\t 8. Exit" << endl; int choice; cout <> choice; cout.flush(); return choice; } //************************************************// //************* Deallocate 4D array ************// //************************************************// void CRS::deallocateArray() { for(i = 0; i < d1; i++) { for(j = 0; j < d2; j++) { for(k = 0; k < d3; k++) { delete[] arrayMD[i][j][k]; } } delete[] arrayMD[i]; } delete[] arrayMD; // Deallocate CO JO KO and VL array delete[] arrayVL; delete[] arrayCO; delete[] arrayJO; delete[] arrayKO; delete[] arrayRO; } void credits() { std::cout < 0) { crs.deallocateArray(); crsapp = 0; cout << "deallocated" << endl; } crs.allocateArray(); crs.assignArrayMD(); allocate = 1; flag = 1; break; } case 2: // Show the MD the array { if(flag == 0) { cout << "Array is not Available" << endl; } else crs.showArrayMD(); getch(); //ch = std::getchar(); break; } case 3: // Apply CRS { if(flag == 0) { cout << "Array is not Available and CRS can't be Applied" << endl; } else if(flag != 0 && crsapp == 0) { crs.applyCRS(); crsapp =1; } else { cout << "CRS is already Applied" << endl; } break; } case 4: // Show CRS data { if(crsapp == 1) { crs.showCRSarrays(); getch(); } else cout << "CRS is not even applied" << endl; break; } case 5: //Search by a Item in the CRS Arrays { if(crsapp == 1) { crs.searchLoc(); getch(); } else cout << "CRS is not even applied" << endl; break; } case 6: //Search by a Loc in the CRS arrays { if(crsapp == 1) { crs.searchItem(); getch(); } else cout << "CRS is not even applied" << endl; break; } case 7: // Save Array { crs.saveArray(); MDsize = crs.getFileSize("MDArray.txt"); cout << "MDsize: " << MDsize << " bytes" << endl; CRSsize = crs.getFileSize("CRSArray.txt"); cout << "CRSsize: " << CRSsize << " bytes" << endl; lambda = CRSsize / MDsize; cout << "Compression ratio= " << lambda < 0) { crs.deallocateArray(); } credits(); getch(); return 0; } ```

hope this helps