Question

In: Computer Science

WRITE A C++ PROGRAM: QUESTION 1 Assume you have a hobby to collect a particular type...

WRITE A C++ PROGRAM:

QUESTION 1

  1. Assume you have a hobby to collect a particular type of items. Specify the category to which the items you collect belong to ____________________________
    1. [2 points] Define a struct with the name the same as the category that you filled in the blank above
    2. [4 points] The struct must have at least two members corresponding to properties relevant to the items in the category you specified. The struct members must have names meaningful to the items.
    3. [2 points] Declare an array to store 100 of your hobby items.
    4. [2 points] Declare another array to store 100 of the hobby items that belong to a friend
    5. [2 points] Declare a two dimensional array called matchingPairs that has a maximum of 100 rows. The values in this array will be indices to arrays declared in c and d.
    6. [5 points] Write a function to read values into any array similar to the ones in c and d. Note that when the function returns, the array that is passed into the function must contain the values read in.
    7. [5 points] Write statements to call the function in f to read values into arrays declared in c and d.
    8. [5 points] Write a function that takes in two arrays similar to the ones in c and d and returns an array similar to the one in e. The function must compare elements in the input arrays and store indices where matches are found. For instance, if the item in array index 5 of the first array matches the item in array index 2 of the second array, then 5 and 2 are stored in one row in the third array. Care must be taken to ensure that the first two arrays remain intact, that is no changes happen in the first two arrays.
    9. [5 points] Write code to call the function in h and print the results of the array returned by the function in h.
    10. Adequate comments in code [3 points]
    11. Program structure and readability [5 points]

Solutions

Expert Solution

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


Related Solutions

QUESTION 2 write a c++ program Assume that you own a construction company that builds different...
QUESTION 2 write a c++ program Assume that you own a construction company that builds different types of buildings. The goal is to write a program that is potentially useful for your company. [5 points] Define a base class called building. Include at least two member variables and three member functions [10 points] Define two derived classes that inherit properties from the base class [10 points] Demonstrate at least one example each of overriding and overloading by defining the functions...
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Write a C++ or program that parses (and stores into a variable of the correct type)...
Write a C++ or program that parses (and stores into a variable of the correct type) a string into 4 fields separated by a colon (:): Field 1: A non-empty string value that does not contain a colon Filed 2: An odd integer, or empty Filed 3: One of these values: red, orange, yellow, green, blue, indigo, violet Field 4: Another string value (may contain colon characters) output: Accept: sample:556989:green:Longer example string :) Reject (bad color): sample:556989:purple:Longer example string :)...
Please if you are able to answer the question below: Write C++ program using native C++...
Please if you are able to answer the question below: Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT