Chapter 2
Chapter 2
DSALGLABLC
BSINFOTECH 2A MS. MACASIL
CHAPTER 2
1. Create a program that will accept the number of elements preferred by the user, and the elements.
Display the entered elements.
CODE:
#include <iostream>using namespace std;
int main() {
int numElements;
cout << "Enter " << numElements << " elements: ";
for (int i = 0; i < numElements; ++i) {
cin >> elements[i];
}
OUTPUT:
2. Create a program that will create a one dimensional dynamic array of 4 integers.
CODE:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr(4);
int value = 0;
int searchValue = 0;
cout << "Enter an integer to search for: ";
cin >> searchValue;
if (found) {
arr.erase(arr.begin() + arr.size() - 1);
for (size_t i = arr.size() - 1; i > 0; --i) {
arr[i] = arr[i - 1];
}
arr[0] = 0;
} else {
cout << "Integer not found in the array." << endl;
}
return 0;
}
OUTPUT:
This program follows these steps:
1. It creates a one-dimensional dynamic array arr of 4 integers.
2. It prompts the user to enter 4 integers, storing each value in the array.
3. It then prompts the user to enter an integer to search for in the array.
4. If the search value is found, the program removes the last element of the array and shifts each
remaining element to the left. The first element of the array is set to 0.
5. Finally, the program prints the final array.
If the search value is not found in the array, the program will simply display a message stating that the
value was not found.
2. Create a program that will require the user to input integers, ask for an integer, and then display the
location of that integer in the array.
CODE:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter " << n << " integers: ";
for (int& element : arr) {
cin >> element;
}
int value;
cout << "Enter an integer to search for: ";
cin >> value;
if (!found) {
cout << "The integer " << value << " was not found in the array." << endl;
}
return 0;
}
OUTPUT:
4. Create a program that will determine the size of a dynamic array.
CODE:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter " << n << " integers: ";
for (int& element : arr) {
cin >> element;
}
cout << "The size of the array is: " << arr.size() << endl;
return 0;
}
OUTPUT:
int main()
{
int** ptr2;
ptr2 = new int*[3];
for(int i=0; i<3; i++)
{
ptr2[i] = new int[4];
}
return 0;
}
OUTPUT:
6. Write a program that asks the user to type the number of integers of an array and an integer value V.
The program must search if the value V exists in the array and must remove the first occurrence of V,
shifting each following element left and adding a zero at the end of the array. The program must then
write the final array.
CODE:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, v;
cout << "Enter the number of integers: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the integers: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
cout << "Enter the value to remove: ";
cin >> v;
remove_and_shift(arr, v);
cout << "The final array is: ";
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT:
7. Make a program that uses a multi-dimensional dynamic array of 2 by 3 by 4 and creates this output:
ptr3(0,0,0)= 0
ptr3(0.0.1) = 0.1
ptr3(0,0,2) = 0.2
ptr3(0.0.3) = 0.3
ptr3(0,1,0) = 4.1
ptr3(0.1.1)= 4.2
ptr3(0.1.2) = 4.3
ptr3(0.1,3) = 4.4
ptr3(0,2,0) = 8.2
ptr3(0,2.1) = 8.3
ptr3(0.2.2) = 8.4
ptr3(0,2,3)= 8.5
ptr3(1,0,0)= 12.3
ptr3(1,0,1) = 12.4
ptr3(1.0.2) = 12:5
ptr3(1.0.3)= 12.6
ptr3(1,1,0)=-16.4
ptr3(1,1,1) 16.5
ptr3(1.1.2) 16.6
ptr301,1,3) 16.7
ptr3(1,2,0) = 20.5
ptr3(1,2.1) = 20.6
ptr3(1.2.2) = 20.7
ptr3(1,2,3) = 20.8
CODE :
#include <iostream>
int main() {
const int dim1 = 2, dim2 = 3, dim3 = 4;
return 0;
}
OUTPUT: