Array-Single Dimension [Set 1]
#include <iostream> using namespace std; int main() { int n, sum = 0; cout << "Enter the number of elements in the array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } double average = sum / static_cast<double>(n); cout << "Sum: " << sum << endl; cout << "Average: " << average << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of elements in the array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } // Swap first and last elements int temp = arr[0]; arr[0] = arr[n - 1]; arr[n - 1] = temp; cout << "Array after swapping first and last elements: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of elements in the array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } cout << "Reversed array: "; for (int i = n - 1; i >= 0; i--) { cout << arr[i] << " "; } cout << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of elements in the array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } int largest = arr[0], smallest = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > largest) largest = arr[i]; if (arr[i] < smallest) smallest = arr[i]; } cout << "Largest: " << largest << ", Smallest: " << smallest << endl; return 0; }
#include <iostream> using namespace std; void acceptArray(int arr[], int &n) { cout << "Enter the number of elements: "; cin >> n; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) cin >> arr[i]; } void displayArray(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i], j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int minIdx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIdx]) minIdx = j; } swap(arr[minIdx], arr[i]); } } 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[100], n, choice; do { cout << "\nMenu:\n"; cout << "1. Accept elements of an array\n2. Display elements\n3. Insertion Sort\n4. Selection Sort\n5. Bubble Sort\n6. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: acceptArray(arr, n); break; case 2: displayArray(arr, n); break; case 3: insertionSort(arr, n); cout << "Array sorted using Insertion Sort.\n"; break; case 4: selectionSort(arr, n); cout << "Array sorted using Selection Sort.\n"; break; case 5: bubbleSort(arr, n); cout << "Array sorted using Bubble Sort.\n"; break; case 6: cout << "Exiting program.\n"; break; default: cout << "Invalid choice!\n"; } } while (choice != 6); return 0; }
#include <iostream> using namespace std; int searchValue(int P[], int n, int VAL) { for (int i = 0; i < n; i++) { if (P[i] == VAL) return 1; } return 0; } int main() { int n, VAL; cout << "Enter the number of elements in the array: "; cin >> n; int P[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) cin >> P[i]; cout << "Enter the value to search: "; cin >> VAL; if (searchValue(P, n, VAL)) cout << "Value found in the array.\n"; else cout << "Value not found in the array.\n"; return 0; }
#include <iostream> using namespace std; int binarySearch(int AR[], int n, int num) { int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (AR[mid] == num) return 1; else if (AR[mid] < num) left = mid + 1; else right = mid - 1; } return 0; } int main() { int n, num; cout << "Enter the number of elements in the array: "; cin >> n; int AR[n]; cout << "Enter the elements of the array in ascending order: "; for (int i = 0; i < n; i++) cin >> AR[i]; cout << "Enter the number to search: "; cin >> num; if (binarySearch(AR, n, num)) cout << "Number found in the array.\n"; else cout << "Number not found in the array.\n"; return 0; }
#include <iostream> #include <algorithm> using namespace std; void mergeArrays(int A[], int B[], int C[], int M, int N) { int i = 0, j = N - 1, k = 0; while (i < M && j >= 0) { if (A[i] < B[j]) C[k++] = A[i++]; else C[k++] = B[j--]; } while (i < M) C[k++] = A[i++]; while (j >= 0) C[k++] = B[j--]; } int main() { int M, N; cout << "Enter the size of array A: "; cin >> M; int A[M]; cout << "Enter elements of array A in ascending order: "; for (int i = 0; i < M; i++) cin >> A[i]; cout << "Enter the size of array B: "; cin >> N; int B[N]; cout << "Enter elements of array B in descending order: "; for (int i = 0; i < N; i++) cin >> B[i]; int C[M + N]; mergeArrays(A, B, C, M, N); cout << "Merged array in ascending order: "; for (int i = 0; i < M + N; i++) cout << C[i] << " "; cout << endl; return 0; }
#include <iostream> using namespace std; void mergeDescending(int X[], int Y[], int Z[], int M, int N) { int i = 0, j = 0, k = 0; while (i < M && j < N) { if (X[i] > Y[j]) Z[k++] = X[i++]; else Z[k++] = Y[j++]; } while (i < M) Z[k++] = X[i++]; while (j < N) Z[k++] = Y[j++]; } int main() { int M, N; cout << "Enter the size of array X: "; cin >> M; int X[M]; cout << "Enter elements of array X in descending order: "; for (int i = 0; i < M; i++) cin >> X[i]; cout << "Enter the size of array Y: "; cin >> N; int Y[N]; cout << "Enter elements of array Y in descending order: "; for (int i = 0; i < N; i++) cin >> Y[i]; int Z[M + N]; mergeDescending(X, Y, Z, M, N); cout << "Merged array in descending order: "; for (int i = 0; i < M + N; i++) cout << Z[i] << " "; cout << endl; return 0; }
#include <iostream> #include <vector> using namespace std; void MIX(int A[], int B[], int C[], int M, int N) { vector<int> even, odd; for (int i = 0; i < M; i++) { if (A[i] % 2 == 0) even.push_back(A[i]); else odd.insert(odd.begin(), A[i]); } for (int i = 0; i < N; i++) { if (B[i] % 2 == 0) even.push_back(B[i]); else odd.insert(odd.begin(), B[i]); } int k = 0; for (int val : even) C[k++] = val; for (int val : odd) C[k++] = val; } int main() { int M, N; cout << "Enter the size of array A: "; cin >> M; int A[M]; cout << "Enter elements of array A: "; for (int i = 0; i < M; i++) cin >> A[i]; cout << "Enter the size of array B: "; cin >> N; int B[N]; cout << "Enter elements of array B: "; for (int i = 0; i < N; i++) cin >> B[i]; int C[M + N]; MIX(A, B, C, M, N); cout << "Resultant array C: "; for (int i = 0; i < M + N; i++) cout << C[i] << " "; cout << endl; return 0; }