Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a program where a user enters elements to 2 arrays, and it checks which elements are the same:
C++
#include <iostream>

using namespace std;

int main()
{
    int firstarray[5] = {0, 0, 0, 0, 0};
    int secondarray[5] = {0, 0, 0, 0, 0};

    cout << "enter 5 numbers seperated by a space e.g 3 4 5 6 7: ";
    cin >> firstarray[0] >> firstarray[1] >> firstarray[2] >> firstarray[3] >> firstarray[4];

    cout << "enter another 5 numbers seperated by a space: ";
    cin >>  secondarray[0] >> secondarray[1] >> secondarray[2] >> secondarray[3] >> secondarray[4];

    for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
    {
       if(firstarray[i] == secondarray[j])
       {
            cout << "the " << i << " element of both arrays are the same" << endl;
            continue;
       }
       else
       {
           cout << "the " << i << " element of both arrays are not same" << endl;
           continue;
       }
    }

    return 0;
}


when i run it and it i get this output:
enter 5 numbers seperated by a space e.g 3 4 5 6 7: 1 1 1 1 1
enter another 5 numbers seperated by a space: 1 1 1 1 1
the 0 element of both arrays are the same
the 0 element of both arrays are the same
the 0 element of both arrays are the same
the 0 element of both arrays are the same
the 0 element of both arrays are the same
the 1 element of both arrays are the same
the 1 element of both arrays are the same
the 1 element of both arrays are the same
the 1 element of both arrays are the same
the 1 element of both arrays are the same
the 2 element of both arrays are the same
the 2 element of both arrays are the same
the 2 element of both arrays are the same
the 2 element of both arrays are the same
the 2 element of both arrays are the same
the 3 element of both arrays are the same
the 3 element of both arrays are the same
the 3 element of both arrays are the same
the 3 element of both arrays are the same
the 3 element of both arrays are the same
the 4 element of both arrays are the same
the 4 element of both arrays are the same
the 4 element of both arrays are the same
the 4 element of both arrays are the same
the 4 element of both arrays are the same

However what I am only trying to print one line saying the element is the same, but it prints it 5 times. Is there a way I can print the line only 1 time?

What I have tried:

I have tried to write different code but it still doesnt work
Posted
Updated 25-May-21 1:55am
v2

If I understood your requirement correctly I would try this.

C++
<pre>#include <iostream>

using namespace std;

int main()
{
	int firstarray[5] = { 0, 0, 0, 0, 0 };
	int secondarray[5] = { 0, 0, 0, 0, 0 };

	cout << "enter 5 numbers seperated by a space e.g 3 4 5 6 7: ";
	cin >> firstarray[0] >> firstarray[1] >> firstarray[2] >> firstarray[3] >> firstarray[4];

	cout << "enter another 5 numbers seperated by a space: ";
	cin >> secondarray[0] >> secondarray[1] >> secondarray[2] >> secondarray[3] >> secondarray[4];

	for (int i = 0; i < 5; i++)
	{
		bool matched = false;

		for (int j = 0; j < 5; j++)
		{
			if (firstarray[i] == secondarray[j])
			{
				matched = true;
				break;
			}
		}

		if (matched)
		{
			cout << "the " << i << " element of both arrays are the same" << endl;
		}
		else
		{
			cout << "the " << i << " element of both arrays are not same" << endl;
		}
	}

	return 0;
}
 
Share this answer
 
v2
Comments
iwanttoaskquestions 23-May-21 6:12am    
thank you
Priyadharshini S 2021 29-May-21 6:42am    
...
Patrice T 23-May-21 7:47am    
+5
Quote:
but it prints it 5 times. Is there a way I can print the line only 1 time?

It is exactly what you told your code to do.
Try this:
C++
#include <iostream>

using namespace std;

int main()
{
    int firstarray[5] = {0, 0, 0, 0, 0};
    int secondarray[5] = {0, 0, 0, 0, 0};

    cout << "enter 5 numbers seperated by a space e.g 3 4 5 6 7: ";
    cin >> firstarray[0] >> firstarray[1] >> firstarray[2] >> firstarray[3] >> firstarray[4];

    cout << "enter another 5 numbers seperated by a space: ";
    cin >>  secondarray[0] >> secondarray[1] >> secondarray[2] >> secondarray[3] >> secondarray[4];

    for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
    {
       if(firstarray[i] == secondarray[j])
       {
            cout << "the " << i << " element of both arrays are the same" << endl;
            continue; // remove this
            break; // and add this to get out of inner loop when match is found.
       }
       else
       {
           // this part is misplaced because you lnow "not same" only after last check
           cout << "the " << i << " element of both arrays are not same" << endl;
           continue;
       }
    }

    return 0;
}

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
iwanttoaskquestions 23-May-21 6:12am    
thank you very much for the solution
The problem is that the test you perform does not correspond to the text you print. Going by the text, if I read "the 3 element of both arrays are the same", then my interpretation is that firstarray[3]==secondarray[3].

If that is what you want to test, then you must use a single loop, not a nested loop:
C++
for (int i = 0; i < 5; i++)
{
  if (firstarray[i] == secondarray[i])
  {
    cout << "The elements at position " << i " of the two arrays are equal" << endl;
  }
}


Otherwise, if you just want to find out whether secondarray contains element i of firstarray, then the first two solutions will give you the right result - but then the output text would be incorrect and should be fixed to something like:
C++
cout << "The element at position " << i << " of the first array is contained in the second array" << endl;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900