Write a program that will store 7 integers (entered by the user) into an array c
ID: 3569561 • Letter: W
Question
Write a program that will store 7 integers (entered by the user) into an array ca lled "list1". Next, prompt the user for 5 integers and put them into an array called "list2". Now, list all of the numbers that are in BOTH lists. Below is a sample run. NOTE: Include a functio n called "getNumber" which asks the user for a number, then RE TURNS it to calling function (in this case, "main").
List 1
------
Enter an integer: 4
Enter an integer: 2
Enter an integer: 7
Enter an integer: 1
Enter an integer: 5
Enter an integer: 6
Enter an integer: 3
List 2
------
Enter an integer: 11
Enter an integer: 9
Enter an integer: 7
Enter an integer: 5
Enter an integer: 3
The numbers that are in both lists are:
3, 5, 7
NOTE: DO NOT USE GLOBAL VARIABLES!!!!! A global variable is a variable that is defined outside of any function (including ma in). We don't cover them in this co urse because they are generally a very bad idea. So, don't do this in your projects!
Explanation / Answer
#include<iostream>
using namespace std;
int getnumber()
{
int k;
cin>>k;
return k;
}
int main()
{
int list1[7],list2[5],i,j;
cout<<"list1"<<endl;
for(i=0;i<7;i++)
{
cout<<"Enter an Integer: ";
list1[i]=getnumber();
}
cout<<"list2"<<endl;
for(i=0;i<5;i++)
{
cout<<"Enter an Integer: ";
list2[i]=getnumber();
}
for(i=0;i<5;i++)
{
for(j=0;j<7;j++)
{
if(list2[i]==list1[j])
{
if(i==0)
cout<<"Numbers in both list are: ";
cout<<list2[i]<<" ,";
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.