Registration workers at a conference for authors of children
Registration workers at a conference for authors of children’s books have collected data about conference participants, including the number of book’s each author has written and the target age of their readers. The participants have written from 1 to 40 books each, and target readers’ ages range from 0 through 16. Design a program that continuously accepts an author’s name, a number of books written, and target reader age until a sentinel value is entered. Then display a list of how many participants have written each number of books (1 through 40.)
i. Assume that we will have up to 500 authors. the
ii. For part a),store the author\'s name, number of books written and target reader age in three separate parallel arrays. Then, write separate loops and other code required to: a. displays each author\'s name, the number of books he/she has written and the target reader age b. calculate and display the total number of books written c. calculate and display the average number of books written by all authors d. calculate and display the average target reader age e. allow the user to enter an author\'s name, then search for that name and display how many books that author has written and his/her target reader age
Solution
#include <iostream>
using namespace std;
int main(){
// Number of author could be upto 500 so created three arrays to store each author name,
// book count and intended age of book reader
int bookCount[501], targetAge[501]; // number of books and age of book reader
char authorName[501][50]; // each array index is array of character to store the author\'s name
int index = 0; // increment this value if you found new author
while(1){ // If user is giving three input then continue
char name[50];
int bookC, age;
scanf(\"%s%d%d\",name, &bookC, &age);
if(age == -1)break; // give third input -1 to break the incoming input
bool authorIsAlreadyPresent = false; // if that author has came before we just need to
printf(\"\ \");
for(int i = 0;i < index;++i){
if(strcmp(name, authorName[i]) == 0){ // if author has already written a book
bookCount[i]+=bookC; // increase the book count by that author
authorIsAlreadyPresent = true;
break;
}
}
if(!authorIsAlreadyPresent){ // if this is the new author which we still have not found in the input add it to the list and increment index counter
strcpy(authorName[index], name);
bookCount[index] = bookC;
targetAge[index] = age;
index++;
}
}
//part 1
int numberOfBooks[41];
for(int i = 0;i < index;++i){
numberOfBooks[bookCount[i]]++; // array where index has value of number of author written that many books
}
//display the list
for(int i = 1;i <= 40; ++i){
printf(\"%d author has written %d books\ \", numberOfBooks[i], i);
}
//part 2
//2a displaying author name and number of books written by them
for(int i = 0;i < index;++i){
printf(\"%s has written %d books\ \", authorName[i], bookCount[i]);
}
//2b
int totalBooks = 0;
for(int i = 0;i < index;++i){
totalBooks += bookCount[i];
}
printf(\"Total books written is %d\ \",totalBooks);
//2c average number of book written by all authors
printf(\"Avergae number of books written by all authors is %.2f\ \", (1.*totalBooks) / index);
//2d average target reader age
int age[17];
//as age of readers could be from 0 to 16
for(int i = 0;i < 17;++i){
age[i] = 0;
}
for(int i = 0;i < index;++i){
age[targetAge[i]]++; // increment the age index if author has written the book for that age
}
//Average age will be find by multiplying number of people into their age in range from 0 to 16
// dividing by total number of people for each age in rage 0 to 16
int totalAge = 0, count = 0;
for(int i = 0;i < 17;++i){
totalAge += age[i] * i;
count += age[i];
}
printf(\"Average Readers age is %.2f\ \", (1.*totalAge) / count);
//2e search the people by name
char name[50];
while(scanf(\"%s\", name) == 1){
// search the author in the list
printf(\"Please enter the author name to know more about his/her\ \");
for(int i = 0;i < index;++i){
if(strcmp(name, authorName[i]) == 0){
printf(\"%s has written %d for %d years old\ \",name, bookCount[i], targetAge[i]);
}
}
}
return 0;
}

