# Thread: CS201 Introduction to Programming Fall 2010 second assignment solution

1. ## CS201 Introduction to Programming Fall 2010 second assignment solution

Problem 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.

Example of two identical (Exactly same) arrays:

Array1 Array2
1 1
2 2
3 3
5 5
7 7
9 9
11 11
13 13
15 15
17 17

Example of two arrays with some matching elements:

Array1 Array2
1 7
2 9
3 3
5 4
7 6
9 8
11 11
13 15
8 8
17 17

Here elements on index 3, 7, 9, and 10 of both arrays are same.

Detailed Description:

The program should take 10 integer inputs from user in arrray1 and array2.

• After taking inputs in both arrays, you should pass both arrays to a function named match.
• If both arrays are exactly same, the program should display a message “Both arrays are identical”.
• If both arrays are not exactly same, then program should match the indexes which have same elements in both arrays and display the matching indexes.
• If there is no element matching in both arrays, the program should display a message
• “There is no matching element in both arrays”.
• Implementation regarding comparison of arrays and displaying message should be inside the function match.

Sample Output for two exactly same arrays

Please enter 10 integers for array1:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19

Please enter 10 integers for array2:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19

Both arrays are identical

Sample Output for arrays with some matching indexes

Please enter 10 integers for array1:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19
Please enter 10 integers for array2:
Enter element 1 : 2
Enter element 2 : 4
Enter element 3 : 5
Enter element 4 : 6
Enter element 5 : 9
Enter element 6 : 12
Enter element 7 : 14
Enter element 8 : 16
Enter element 9 : 17
Enter element 10 : 20

Both arrays have same elements on
Index 3
Index 5
Index 9

Solution will be update soon.

2. #include<iostream.h>
#include<stdio.h>
#include<conio.h>

int main()
{

clrscr();
int IndexA[10] = {0,0,0,0,0,0,0,0,0,0};
int IndexB[10] = {0,0,0,0,0,0,0,0,0,0};

cout<<"You Are Requested To Enter Values in Arrays "<<endl;
cout<<"you should enter values between 1 & 20 "<<endl;
cout<<"Enter Value In First Array"<<endl;
for(int x=0 ; x<10 ; x++)
{
cout<<"Enter Value at - "<<x+1<<" - location - ";
cin>>IndexA[x];
if(IndexA[x] < 0 )
{
cout<<"please eneter the value between 1 & 20";
cin>>IndexA[x];
}
else if(IndexA[x] >20)
{
cout<<"please eneter the value between 1 & 20";
cin>>IndexA[x];
}
}
clrscr();
cout<<"enter Value In Second Array"<<endl;
for(x=0 ; x<10 ; x++)
{
cout<<"Enter Value at - "<<x<<" - location - ";
cin>>IndexB[x];
if(IndexB[x] < 0 )
{
cout<<"please eneter the value between 1 & 20";
cin>>IndexB[x];
}
else if(IndexB[x] >20)
{
cout<<"please eneter the value between 1 & 20";
cin>>IndexB[x];
}
}
clrscr();
cout<<"You have enter these values in Array"<<endl;
for(x =0 ; x<10 ; x++)
{
cout<<"Ist Array at - "<<x+1<<" - " <<IndexA[x]<<"| 2nd Array - " <<IndexB[x]<<endl;
}

//int IndexC[10] = {0,0,0,0,0,0,0,0,0,0};
for(x =0 ; x <10 ;x++)
{
if(IndexA[x] == IndexB[x])
{
cout<<"Values Are Same At Index - "<<x+1<<"Where Value Is = "<<IndexA[x]<<endl;
}
}

return 0;
}

Solovd By Aatish...

3. Good job.

4. Xpert why it does not compile..........when i click RUN it gives message that "source file not compile"

5. After compiling, I found 4 errors. How can I rectify them?

6. well this should be completed by you. i just can offer help. i guess a person who is studying cs201 should remove the errors by him/her self. so i hope you will try and remove the errors and will post the correct error free code down this post.