├── .gitattributes ├── .github └── FUNDING.yml ├── LICENSE ├── README.md ├── code.cpp └── images ├── f.png └── p.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://www.cyfylabs.com'] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 harismuneer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🌌 Mapping 3D Array to 1D Array 2 | 3 | 4 | views 5 | [![Open Source Love svg1](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](#) 6 | [![GitHub Forks](https://img.shields.io/github/forks/harismuneer/Mapping-3D-Array-to-1D-Array.svg?style=social&label=Fork&maxAge=2592000)](https://www.github.com/harismuneer/Mapping-3D-Array-to-1D-Array/fork) 7 | [![GitHub Issues](https://img.shields.io/github/issues/harismuneer/Mapping-3D-Array-to-1D-Array.svg?style=flat&label=Issues&maxAge=2592000)](https://www.github.com/harismuneer/Mapping-3D-Array-to-1D-Array/issues) 8 | [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat&label=Contributions&colorA=red&colorB=black )](#) 9 | 10 | 11 | 12 | ## Mapping Formula 13 | If you have a 3D array **Original[HEIGHT, WIDTH, DEPTH]** then you could turn it into **Flat[HEIGHT * WIDTH * DEPTH]** 1D array by 14 | 15 | ```Flat[x + HEIGHT * (y + WIDTH * z)] = Original[x, y, z], assuming Original[HEIGHT,WIDTH,DEPTH]``` 16 | 17 | To study more about this, refer to following links: 18 | * [Row and Column Major Order](https://en.wikipedia.org/wiki/Row-_and_column-major_order) 19 | * [Flatten a 3D array to 1D array](https://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array) 20 | 21 | ## Problem 22 | In order to better understand the array mapping, following real life problem is solved: 23 | 24 | ![problem](../master/images/p.png) 25 | 26 | ## Functions Implemented 27 | ![problem](../master/images/f.png) 28 | 29 | 30 |
31 | 32 | ## Author 33 | You can get in touch with me on my LinkedIn Profile: [![LinkedIn Link](https://img.shields.io/badge/Connect-harismuneer-blue.svg?logo=linkedin&longCache=true&style=social&label=Follow)](https://www.linkedin.com/in/harismuneer) 34 | 35 | You can also follow my GitHub Profile to stay updated about my latest projects: [![GitHub Follow](https://img.shields.io/badge/Connect-harismuneer-blue.svg?logo=Github&longCache=true&style=social&label=Follow)](https://github.com/harismuneer) 36 | 37 | If you liked the repo then kindly support it by giving it a star ⭐ and share in your circles so more people can benefit from the effort. 38 | 39 | ## Contributions Welcome 40 | [![GitHub Issues](https://img.shields.io/github/issues/harismuneer/Mapping-3D-Array-to-1D-Array.svg?style=flat&label=Issues&maxAge=2592000)](https://www.github.com/harismuneer/Mapping-3D-Array-to-1D-Array/issues) 41 | 42 | If you find any bugs, have suggestions, or face issues: 43 | 44 | - Open an Issue in the Issues Tab to discuss them. 45 | - Submit a Pull Request to propose fixes or improvements. 46 | - Review Pull Requests from other contributors to help maintain the project's quality and progress. 47 | 48 | This project thrives on community collaboration! Members are encouraged to take the initiative, support one another, and actively engage in all aspects of the project. Whether it’s debugging, fixing issues, or brainstorming new ideas, your contributions are what keep this project moving forward. 49 | 50 | With modern AI tools like ChatGPT, solving challenges and contributing effectively is easier than ever. Let’s work together to make this project the best it can be! 🚀 51 | 52 | ## License 53 | [![MIT](https://img.shields.io/cocoapods/l/AFNetworking.svg?style=style&label=License&maxAge=2592000)](../master/LICENSE) 54 | 55 | Copyright (c) 2018-present, harismuneer 56 | 57 | 58 | 59 |
60 | 61 |

Waving hand 62 | Hey there, I'm Haris Muneer 👨🏻‍💻 63 |

64 | 65 | 66 | Total Github Stars 67 | Total Github Followers 68 | 69 |
70 | 71 | - 🛠️ Product Builder: Agile Product Manager with 5+ years of hands-on experience delivering SaaS solutions across sales, recruiting, AI, social media, and public sector domains. Background in Computer Science, with a proven track record of scaling products from inception to $XXM+ ARR, launching 3 top-ranking tools on Product Hunt, and developing solutions adopted by 250+ B2B clients in 40+ countries. 72 | 73 | - 🌟 Open Source Advocate: Passionate about making technology accessible, I’ve developed and open-sourced several software projects for web, mobile, desktop, and AI on my GitHub profile. These projects have been used by thousands of learners worldwide to enhance their skills and knowledge. 74 | 75 | - 📫 How to Reach Me: To learn more about my skills and work, visit my LinkedIn profile. For collaboration or inquiries, feel free to reach out via email. 76 | 77 |
78 | 79 |

🤝 Follow my journey

80 |

81 | 82 | 83 | 84 | 85 |

86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | class building 9 | { 10 | 11 | private: 12 | 13 | int* area; 14 | int totalFloors, totalApartments, totalRooms; 15 | 16 | 17 | 18 | //////////////////////////// Utility Functions ////////////////////////////// 19 | 20 | 21 | 22 | ////Gets the room areas from the user 23 | void getAreasOfRooms() 24 | { 25 | int roomPosition = 0; 26 | 27 | cout << "\nStoring the areas of the rooms in column major order."; 28 | cout << "\nInput areas for the rooms of the building :\n\n"; 29 | //Column Major Order Offset Calculation 30 | for (int k = 0; k < totalRooms; ++k) 31 | { 32 | for (int j = 0; j < totalApartments; ++j) 33 | { 34 | for (int i = 0; i < totalFloors; ++i) 35 | { 36 | //Calculating the position where the room area is to be placed 37 | //in 1D Array (viz. Area) 38 | roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + i; 39 | 40 | cout << "Enter the area of the room \""<> area[roomPosition]; 43 | } 44 | } 45 | } 46 | 47 | cout << "\nThe areas have been entered successfully.\n"; 48 | } 49 | 50 | 51 | 52 | //Copies the room areas from the old array to the newly created array 53 | //which has changed (new) dimensions 54 | void copyAreasToNewArray(int* temp, int nFloors, int nApartments) //Here 'n' means "new" e.g "nFloors means New Floors" 55 | { 56 | int roomPosition, nRoomPosition = 0; 57 | 58 | int min_floors = min (totalFloors,nFloors); 59 | int min_apartments = min (totalApartments,nApartments); 60 | 61 | //Column Major Order Offset Calculation 62 | 63 | for (int k = 0; k < totalRooms; ++k) //totalRooms in an appartment never changes. 64 | { 65 | for (int j = 0; j < min_apartments; ++j) 66 | { 67 | for (int i = 0; i < min_floors; ++i) 68 | { 69 | //Calculating Room Position for Old Array 70 | roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + i; 71 | 72 | //Calculating Room Position for New Array with Changed Dimensions 73 | nRoomPosition = (k * nApartments * nFloors) + (j * nFloors) + i; 74 | 75 | temp[nRoomPosition] = area[roomPosition]; 76 | } 77 | } 78 | } 79 | } 80 | 81 | 82 | 83 | public: 84 | 85 | //Overloaded Constructor for initializing the building dynamically 86 | //with given dimensions 87 | building (int floors,int appartments,int rooms) 88 | { 89 | totalFloors = floors; 90 | totalApartments = appartments; 91 | totalRooms = rooms; 92 | 93 | area = new int [totalFloors * totalApartments * totalRooms]; 94 | 95 | getAreasOfRooms (); 96 | } 97 | 98 | 99 | 100 | //This functions adds a new floor in the building 101 | void addFloor (int **new_floor) 102 | { 103 | int* temp = new int [ (totalFloors + 1) * totalApartments * totalRooms ]; 104 | 105 | //Copying the room areas of the old array to the 106 | //new array 107 | copyAreasToNewArray (temp, totalFloors+1, totalApartments); 108 | 109 | //Updating the Total Floors 110 | totalFloors = totalFloors + 1; 111 | 112 | int roomPosition = 0; 113 | 114 | //Copying the room areas of the newly made Floor to the 115 | //new array 116 | for (int k = 0; k < totalRooms; ++k) 117 | { 118 | for (int j = 0; j < totalApartments; ++j) 119 | { 120 | //Calculating the positions for the newly made Rooms 121 | roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + (totalFloors-1); 122 | 123 | temp [roomPosition] = new_floor[j][k]; 124 | } 125 | } 126 | 127 | cout <<"\nThe floor has been added successfully."; 128 | 129 | delete[] area; 130 | area = temp; 131 | } 132 | 133 | 134 | 135 | //This function removes the top floor from the building 136 | bool removeTopFloor () 137 | { 138 | if (totalFloors != 0) 139 | { 140 | int* temp = new int [ (totalFloors-1) * totalApartments * totalRooms]; 141 | 142 | copyAreasToNewArray (temp, totalFloors-1, totalApartments); 143 | 144 | //Updating the Total Floors 145 | totalFloors = totalFloors - 1; 146 | 147 | cout << "\nThe top floor \""< 0) && (currApartment <= totalApartments && currApartment > 0) && (currRoom <= totalRooms && currRoom > 0)) 209 | room_Area = area [ ((currRoom-1) * totalApartments * totalFloors) + ((currApartment-1) * totalFloors) + (currFloor-1)]; 210 | 211 | else 212 | cout << "\nThe input is not valid."; 213 | } 214 | 215 | else 216 | { 217 | cout << "\nThe building has no floor."; 218 | } 219 | 220 | return room_Area; 221 | } 222 | 223 | 224 | //Sets the specified room's area 225 | void setRoomArea(int currFloor, int currApartment, int currRoom, int roomArea) 226 | { 227 | if (totalFloors != 0) 228 | { 229 | if ( (currFloor <= totalFloors && currFloor > 0) && (currApartment <= totalApartments && currApartment > 0) && (currRoom <= totalRooms && currRoom > 0)) 230 | { 231 | area [ ((currRoom-1) * totalApartments * totalFloors) + ((currApartment-1) * totalFloors) + (currFloor-1)] = roomArea; 232 | cout << "\nThe area has been successfully set."; 233 | } 234 | else 235 | cout << "\nThe input is not valid."; 236 | } 237 | 238 | else 239 | cout << "\nThe building has no floor."; 240 | } 241 | 242 | 243 | //Outputs the area of rooms of one floor in tabular format 244 | void outputEntireBuilding() 245 | { 246 | if(totalFloors != 0) 247 | { 248 | for (int i = 0; i < totalFloors; ++i) 249 | { 250 | cout <<"\nRoom Areas of Floor \""<> f; 313 | cout << "Apartments : "; 314 | cin >> a; 315 | cout << "Rooms : "; 316 | cin >> r; 317 | 318 | building b1 (f,a,r); 319 | 320 | int userInput = 0; 321 | int currRoom = 0,currFloor = 0,currApartment = 0; 322 | 323 | int ** new_floor = nullptr; 324 | int ** new_apartment = nullptr; 325 | 326 | while (userInput != -1) 327 | { 328 | b1.printFunctions(); 329 | cin >> userInput; 330 | 331 | //////////////////////////////////Add Floor////////////////////////////////////// 332 | if(userInput == 1) 333 | { 334 | //Creating the new floor 335 | 336 | new_floor = new int* [a]; 337 | 338 | for (int i = 0; i < a; ++i) 339 | new_floor[i] = new int [r]; 340 | 341 | //Initializng the new floor 342 | 343 | cout << "\nInput areas for the newly created floor : \n\n"; 344 | 345 | for (int i = 0; i < r; ++i) 346 | { 347 | for (int j = 0; j < a; ++j) 348 | { 349 | cout << "Enter the area of the room \""<> new_floor[j][i]; 351 | } 352 | } 353 | 354 | //Adding the new floor 355 | b1.addFloor(new_floor); 356 | f = f + 1; 357 | 358 | 359 | if (new_floor) 360 | { 361 | //De allocating the new floor array because we don't need that anymore 362 | for (int i = 0; i < a; ++i) 363 | { 364 | delete [] new_floor[i]; 365 | } 366 | 367 | delete[] new_floor; 368 | new_floor = nullptr; 369 | } 370 | } 371 | 372 | //////////////////////////////////Remove Top Floor////////////////////////////////////// 373 | else if (userInput == 2) 374 | { 375 | if (b1.removeTopFloor()) 376 | f = f - 1; 377 | } 378 | 379 | //////////////////////////////////Expand Each Floor////////////////////////////////////// 380 | else if (userInput == 3) 381 | { 382 | new_apartment = new int* [f]; 383 | 384 | for (int i = 0; i < f; ++i) 385 | new_apartment[i] = new int [r]; 386 | 387 | //Initialing the new appartment 388 | for (int i = 0; i < r; ++i) 389 | { 390 | for (int j = 0; j < f; ++j) 391 | { 392 | cout << "Enter the area of the room \""<> new_apartment[j][i]; 394 | } 395 | } 396 | 397 | if( b1.expandEachFloor(new_apartment) ) 398 | a = a + 1; 399 | 400 | if (new_apartment) 401 | { 402 | for (int i = 0; i < f; ++i) 403 | delete [] new_apartment[i]; 404 | 405 | delete[] new_apartment; 406 | new_apartment = nullptr; 407 | } 408 | } 409 | 410 | 411 | //////////////////////////////////Get Room Area////////////////////////////////////// 412 | else if (userInput == 4) 413 | { 414 | cout << "\nWhich room area do you want to read ? \n"; 415 | cout << "Floor Number : "; 416 | cin >> currFloor; 417 | cout << "Apartment Number : "; 418 | cin >> currApartment; 419 | cout << "Room Number : "; 420 | cin >> currRoom; 421 | 422 | if (b1.getRoomArea(currFloor,currApartment,currRoom) != -1) 423 | cout << "\n\nThe area of room \""<> currArea; 435 | cout << "Floor Number : "; 436 | cin >> currFloor; 437 | cout << "Apartment Number : "; 438 | cin >> currApartment; 439 | cout << "Room Number : "; 440 | cin >> currRoom; 441 | 442 | b1.setRoomArea (currFloor,currApartment,currRoom,currArea); 443 | } 444 | 445 | 446 | //////////////////////////////////Output Entire Building////////////////////////////////////// 447 | else if (userInput == 6) 448 | b1.outputEntireBuilding(); 449 | 450 | else if( userInput == -1) 451 | cout << "\nExiting !!!"; 452 | 453 | else 454 | cout << "---Invalid Entry--- \nPlease choose from the Options below.\n "; 455 | } 456 | } 457 | 458 | 459 | 460 | 461 | ////////////////////Main///////////////////////// 462 | int main() 463 | { 464 | Driver(); 465 | system("PAUSE"); 466 | return 0; 467 | } -------------------------------------------------------------------------------- /images/f.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harismuneer/Mapping-3D-Array-to-1D-Array/a9827635d86ac18b0b8ec2fbe5484945e8bbc635/images/f.png -------------------------------------------------------------------------------- /images/p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harismuneer/Mapping-3D-Array-to-1D-Array/a9827635d86ac18b0b8ec2fbe5484945e8bbc635/images/p.png --------------------------------------------------------------------------------