[go: up one dir, main page]

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

Array Programs

Uploaded by

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

Array Programs

Uploaded by

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

1.

​ Displaying Array Elements

#include <iostream>
using namespace std;

int main() {

int numbers[5] = {7, 5, 6, 12, 35};


cout << "\nThe numbers are: ";

// Printing array elements


// using traditional for loop
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}

return 0;
}

2.​ Take Inputs from User and Store Them in an Array

#include <iostream>
using namespace std;

int main() {

int numbers[5];

cout << "Enter 5 numbers: " << endl;

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}
3.​ C++ Program: Insert at Beginning, End, and Any Position

#include <iostream>
using namespace std;

int main() {
int arr[100], n, element, position;

// Input size of array


cout << "Enter number of elements: ";
cin >> n;

// Input elements
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

// Display original array


cout << "Original array:\n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

// Insert at the beginning


cout << "\nInsert at beginning:\nEnter element: ";
cin >> element;
for (int i = n; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = element;
n++;

cout << "Array after inserting at beginning:\n";


for (int i = 0; i < n; i++) cout << arr[i] << " ";

// Insert at the end


cout << "\n\nInsert at end:\nEnter element: ";
cin >> element;
arr[n] = element;
n++;
cout << "Array after inserting at end:\n";
for (int i = 0; i < n; i++) cout << arr[i] << " ";

// Insert at a specific position


cout << "\n\nInsert at specific position:\nEnter element: ";
cin >> element;
cout << "Enter position (0 to " << n << "): ";
cin >> position;

if (position < 0 || position > n) {


cout << "Invalid position!";
} else {
for (int i = n; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
n++;

cout << "Array after inserting at position " << position << ":\n";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
}

return 0;
}

4.​ C++ program to insert an element into an array at a given position:

#include <iostream>
using namespace std;

int main() {
int arr[100], n, element, position;

// Input size of array


cout << "Enter number of elements: ";
cin >> n;

// Input elements
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

// Input element and position to insert


cout << "Enter element to insert: ";
cin >> element;
cout << "Enter position (0 to " << n << "): ";
cin >> position;

// Shift elements to the right


for (int i = n; i > position; i--) {
arr[i] = arr[i - 1];
}

// Insert element
arr[position] = element;
n++; // Increase array size

// Print updated array


cout << "Array after insertion:\n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}

return 0;
}

5.​ C++ Program to Delete Element from Array

#include <iostream>
using namespace std;

int main() {
int arr[100], n, value, position, i;

// Input array size


cout << "Enter number of elements: ";
cin >> n;

// Input elements
cout << "Enter " << n << " elements:\n";
for (i = 0; i < n; i++) {
cin >> arr[i];
}

// Display original array


cout << "\nOriginal array:\n";
for (i = 0; i < n; i++) cout << arr[i] << " ";
// Delete by position
cout << "\n\nDelete by position:\nEnter position (0 to " << n-1 << "): ";
cin >> position;

if (position < 0 || position >= n) {


cout << "Invalid position!\n";
} else {
for (i = position; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;

cout << "Array after deleting at position " << position << ":\n";
for (i = 0; i < n; i++) cout << arr[i] << " ";
}

// Delete by value
cout << "\n\nDelete by value:\nEnter value to delete: ";
cin >> value;

bool found = false;


for (i = 0; i < n; i++) {
if (arr[i] == value) {
found = true;
for (int j = i; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
n--;
break;
}
}

if (found) {
cout << "Array after deleting value " << value << ":\n";
for (i = 0; i < n; i++) cout << arr[i] << " ";
} else {
cout << "Value not found in array.";
}

return 0;
}
6.​ C++ Example: 2D Array

#include <iostream>
using namespace std;

int main() {
int rows, cols;

cout << "Enter number of rows: ";


cin >> rows;
cout << "Enter number of columns: ";
cin >> cols;

int arr[10][10]; // Max size 10x10

// Input elements
cout << "Enter elements of the 2D array:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> arr[i][j];
}
}

// Display elements
cout << "\n2D Array is:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl; // Move to next row
}

return 0;
}

7.​ C++ Example: 3×3 Matrix

#include <iostream>
using namespace std;

int main() {
int matrix[3][3];
// Input elements for 3x3 matrix
cout << "Enter elements for 3x3 matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}

// Display matrix
cout << "\n3x3 Matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

return 0;
}

You might also like