In: Computer Science
WRITE A C++ PROGRAM:
QUESTION 1
ANSWER :-
GIVEN THAT :-
#include <iostream>
#include <cstring>
using namespace std;
const int hobbyItems=100;
int matchingPairs[hobbyItems][2]; //stores the matching
pairs
struct currency{ //hobby is collection of coins of
different countries
char country[20];
int denomination;
};
void readArray(currency items[]){ //read the values in
the parameter
int counter;
cout<<"Enter the country name and currency
denomination\n";
for(counter=0;counter<hobbyItems;counter++){
cin>>items[counter].country>>items[counter].denomination;
}
}
void displayArray(currency items[]){ //display the hobby
items of the parameter passed
int counter;
for(counter=0;counter<hobbyItems;counter++){
cout<<items[counter].country<<"\t"<<items[counter].denomination<<"\n";
}
}
int findMatchingPairs(currency myItems[],currency
friendItems[]){
int i,j;
int matchCount=0;
//find the matching pairs
//compare each item of my hobby with each element of friend
hobby
for(i=0;i<hobbyItems;i++){
for(j=0;j<hobbyItems;j++){
if((!strcmp(myItems[i].country,friendItems[j].country))&&(myItems[i].denomination==friendItems[j].denomination)){
matchingPairs[matchCount][0]=i;
matchingPairs[matchCount][1]=j;
matchCount++;
}
}
}
return matchCount;
}
int main()
{
currency myItems[hobbyItems],friendItems[hobbyItems];
int matchCount;
int i; //used as counter for loops
//read the hobby items
readArray(myItems);
readArray(friendItems);
matchCount=findMatchingPairs(myItems,friendItems);
//display my hobby items and friend hobby items
cout<<"My hobby items:\n";
displayArray(myItems);
cout<<"Hobby items of my friend:\n";
displayArray(friendItems);
//display the matching pairs
cout<<matchCount<<" matching enteries found";
for(i=0;i<matchCount;i++){
cout<<"\nMy item with index
"<<matchingPairs[i][0]<<" matches with friend's item
with index "<<matchingPairs[i][1];
}
return 0;
}
The program has been attached and the naming of the variables will ensure easy readability.The hobby has been assumed to be collection of currencies of different countries where each currency has two properties - country and denomination.
R1a- structure with name currency
R1b- country and denomination members
R1c- myItems[hobbyItems]
R1d- friendItems[]
R1e- matchingPairs[hobbyItems][2]
R1f- void readArray(currency items[])
R1g- readArray(myItems);
readArray(friendItems);
R1h- int findMatchingPairs(currency myItems[],currency friendItems[]){
R1i- matchCount=findMatchingPairs(myItems,friendItems);
THANKYOU