0% found this document useful (0 votes)
43 views8 pages

Chapter 2

This document contains 7 code examples that demonstrate the use of dynamic arrays in C++. The examples cover: 1. Creating a one-dimensional dynamic array and prompting the user to enter elements. 2. Creating a one-dimensional array of 4 integers and searching for a user-input value. 3. Prompting the user for array size and elements, then searching for a value and outputting its index. 4. Determining the size of a dynamic array. 5. Initializing a 2D array and outputting its values. 6. Removing the first occurrence of a user-input value from an array. 7. Initializing a 3D array and

Uploaded by

Blas Abigail N.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Chapter 2

This document contains 7 code examples that demonstrate the use of dynamic arrays in C++. The examples cover: 1. Creating a one-dimensional dynamic array and prompting the user to enter elements. 2. Creating a one-dimensional array of 4 integers and searching for a user-input value. 3. Prompting the user for array size and elements, then searching for a value and outputting its index. 4. Determining the size of a dynamic array. 5. Initializing a 2D array and outputting its values. 6. Removing the first occurrence of a user-input value from an array. 7. Initializing a 3D array and

Uploaded by

Blas Abigail N.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Magahod, Jojo S.

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 the number of elements: ";


cin >> numElements;

int* elements = new int[numElements];

cout << "Enter " << numElements << " elements: ";
for (int i = 0; i < numElements; ++i) {
cin >> elements[i];
}

cout << "You entered: ";


for (int i = 0; i < numElements; ++i) {
cout << elements[i] << " ";
}

delete[] elements; // Don't forget to free the memory


return 0;
}

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;

cout << "Enter 4 integers: ";


for (int& element : arr) {
cin >> value;
element = value;
}

int searchValue = 0;
cout << "Enter an integer to search for: ";
cin >> searchValue;

bool found = false;


for (size_t i = 0; i < arr.size(); ++i) {
if (arr[i] == searchValue) {
found = true;
break;
}
}

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;
}

cout << "The final array is: ";


for (int num : arr) {
cout << num << " ";
}
cout << 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;

bool found = false;


for (size_t i = 0; i < arr.size(); ++i) {
if (arr[i] == value) {
found = true;
cout << "The integer " << value << " was found at location " << i << " in the array." << endl;
break;
}
}

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:

5. What is the output of this program?


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int** ptr2;
ptr2 = new int*[3];
for(int i=0; i<3; i++)
{
ptr2[i] = new int[4];
}

for(int i=0; i<3; i++)


{
for (int j=0; j<4; j++)
{
ptr2[i][j] = i * 4 + j;
}
}

cout << "\nTwo dimensions array of 3 by 4 integers:\n";


for(int i=0; i<3; i++)
{
for (int j=0; j<4; j++)
{
cout << setw(4) << right << ptr2[i][j] << "";
}
cout << endl;
}

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;

void remove_and_shift(vector<int>& arr, int value) {


int pos = -1;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] == value) {
pos = i;
break;
}
}
if (pos != -1) {
for (int i = pos; i < arr.size() - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.size() - 1] = 0;
}
}

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

You can use this formula: (i*3+j) *4.1+k*0.1

CODE :
#include <iostream>

int main() {
const int dim1 = 2, dim2 = 3, dim3 = 4;

double*** ptr3 = new double**[dim1];


for (int i = 0; i < dim1; ++i) {
ptr3[i] = new double*[dim2];
for (int j = 0; j < dim2; ++j) {
ptr3[i][j] = new double[dim3];
}
}

for (int i = 0; i < dim1; ++i) {


for (int j = 0; j < dim2; ++j) {
for (int k = 0; k < dim3; ++k) {
ptr3[i][j][k] = (i * 3 + j) * 4.1 + k * 0.1;
}
}
}

for (int i = 0; i < dim1; ++i) {


for (int j = 0; j < dim2; ++j) {
for (int k = 0; k < dim3; ++k) {
std::cout << "ptr3(" << i << "," << j << "," << k << ") = " << ptr3[i][j][k] << std::endl;
}
}
}

for (int i = 0; i < dim1; ++i) {


for (int j = 0; j < dim2; ++j) {
delete[] ptr3[i][j];
}
delete[] ptr3[i];
}
delete[] ptr3;

return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy