Lab 01
Lab 01
Question01:
You are provided with a dataset representing temperatures at different points in a room over time. Write
a C++ program to read this dataset into a 3D array. Once the data is loaded, find and display the
maximum temperature recorded in the room along with its location (coordinates) within the array. write
code
Soution:
#include <iostream>
maxTemperature = roomTemperatures[z][y][x];
maxX = x;
maxY = y;
maxZ = z;
}
}
std::cout << "Maximum temperature recorded: " << maxTemperature << "°C\n";
std::cout << "Location (X, Y, Z): (" << maxX << ", " << maxY << ", " << maxZ << ")\n";
int main() {
double roomTemperatures[ROOM_HEIGHT][ROOM_LENGTH][ROOM_WIDTH] = {
{{22.5, 23.1, 21.8, 20.3, 24.0}, {25.2, 26.5, 27.8, 28.3, 25.9}, {23.4, 24.7, 26.2, 27.1, 28.6}, {22.8, 21.4,
20.9, 20.1, 19.7}},
{{20.7, 22.3, 23.9, 22.4, 21.1}, {24.6, 25.9, 27.3, 28.6, 25.9}, {22.8, 24.1, 25.6, 27.2, 28.7}, {21.2, 20.8,
20.3, 19.9, 18.7}},
{{18.5, 19.1, 17.8, 16.3, 19.0}, {21.2, 22.5, 23.8, 24.3, 21.9}, {19.4, 20.7, 22.2, 23.1, 24.6}, {18.8, 17.4,
16.9, 16.1, 15.7}}
};
findMaxTemperature(roomTemperatures);
return 0;
Question02:
Design a C++ program to manage inventory in a warehouse using a 3D array. Each element of the array
should represent the quantity of a specific item stored at a particular location (aisle, shelf, bin).
Implement functions to add, remove, and display the quantity of a specific item at a given location.
Soution:
#include <iostream>
void addItem(int inventory[MAX_AISLES][MAX_SHELVES][MAX_BINS], int aisle, int shelf, int bin, int
quantity) {
inventory[aisle][shelf][bin] += quantity;
void removeItem(int inventory[MAX_AISLES][MAX_SHELVES][MAX_BINS], int aisle, int shelf, int bin, int
quantity) {
inventory[aisle][shelf][bin] -= quantity;
if (inventory[aisle][shelf][bin] < 0) {
std::cout << "Quantity of item at location (" << aisle << ", " << shelf << ", " << bin << "): "
int main() {
// Example usage:
return 0;
Question03:
Develop a C++ program to simulate a 3D Tic-Tac-Toe game using a 3D array. The program should allow
two players to take turns marking cells in the 3D grid. Ensure that the game board is represented using a
3D array, and implement functions to check for winning combinations in all three dimensions.
Soution:
#include <iostream>
char board[BOARD_SIZE][BOARD_SIZE][BOARD_SIZE];
void initializeBoard() {
}
// Display the current state of the board
void displayBoard() {
if (k < BOARD_SIZE - 1) {
if (j < BOARD_SIZE - 1) {
// Check rows
return true;
}
}
// Check columns
return true;
// Check diagonals
return true;
return true;
// Check layers
return true;
}
// Check diagonals in 3D
return true;
return true;
return true;
return true;
return false;
bool checkTie() {
return false;
}
return true;
void playGame() {
initializeBoard();
while (true) {
std::cout << "Player " << currentPlayer << "'s turn. Enter row, column, and layer (1-3): ";
board[layer][row][col] = currentPlayer;
displayBoard();
if (checkWin(currentPlayer)) {
break;
}
// Check for a tie
if (checkTie()) {
break;
// Switch player
} else {
int main() {
playGame();
return 0;
Question04:
Write a C++ program that creates a 3D array of integers. Implement functions to populate the array with
random values, display the array contents, and find the sum of all elements in the array.
Soution:
#include <iostream>
srand(time(NULL));
int sum = 0;
sum += array[x][y][z];
return sum;
int main() {
int array[X_DIM][Y_DIM][Z_DIM];
populateArray(array);
displayArray(array);
std::cout << "Sum of all elements in the array: " << sum << std::endl;
return 0;
Question05:
Create a C++ program that generates a random 3D array of integers. Implement functions to calculate
and display basic statistics such as the sum, average, maximum, and minimum values of the elements in
the array.
Soution:
#include <iostream>
}
}
int sum = 0;
sum += array[i][j][k];
return sum;
return maxVal;
minVal = array[i][j][k];
return minVal;
int main() {
srand(time(NULL));
int randomArray[X_DIM][Y_DIM][Z_DIM];
fillRandomArray(randomArray, 1, 100); // Fill with random integers between 1 and 100
return 0;
}
Question 06:
write a c++ code that allows the user to slice a 4d array to exract 3d subarray based on specified
dimensions and indices. implement a function using pointers that takes the orignal 4d array user
defined slicing parameter and returns the sliced 3d array return complete runnig code
Solution:
#include <iostream>
cout << "Enter value of a[" << i << "][" << j << "][" << k << "][" << l << "]: ";
void sliceAndPrint(int* a, int dim1, int dim2, int dim3, int idx1, int idx2, int idx3) {
cout << "Sliced 3D subarray for dimensions [" << dim1 << "][" << dim2 << "][" << dim3 << "] and indices
[" << idx1 << "][" << idx2 << "][" << idx3 << "]:" << endl;
if (dim1 == 0) {
index1 = idx1;
} else {
index1 = i;
if (dim2 == 1) {
index2 = idx2;
} else {
index2 = j;
if (dim3 == 2) {
index3 = idx3;
} else {
index3 = k;
if (dim3 == 3) {
index4 = idx3;
} else {
index4 = k;
cout << *((a + index1 * 8 + index2 * 4 + index3 * 2) + index4) << " ";
}
int main() {
// Array declaration
int a[2][2][2][2];
inputArray(a);
cout << "Enter dimensions to slice (0, 1, or 2) and indices to slice (0 or 1) (dim1 dim2 dim3 idx1 idx2
idx3): ";
cin >> dim1 >> dim2 >> dim3 >> idx1 >> idx2 >> idx3;
return 0;
Question 07:
write a c++ program that searches for a specific element in a 4d array using pointer. if is found,
replace it with a user-specified value . ensure that the program handles multiple occurence of
the elemnt correctly
Solution:
#include <iostream>
if (temp == target) {
int main() {
int size;
cout << "Enter the size of each dimension of the 4D array: ";
cout << "Enter value of arr[" << i << "][" << j << "][" << k << "][" << l << "]: ";
}
cout << endl;
// Deallocate memory
delete[] arr[i][j][k];
delete[] arr[i][j];
delete[] arr[i];
delete[] arr;
return 0;