I'm working on some Uni tasks and I've made it 2 thirds of the way through the following but am stuck on the final part.
•Asks the user to enter 5 different numbers (between 1 and 20) and stores them in an array
•Asks the reader to enter another 5 numbers (between 1 and 20) into a second array
///////This is the one I'm stuck on , I've tried lots of different ways nut none seem to want to play ball!!
•Display both arrays on the screen as well as a list of the numbers that appear in both arrays (see example input and output below)
#include <iostream>
#include <Windows.h>
usingnamespace std;
int main()
constint NUM1 = 5;
constint NUM2 = 5;
int numbers1[NUM1];
int numbers2[NUM2];
int sum = 0;
int count = 0;
for (int firstArray = 0; firstArray < NUM1; firstArray++)
{
cout << (firstArray + 1) << ". " << "Enter a number between 1 and 20 ";
cin >> numbers1[firstArray];
while ((numbers1[firstArray] < 0) || (numbers1[firstArray] >20))
{
cout << "Please enter a number between 1 and 20 you IDIOT! ";
cin >> numbers1[firstArray];
}
}
for (int secondArray = 0; secondArray < NUM2; secondArray++)
{
cout << (secondArray + 1) << ". " << "Enter a number between 1 and 20 ";
cin >> numbers2[secondArray];
while ((numbers2[secondArray] < 0) || (numbers2[secondArray] >20))
{
cout << "Please enter a number between 1 and 20 you IDIOT! ";
cin >> numbers2[secondArray];
}
}
cout << "The numbers entered by you in the first array were:" ;
for (int i = 0; i < NUM1; i++)
{
cout << numbers1[i]<< ", ";
}
cout << "\n\nThe numbers entered by you in the second array were:";
for (int j = 0; j < NUM2; j++)
{
cout << numbers2[j]<< ", ";
}
system("pause");
return 0;
}
Thought would be hugely appreciated. I'm sure the solution is simple I just cant see the light through the trees right now :(
1. Enter a number between 1 and 20 2
2. Enter a number between 1 and 20 3
3. Enter a number between 1 and 20 4
4. Enter a number between 1 and 20 5
5. Enter a number between 1 and 20 6
1. Enter a number between 1 and 20 7
2. Enter a number between 1 and 20 8
3. Enter a number between 1 and 20 9
4. Enter a number between 1 and 20 3
5. Enter a number between 1 and 20 5
The numbers entered by you in the first array were:2, 3, 4, 5, 6,
The numbers entered by you in the second array were:7, 8, 9, 3, 5,
All I can see is you missed an opening brace after line 6
I understand the question but I don't know how to write the code. It's not really a lack of planning. More a case of purely and simply having hit a brick wall tbh. It's very rare that I ask for help, I feel a lot more satisfaction knowing I figured it out without having to ask but this time I really am stuck and I find arrays confusing at the best of times in any case.
Thanks for the advice though :)
What you said makes sense >> Does the number in the first slot of the first array match any of the numbers in array 2 memory slots? If so, remember those numbers so that I can output them when we have looped through all 5 numbers in the first array.
See I can say it, but I cant figure out how to code it.
int count1 = 0;
int count2 = 0;
int k = 0;
int l = 0;
......
......
for (; k < 6; k++)
{
for (; l < 6; l++)
if (numbers1[1,2,3,4,5] == numbers2[1,2,3,4,5])
{
count1++;
count2++;
}
}
I sort of knew this want going to work but it was one of my attempts.