For this assignment, you are required to create a 2d dynamic array that stores the
following information about multiple books:
The book name
Its release year
The total number of ratings (total number of people that have rated the book)
The number of people that have given the book a 1-star rating
The number of people that have given the book a 2-star rating
The number of people that have given the book a 3-star rating
You are to write the following functions:
A function to create a 2D array according to user provided dimensions
A function to calculate the average rating.
Display function to display all the details of a selected book
A function to write the 2d array to a file called “BookLibrary.txt”.
#include <iostream>
using namespace std;
struct book {
string name;
int year;
int totalNumber;
int oneStar;
int twoStar;
int threeStar;
double avgRate() {
return (1.0 * oneStar + 2.0 * twoStar + 3.0 * threeStar) / totalNumber;
}
};
int main()
{
int numberOfBooks;
cout << "Enter number of books: ";
cin >> numberOfBooks;
book books[numberOfBooks];
for (int i = 0; i < numberOfBooks; i++) {
cout << "Name:";
cin >> books[i]->name;
cout << "Release year:";
cin >> books[i]->year;
cout << "Total number:";
cin >> books[i]->totalNumber;
cout << "One Star:";
cin >> books[i]->oneStar;
cout << "Two Star:";
cin >> books[i]->twoStar;
cout << "Three Star:";
cin >> books[i]->threeStar;
}
for (int i = 0; i < numberOfBooks; i++) {
cout << (i + 1) << "). " << books[i].avgRate() << endl;
}
return 0;
}
Comments
Leave a comment