Need Help with Assignment. Now sure how to start



This assignment requires you to maintain ratings for products sold by a company. Each product sold
is stored in an array. A rating for a product consists of the name of the person who rated the product
and the number of stars that were given, in a range of 1 to 5. All the ratings for a product are stored
as nodes in a linked list.
You are required to write a program that reads product information from a file and store the
information in an array. Your program must then read commands from another file and perform the
corresponding operations in the array of products and their ratings.
Structure of Command File (Commands.txt)
Each line in the command file contains one of six (6) commands as specified in the table below:
Command Command Followed By Description
1 Product code (string)
Person name (string)
Rating (integer)
The rating must be added to the linked list of ratings for
that product subject to the condition that a person cannot
rate a product two or more times. The nodes of the linked
list must be maintained in descending sorted order based
on the rating value. If the product is not found or the
rating is out of range, an appropriate message should be
displayed.
2 Product code (string) If the product is found, the list of all the ratings for that
product must be displayed on the monitor. If the product
is not found, an appropriate message should be displayed.
3 Person name (string) Each product is checked to find out if it has a rating by the
person specified. If so, the product data and rating is
displayed, one per line. Otherwise, an error message
should be displayed.
4 Rating (integer) Each product is checked to determine if its average rating
is greater than or equal to the rating specified. If so, the
product data and average rating is displayed, one per line.
5 Product code (string)
Person name (string)
If the product is found, delete any rating for that product
by the given person (if there is one).
99 Nothing Terminate the program.
Table 2: List of Commands in the Command File
2
Programming Requirements
1. You must use the following declarations for a Node in the linked list and a Product:
struct Node {
string name;
int rating;
Node * next;
};
struct Product {
string code;
string name;
Node * top;
};
2. Your main function must declare an array to hold at most 1000 Product structs.
3. You must write the functions specified in the Table 2.
Function Description
Node * createNode (string name, int rating) Creates a node to be inserted in the linked list
of ratings for a product.
Node * insertSortedList (Node * top, string
name, int rating)
Given top, inserts a new node in a linked list of
ratings for a product and returns the top of the
updated list. The node must be inserted in
descending order of rating.
void insertProductRating (Product products[],
int numProducts, string code, string name, int
rating)
Given a product code, inserts a new node in the
linked list of ratings for that product using the
insertSortedList function. Ensure that there is
only one rating by the same person. It should
be called when Command 1 is being processed.
Node * deleteList (Node * top, string name) Give top, deletes the rating by the person
specified and returns the top of the updated
list.
void deleteProductRating (Product products[],
int numProducts, string code, string name)
Given a product code, deletes the rating by the
person specified using the deleteList function.
Display an error if no such rating exists. It
should be called when Command 5 is being
processed.
void displayRatings (Node * top) Given top, displays all the ratings for a product
in the linked list.
int readProducts (Product products[]) Reads the data on each product from the
Product.txt file and stores it in the products
array. Returns the number of products read
from the file.
Node * findRating (Node * top, string name) Given top and the name of a person, returns
the address of the node with the rating made
by that person, or NULL if it does not exist.
int averageRating (Node * top) Given top, returns the average of the ratings in
the linked list for that product. The average
must be rounded to the nearest whole number.
3
int findProduct (Product products[], int
numProducts, string code)
Given the products array, returns the location
of the product with the given code in the array.
If it does not exist, return -1. [Optional]
void displayProductRatings (Product products[],
int numProducts, string code)
This function should be called when Command
2 is being processed with the given product
code.
void displayPersonRatings (Product products[],
int numProducts, string name)
This function should be called when Command
3 is being processed with the given person
name.
void displayRatingsGTE (Product products[], int
numProducts, int rating)
This function should be called when Command
4 is being processed with the given rating.
Table 2: List of Functions to Write and Test for Assignment 2
4. You do not have to write all the functions in Table 2 from scratch. Table 3 lists the similarity
between some of the functions in Table 2 and the functions that have already been provided in
the lectures and labs.
Function Similar To
Node * createNode (string name, int rating) createNode function of Lab #7.
Node * insertSortedList (Node * top, string
name, int rating)
insertSortedList and predecessor
functions of Lecture #8 and Lab #7.
Node * deleteList (Node * top, string name) deleteList function of Lecture #8.
void displayRatings (Node * top) printList function of Lab #7.
Node * findRating (Node * top, string name) containsList function of Lab #7 (except
that it returns the address of the node
instead of true/false).
Data Files
The data file with the product data is Products.txt. The data file with the commands is
Commands.txt. These files have been provided with the assignment but will be updated on March
12, 2022. At the same time, sample output that is expected from your program using these files will
be made available.
(Continued on Page 4 …)
4
Visualization



Last edited on
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
Well you start by first producing a program design. Once you have that, then you code from the design. Code in small parts and compile and test frequently.

From the requirements, what don't you understand?
Last edited on
You're given a lot of the data and function declarations already. Copy those to your source file. Include the descriptions as comments. For example, the assignment includes:
void deleteProductRating (Product products[],
int numProducts, string code, string name)
Given a product code, deletes the rating by the
person specified using the deleteList function.
Display an error if no such rating exists. It
should be called when Command 5 is being
processed.

Put this in your file:
1
2
3
4
5
6
7
8
// Given a product code, deletes the rating by the
// person specified using the deleteList function.
// Display an error if no such rating exists. It
// should be called when Command 5 is being
// processed.
void deleteProductRating (Product products[], int numProducts, string code, string name)
{
}


Next, pay close attention to this:
4. You do not have to write all the functions in Table 2 from scratch. Table 3 lists the similarity between some of the functions in Table 2 and the functions that have already been provided in the lectures and labs.
Look at the other code and see what can be reused.

Now you can start writing code. The key to doing this efficiently is to work on a little bit at a time. I'd start with this:
int readProducts (Product products[])
Also write a function to display the list of products. You won't use this in the final program. It's just for testing readProducts:
1
2
// Display the product info for "size" products in the array.
void displayProducts(Product products[], unsigned size);


Now test this main program:
1
2
3
4
5
int main()
{
unsigned size = readProducts(products);
displayProducts(products, size);
}

Start with just one product in your input file. Then try it with two, then with all of them.

The reason for doing this is "garbage in, garbage out." Until you can read the products correctly, there is NO HOPE of the rest of the code working correctly. Also, if you're reading the products wrong, you could spent hours trying to find a bug in, say, the code that adds a rating, only to find that the real problem is that the product was never read correctly in the first place.

Now add code to insert a rating for a product and display ratings for a product. Add one rating to one product. Then add 2. then 3 or 4. Make sure they're sorted correctly.

Continue, one small function at a time.

If you get stuck for more than an hour, post your code here, along with any input that you're using, the output you get (if any) and the output that you expect. People will be amazingly helpful if you give these details. The reason to ask for help after an hour is that if you haven't figured something out in an hour, you aren't likely to figure it out in 12 hours either. You don't want to be "that guy" who posts "it's 3am and I've been working on this for 12 hours and just can't get it." When the answer is "you have an extra semicolon at line 74."

Good luck.

Topic archived. No new replies allowed.