Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program that calculates the average price for all books, the aver

ID: 3750539 • Letter: I

Question

I am writing a program that calculates the average price for all books, the average price for books published by macmillan and prints the title and publisher of the most expensive book. I keep getting the error 'lgs' :undeclared identifier & 'lgs' is undefined even though i have clearly defined it. I am also unaware if the rest of the code is written correctly.

#include <stdio.h>

#include <string.h>

char T[12][26] = { "the pelican", "the raven", "canary is yellow", "frankenstein", "dracula", "ender's game", "The wolfman",

"the tell tale heart", "sleep walkers" "the lost boys", "true blood", "james and the giant peach", "the odyssey"};

char PU[3][26] = { "springer verlag", "macmillan", "prentice hall" };

float P[12] = {3.45, 2.89, 12.76, 109.21, 14.20, 7.50, 18.25, 13.79, 16.42, 10.67, 11.86, 5.50};

struct book {

char title[26];

char publisher[26];

float price;

};

void main()

{

struct book Library[50];

int i;

int ctr = 0, mac_ctr = 0; //counter to add the number of books for taking the averge.

float tot = 0, mac_tot = 0; //total prices of books for taking the averge

double avg, mac_avg;

char pubcheck[9] = { "macmillan" }; //sets an array to check the publishers with

for (i=0; i <= 26; i++)

{

float lg = Library[i].price; //will save the position of the most expensive book

int lgs = i; //elem in the array of the most expensive book

int pub = strcmp(pubcheck, Library[i].publisher);//checks to see if book publisher is macmillan

if (Library[i].price > lg)

{

lg = Library[i].price;

lgs = i;

}

if (pub == 0)

{

mac_tot += Library[i].price;

mac_ctr++;

}

tot += Library[i].price;

ctr++;

}

avg = tot / ctr;

mac_avg = mac_tot / mac_ctr;

printf("The average book cost is %.2f ", avg);

printf("The average book cost for Macmillan is %.2f ", mac_avg);

printf("The most expensive book is %s published by %s. ", Library[lgs].title, Library[lgs].publisher);

printf("hello");

system("pause");

return;

}

Explanation / Answer

The code is now working fine. But sir initialize the array Library according to your program requirements. Reast is working correctly.

Code

#include <stdio.h>

#include <string.h>

char T[12][30] = { "the pelican", "the raven", "canary is yellow", "frankenstein", "dracula", "ender's game", "The wolfman","the tell tale heart", "sleep walkers" "the lost boys", "true blood", "james and the giant peach", "the odyssey"};

char PU[3][30] = { "springer verlag", "macmillan", "prentice hall" };

float P[] = {3.45, 2.89, 12.76, 109.21, 14.20, 7.50, 18.25, 13.79, 16.42, 10.67, 11.86, 5.50};

struct book {

    char title[26];

    char publisher[26];

    float price;

};

int main()

{

   

    struct book Library[50];   

    int i;

    int ctr = 0, mac_ctr = 0; //counter to add the number of books for taking the averge.

    float tot = 0.0, mac_tot = 0.0; //total prices of books for taking the averge

    double avg, mac_avg;

    char pubcheck[10] = { "macmillan" }; //sets an array to check the publishers with

    int lgs = 0;

    float lg = -1;

   

    for (i=0; i <= 26; i++)

    {

        lg = Library[i].price; //will save the price of the most expensive book

       

       // if you declare lgs here, then the scope will only be limited to for loop, so I declared lgs out of the for loop

        lgs = i; //elem in the array of the most expensive book

       

        int pub = strcmp(pubcheck, Library[i].publisher);//checks to see if book publisher is macmillan

       

        if (Library[i].price > lg)

        {

            lg = Library[i].price;

            lgs = i;

        }

       

        if (pub == 0)

        {

            mac_tot += Library[i].price;

            mac_ctr++;

        }

       

        tot += Library[i].price;

        ctr++;

       

    }

   

    avg = (double)tot / (double)ctr;

    mac_avg = (double)mac_tot / (double)mac_ctr;

   

    printf("The average book cost is %.2lf ", avg);

    printf("The average book cost for Macmillan is %.2lf ", mac_avg);

    printf("The most expensive book is %s published by %s. ", Library[lgs].title, Library[lgs].publisher);

    printf("hello ");

   

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote