/* Program for swapping two arrays using function. */ #include<iostream> using namespace std; void swapArray(int[], int [], int); int main() { int size, array1[50], array2[50]; //INPUT STATEMENTS //For Inputting amount of numbers of First Array do { cout << "How many numbers u want to Enter in both Array(Max 50)?\t"; cin >> size; if (size < 0 || size > 50) cout << "Invalid Input.......\nEnter number again\n"; } while (size < 0 || size > 50); //For inputting Numbers in Array cout << "\nInput for Array-1:\n"; for (int i = 0; i < size; i++) { cout << "Enetr number at [" << i << "] index?\t"; cin >> array1[i]; } //for formatting output for (int i = 0; i < 80; i++) cout << "-"; //For inputting Numbers 2nd in Array cout << "\nInput for Array-2:\n"; for (int i = 0; i < size; i++) { cout << "Enetr number at [" << i << "] index?\t"; cin >> array2[i]; } swapArray(array1, array2, size); //for formatting output for (int i = 0; i < 80; i++) cout << "="; //For Displaying Result cout << "Array-1 is:\n\t"; for (int i = 0; i < size; i++) { cout << array1[i]; if (i != size - 1) cout << ", "; } cout << "\nArray-2 is:\n\t"; for (int i = 0; i < size; i++) { cout << array2[i]; if (i != size - 1) cout << ", "; } cout << endl; return 0; } //Function for inputing numbers void swapArray(int array1[], int array2[], int size) { for (int i = 0; i < size; i++) { array1[i] = array1[i] + array2[i]; array2[i] = array1[i] - array2[i]; array1[i] = array1[i] - array2[i]; } }
No comments:
Post a Comment