Comparing entered array's elements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
int main()
{
int array1[5];
for (int i=0; i<5; i=i+1)
{
cout << "Enter element" <<"[" << i << "]" <<endl;
cin >> array1[i];
}
for (int i=0; i<5; i=i+1)
{
cout << "Element" <<"[" << i << "]=" << array1[i] <<endl;
}
int array2[5];
for (int i=0; i<5; i=i+1)
{
cout << "Enter element" <<"[" << i << "]" <<endl;
cin >> array2[i];
}
for (int i=0; i<5; i=i+1)
{
cout << "Element" <<"[" << i << "]=" << array2[i] <<endl;
}
return 0;
}
|
This is my program right now: how could i compare arrays elements?
it's something like {if (array1[i] < array2[i]) cout ...
You just need a if...else statement inside a for loop that checks to see whether or not two elements are equal.
In pseudocode:
1 2 3 4 5
|
for i less than five
if element1 equals element2
do something
else
do something
|
Before your return you could do something like:
1 2 3 4 5 6 7 8 9 10 11 12
|
for( int i = 0; i < 5; i++ )
{
if(array1[i] == array2[i])
{
std::cout << "Both array elements at position [" << i << "] = " << array1[i] << std::endl;
}
else
{
std::cout << "Array elements at position [" << i
<< "] do not match. array1[i] = " << array1[i] << ", array2[i] = " << array2[i] << std::endl;
}
}
|
Topic archived. No new replies allowed.