intmain() {
int arr[10] = {10,20,30,40,50};
int size =5;
int element, index;
cout <<"Array before editing: " << endl;
for (int i =0; i < size; i++) {
cout << arr[i] <<" ";
}
cout << endl;
cout <<"Enter element to insert: ";
cin >> element;
cout <<"Enter index to insert element at (0 - " << size <<"): ";
cin >> index;
if (index <0 || index > size) {
cout <<"Invalid index!" << endl;
}else {
for (int i = size; i > index; i--) {
arr[i] = arr[i -1];
}
arr[index] = element;
size++;
cout <<"Array after editing: " << endl;
for (int i =0; i < size; i++) {
cout << arr[i] <<" ";
}
cout << endl;
}
return0;
}
#include<iostream>
usingnamespace std;
intmain() {
int arr[10] = {10,20,30,40,50};
int size =5;
int element, index;
cout <<"Array before editing: " << endl;
for (int i =0; i < size; i++) {
cout << arr[i] <<" ";
}
cout << endl;
cout <<"Enter element to insert: ";
cin >> element;
cout <<"Enter index to insert element at (0 - " << size <<"): ";
cin >> index;
if (index <0 || index > size) {
cout <<"Invalid index!" << endl;
}else {
for (int i = index; i < size; i++) {
arr[i] = arr[i +1];
}
// Place the new element at the last position
arr[size -1] = element;
// size stays the same (we replaced an element)
// If you want to increase size, you can adjust accordingly
// Print array after editing
cout <<"Array after editing (Left Shift): " << endl;
for (int i =0; i < size; i++) {
cout << arr[i] <<" ";
}
cout << endl;
}
return0;
}
// Delete an element from the array
int size = 5;
int element = 0;
int index = 0;
int arr[size] = {10, 20, 30, 40, 50};
cout << "Array before editing: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Here, I ask the user which number they want to delete
cout << "Enter the element that you want to delete: ";
cin >> element;
// How will I delete it?
// To delete it, I need to access its index
// How do I reach its index?
// I will loop through all the elements of the array
for (int i = 0; i < size; i++) {
if (element == arr[i]) {
index = i; // index = 2
}
}
// Now I have the element, how do I delete it???
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
cout << "Array after editing: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 3, 6, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int element;
bool found = false;
int index = -1;
cout << "Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Enter element to search: ";
cin >> element;
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
index = i;
found = true;
break;
}
}
if (found) {
cout << "Found at index: " << index << endl;
} else {
cout << "Not found" << endl;
}
return 0;
}
Write a program that takes the elements of a 3×3 two-dimensional array from the user and then prints them on the screen.
int main() {
int arr[3][3];
cout << "Enter elements of 3x3 array: " << endl;
for(int i = 0; i<3 ; i++){
for (int j = 0 ; j<3 ; j++){
cout << "Element at position [" << i << "][" << j << "]: ";
cin >> arr[i][j];
}
}
cout << "\\n your arr :\\n";
for(int i = 0; i<3 ; i++){
for (int j = 0 ; j<3 ; j++){
cout << arr[i][j] << " ";
}
}
return 0;
}