i have an assignment can somebody help me,

Assignment Statement: Comparing two arrays for equality or matching indexes

You are required to write a program which will take input from user in two integer arrays. The program should compare both arrays for checking if both arrays are totally identical (Exactly same). If not, then program should determine the matching indexes of both arrays. Matching indexes means that first element of array1 should be equal to first element of array2 and so on.

Dani AI

Generated

A concise, robust approach is to (1) read both arrays into std::vector<int>, (2) check sizes and element-by-element equality for the “identical” test, and (3) otherwise iterate to collect the positions where elements match. Common errors in the original attempt — nonstandard headers, out-of-bounds loops, and treating C++ arrays as 1-based — are fixed below. This complements ’s comments about indexing and ’s suggestion to post working code; it also addresses the issues in ’s snippet.

#include <iostream>
#include <vector>
#include <algorithm>

bool identical(const std::vector<int>& a, const std::vector<int>& b) {
    if (a.size() != b.size()) return false;
    for (size_t i = 0; i < a.size(); ++i) if (a[i] != b[i]) return false;
    return true;
}

std::vector<size_t> matching_indices(const std::vector<int>& a, const std::vector<int>& b) {
    std::vector<size_t> matches;
    size_t n = std::min(a.size(), b.size());
    for (size_t i = 0; i < n; ++i) if (a[i] == b[i]) matches.push_back(i);
    return matches;
}

// Example main: read N, then two lists of N ints; adapt if lengths differ.
int main() {
    size_t n;
    if (!(std::cin >> n)) return 0;
    std::vector<int> a(n), b(n);
    for (size_t i = 0; i < n; ++i) std::cin >> a[i];
    for (size_t i = 0; i < n; ++i) std::cin >> b[i];

    if (identical(a,b)) std::cout << "Arrays are identical\n";
    else {
        auto m = matching_indices(a,b);
        if (m.empty()) std::cout << "No matching positions\n";
        else {
            std::cout << "Matching indices (0-based): ";
            for (size_t i : m) std::cout << i << ' ';
            std::cout << '\n';
        }
    }
    return 0;
}

Notes and troubleshooting: prefer std::vector over raw fixed arrays to avoid magic sizes; use size_t for indices; validate input with std::cin; when presenting results to humans, convert 0-based indices to 1-based if desired. This solution is minimal, compiles with a standard C++ compiler, and directly addresses the assignment while fixing the earlier compile/run and logic issues noted by and others.

Recommended Answers

All 6 Replies

Try to write the code out and see if you get the answer. This is simpler than you think. If you really can't get it, post your code (make sure you use code blocks) and we will give you some code/advice.

-Sam

If this is the biggest problem of your life, you are truly blessed.

commented: LOL! :) +4

haha, yea I agree. "please help me i having biggest problem of my life;" is pretty dramatic.

Not until you help yourself. Show us your attempt first.

i attempt this code but it doesn't run, plz help me
#include<iostream.h>
#include<conio.h>
void match(int a[10],int b[10]);
void main()
{
int x[10],y[10],m,n;
clrscr();
cout<<"Enter elements of 1st array:"<<endl;
for(m=1;m<11;m++)
{
cout<<"element "<<m<<" is =";
cin>>x[m];
}
cout<<"Enter elements of 2nd array:"<<endl;
for(n=1;n<11;n++)
{
cout<<"element "<<n<<" is =";
cin>>y[n];
}
match (x,y);
getch();
}
void match(int a[10], int b[10])
{
int i,j;
{
cout<<"array1\tarray2"<<endl;
for(j=1;j<2;j++)
{
for(i=1;i<11;i++)
if(a[j]==b[j])
cout<<a[j]<<"\t"<<b[j]<<endl;
else
{
for(i=1;i<11;i++)
if(a[j]!=b[j])
cout<<a[j]<<"\t"<<b[j]<<endl;
else
cout<<"mathch";
}
}
}
}

Please repost using code tags.

[code]

// code here

[/code]

  1. Use iostream, not iostream.h
  2. conio.h is not needed. All it does is complicate things for the rest of us.
  3. Ditto clrscr(). It adds nothing to the program, plus we can't compile it.
  4. Change "void main" to "int main". Lots of threads as to why.
  5. Having an array size of 10 and a loop that goes to 11 is just begging for a seg fault.
  6. Array indexes start at 0, not 1.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.