/*
Program for Comparing 2 Integer Arrays of specified size and returns:
0: if both arrays are equal
1: if first array is greater
-1: if second array is greater
Criteria:
Equal: if sizes and contents of both arrays are equal.
Greater: if the size of first array is greater than that of second array or if any number
in the first array is
greater than the number at the corresponding index of the second array.
Smaller: if the size of first array is less than that of second array or if any number in
the first array is less
than the number at the corresponding index of the second array.
using function.
*/
#include<iostream>
using namespace std;
int compareArrays(int[], int, int[], int, int &); //function Declaration/Prototype
int main()
{
int size1, size2, num1[50], num2[50], result, index;
//INPUT STATEMENTS
//For Inputting amount of numbers of First Array
do
{
cout << "How many numbers u want to Enter in 1st Array(Max 50)?\t";
cin >> size1;
if (size1 < 0 || size1 > 50)
cout << "Invalid Input.......\nEnter number again\n";
} while (size1 < 0 || size1 > 50);
//For inputting Numbers in Array
for (int i = 0; i < size1; i++)
{
cout << "Enetr number at [" << i << "] index?\t";
cin >> num1[i];
}
//for formatting output
for (int i = 0; i < 80; i++)
cout << "=";
//For Inputting amount of numbers of Second Array
do
{
cout << "How many numbers u want to Enter in 2nd Array(Max 50)?\t";
cin >> size2;
if (size2 < 0 || size2 > 50)
cout << "Invalid Input.......\nEnter number again\n";
} while (size2 < 0 || size2 > 50);
//For inputting Numbers in Array
for (int i = 0; i < size2; i++)
{
cout << "Enetr number at [" << i << "] index?\t";
cin >> num2[i];
}
//for formatting output
for (int i = 0; i < 80; i++)
cout << "=";
result = compareArrays(num1, size1, num2, size2, index);
//Displays Result
if (result == 0)
cout << "\nBoth Arrays are Equal.";
else if (result == 1)
{
if (size1 == size2)
cout << "\nArray1 is Greater at INDEX [" << index << "].";
else
cout << "\nArray1 is Greater in SIZE.";
}
else
{
if (size1 == size2)
cout << "\nArray2 is Greater at INDEX [" << index << "].";
else
cout << "\nArray2 is Greater in SIZE.";
}
cout << endl;
return 0;
}
//function Definition
int compareArrays(int num1[], int size1, int num2[], int size2, int &index)
{
//int result;
if (size1 < size2) //for comparing array sizes
return -1;
else if (size1 > size2) //for comparing array sizes
return 1;
else //for comparing array elements
{
index = 0;
for (int i = 0; i < size1; i++)
{
if (num1[i] < num2[i])
{
index = i;
return -1;
}
else if (num1[i] > num2[i])
{
index = i;
return 1;
}
}
return 0;
}
}
OUTPUT
No comments:
Post a Comment