[Solved]Lab2 Include Include Int Main Declare Variables Char Title 20 Author 20 Isbn 20 Targetisbn Q37039016
LAB2
#include <stdio.h>
#include <string.h>
int main()
{
//Declare variables
chartitle[20],author[20],ISBN[20],targetISBN[20];
int date;
float cost;
//opening file in read mode
FILE *fp=fopen(“books.txt”,”r”);
if(fp==NULL){
//error. make sure you have books.txt in the same folder
printf(“books.txt file not found.n”);
return 0;
}
printf(“Enter ISBN of the book you want to print: “);
scanf(“%s”,targetISBN);
//looping until EOF
int count=0;
while(fscanf(fp,” %[^tn]s”,title)==1){
//title is already read, reading remaining data
fscanf(fp,” %[^tn]s”,author);
//reading date as integer, isbn as string and cost as float
fscanf(fp,”%d”,&date);
fscanf(fp,”%s”,ISBN);
fscanf(fp,”%f”,&cost);
if(strcmp(ISBN,targetISBN)==0){
//printing everything only if the book matches target ISBN
printf(“Title: %sn”,title);
printf(“Author: %sn”,author);
printf(“Date: %dn”,date);
printf(“ISBN: %sn”,ISBN);
printf(“Cost: $%.2fn”,cost);
count++;
}
}
if(count==0){
//no books found with target ISBN
printf(“No results foundn”);
}
fclose(fp);
return 0;
}
Instructions:
Part 1. This programming assignment requiresyou to define arrays in main which will be used to savethe properties for the five books in your data file, rather thanusing individual variables. Name your arrays by adding ‘s’ to theend of the names you chose for the book variables in Lab 2 (forexample, titles). Use an eof-controlled while-loop to readin and save in the correct arrays all the data for each book in thefile. By using arrays, you will be able to save and later processthe data for all books, rather than having just the last book’sdata saved after the loop finishes. Display all thedata for each book in the file before going to Part 2.
Part 2. You should define 3 additionalfunctions in your program, as defined below. If you define thefunctions above main, no prototype statements arenecessary. If you define the functions below main, includea prototype statement for each function, placed abovemain.
a. For the first version of your program, define the completedetails of only the first function, and writestubs for the next two functions. The stub foravgCost should return a cost of 0.0 and the stub forsortTitles should just print the message “Underconstruction. Sorting coming soon!”. Call each function inmain, capturing a screenshot of the results. (Okay to callall three and capture one screenshot, although typically for thistype of development, only one function would be tested at a timeusing a short driver main function).
b. Next, complete the last two functions and test them,capturing screenshots for them after they have been completed.
findOldest: This function takes thearray with the publication dates for all books and the length ofthe array (use a variable for the length so that the function willwork for any size array, not just 5). It returns the index positionof the oldest date in the array. After the function has finishedexecuting, the title as well as the publication date of the oldestbook should be displayed in main.
avgCost: This function takes the array with thecosts for all books and the length of the array (use a variable forthe length). It returns the average cost of all the books. Afterthe function has finished executing, the average cost should bedisplayed in main. The author and the cost for all bookswith a cost above the average also should be displayed.
sortTitles: This function takes the array withtitles and the length of the array (use a variable for the length)and sorts the titles in alphabetical order. It does not returnanything. After the function has finished executing, all the titlesin the array should be displayed in main. Verify that thearray of titles has been sorted by the function. Question toconsider: what does sorting the titles mean for the other arrayscontaining data for the books?
Expert Answer
Answer to LAB2 #include #include int main() { //Declare variables char title[20],author[20],ISBN[20],targetISBN[20]; int date; flo… . . .
OR

