0

I need my code to output the common numbers from two arrays but I can't seem to find the issues in my code. Here are some more words so I can fill the quota for stack's post requirement.

Here's the code.


#include <iostream>

using namespace std;

int main()
{
    //Variables
    int list1[5], list2[5];

    cout << "Enter numbers for List No.1" << endl;
    for (int i = 1; i <= 5; i++) {
        cout << "Enter number " << i << ":";
        cin >> list1[i];
    }
    cout << "Enter numbers for List No.2" << endl;
    for (int i = 1; i <= 5; i++) {
        cout << "Enter number " << i << ":";
        cin >> list2[i];
    }
    {
        cout << "common elements" << endl;
        for (int i = 0; list1; i < i++) {
            for (int i = 0; list2; i < i++)

                if (list1[i] == list2[i]) {
                    cout << " " << list1[i];
                }
        }
    }

    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    You are using `i` for both array loops. – Devolus Apr 11 '21 at 16:15
  • the counter for the second for loop is the same as the counter for the first loop```int i```. first fix that – newbie Apr 11 '21 at 16:15
  • your nested for loops are wrong on many ways, you want the middle segment to be `i < 5` and the last segment to be `i++`. you also need 2 different names for the index, `i` and `j` probably. you are missing a `{` at the last for loop. – Stack Danny Apr 11 '21 at 16:16
  • 1
    `for(int i = 1; i <= 5; i ++) {` this is wrong, array indexing is 0 based – Borgleader Apr 11 '21 at 16:19

2 Answers2

3

You should use different counter for each loop and also your loop has wrong parameters. Do it like:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {

        if (list1[i] == list2[j]){
            cout<<" "<<list1[i];
        }
    }
}

Also for initial values start from zero index([0]) as zero index is the first element for each array:

for (int i = 0; i < 5; i++) {
        cout << "Enter number " << i << ":";
        cin >> list1[i];
}

Another way to do this is by using std::array. It acts very similar to a normal array but it is much better to work with.

for example if you want a char array of length of 10 you can simply write:

std::array<char, 10> My_happy_array.

First please read this to understand why you should not using namespace std. Second consider reading about Range-based for loop

Third, here is the modified version of your program which uses std::array instead of a normal array:

#include <iostream>
#include <array>

int main() {

    std::array<int,5> list1;
    std::array<int,5> list2;

    std::cout << "Enter numbers for List No.1" << std::endl;
    for (int i = 0; i < list1.size(); ++i) {
        std::cout << "Enter number " << (i+1) << ":";
        std::cin >> list1[i];
    }

    std::cout << "Enter numbers for List No.2" << std::endl;
    for (int i = 0; i < list2.size(); ++i) {
        std::cout << "Enter number " << (i+1) << ":";
        std::cin >> list2[i];
    }

    for(auto i : list1){
        for(auto j : list2){
            if(i==j){
                std::cout << " " << i;
            }
        }
    }


    return 0;
}
newbie
  • 415
  • 3
  • 11
0

Here's fixed code

#include <iostream>
    
using namespace std;

int main()
{
    //Variables
    int list1[5] ,list2[5];
    
    cout << "Enter numbers for List No.1" << endl;
    for(int i = 0; i < 5; i ++) 
    {
        cout << "Enter number " << i << ":";
        cin >> list1[i];
    }
    cout << "Enter numbers for List No.2" << endl;
    for(int i = 0; i < 5; i ++) 
    {
        cout << "Enter number " << i << ":";
        cin >> list2[i];
    }
    cout<<"common elements"<<endl;
    for (int i = 0;i<5; i++) 
    {
        for (int j = 0;j<5; j++)
        {
        if (list1[i] == list2[j])
            {
                cout<<" "<<list1[i];
            }
        }
    }
    
    
return 0;
}

mistakes in code:

you used for loops wrongly whole the time.

  1. array of 5, stores values in index (0,1,2,3,4) not (1,2,3,4,5), so always start loop from 0 and ends before max index (for this case '4' ie. use 'i<5')

  2. while using for loop inside other for loop never use same iterable variable like you used i in both loops use i and j different for each loop, take a look in my code.. ask me if you get any difficulties.