0% found this document useful (0 votes)
14 views7 pages

Codes Love

Uploaded by

pooja25mpawar
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)
14 views7 pages

Codes Love

Uploaded by

pooja25mpawar
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/ 7

Memory Representation of 1D and Multidimensional Arrays

1D Array:

#include <iostream>

using namespace std;

int main() {

int arr[5] = {10, 20, 30, 40, 50};

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

cout << "Element at index " << i << ": " << arr[i] << " | Memory address: " << &arr[i] << endl;

return 0;

+ 2D
#include <iostream>

using namespace std;

int main()

int arr[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

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

for (int j = 0; j < 3; j++) {

cout << "Element at (" << i << "," << j << "): " << arr[i][j] << " | Memory address: " << &arr[i][j]
<< endl;

}
return 0;

Operations in 1D Array
Traversal:
#include <iostream>

using namespace std;

int main() {

int arr[5] = {10, 20, 30, 40, 50};

// Traversal

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

cout << arr[i] << " ";

cout << endl;

return 0;

Insertion:

#include <iostream>

using namespace std;

int main() {

int arr[6] = {10, 20, 30, 40, 50}; // One extra space for insertion

int n = 5; // Current size of array

int position = 2; // Position where we want to insert

int element = 25;


for (int i = n; i > position; i--) {

arr[i] = arr[i-1];

arr[position] = element;

n++;

// Display the array after insertion

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

cout << arr[i] << " ";

cout << endl;

return 0;

Deletion:
#include <iostream>

using namespace std;

int main() {

int arr[5] = {10, 20, 30, 40, 50};

int n = 5; // Current size of array

int position = 2; // Position of the element to delete

for (int i = position; i < n-1; i++) {

arr[i] = arr[i+1];

n--;

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

cout << arr[i] << " ";

cout << endl;


return 0;

Searching in 1D Array
Linear search

#include <iostream>

using namespace std;

int linearSearch(int arr[], int n, int key) {

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

if (arr[i] == key) {

return i;

return -1; // Element not found

int main() {

int arr[5] = {10, 20, 30, 40, 50};

int key = 30;

int index = linearSearch(arr, 5, key);

if (index != -1) {

cout << "Element found at index: " << index << endl;

} else {

cout << "Element not found" << endl;

return 0;

Binary Search:
#include <iostream>

using namespace std;


int binarySearch(int arr[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == key) {

return mid;

if (arr[mid] < key) {

left = mid + 1;

} else {

right = mid - 1;

return -1; // Element not found

int main() {

int arr[5] = {10, 20, 30, 40, 50};

int key = 30;

int index = binarySearch(arr, 0, 4, key);

if (index != -1) {

cout << "Element found at index: " << index << endl;

} else {

cout << "Element not found" << endl;

return 0;

}
Sorting in 1D Array

Bubble Sort:

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n-1; i++) {

for (int j = 0; j < n-i-1; j++) {

if (arr[j] > arr[j+1]) {

swap(arr[j], arr[j+1]);

int main() {

int arr[5] = {50, 20, 40, 10, 30};

int n = 5;

bubbleSort(arr, n);

// Display the sorted array

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

cout << arr[i] << " ";

cout << endl;

return 0;

}
Queue

Front=start of elemnts In q

Rear=last element occupied

Exceptions:

1: no element in q: front =-1,Rear=-1.

2. only one element: F=1;R=1;

3. if R=MAXQ; and left side has 2 empty spaces; then R is countered to 0;

Overflow:

1.F=0;R=MAXQ

2.F=R+1

You might also like