Fix the errors in C program.

Question

I have been an error, How can I fix it?’-‘ -> printf(“Analysis Report (Note: Serving = %dg\n”, ???);

#define _CRT_SECURE_NO_WARNINGS

// defines/macros
#define MAX_PRODUCTS 3
#define MAX_GRAMS 64
#define CONVERSION_FACTOR_LBS_TO_KG 2.20462

// System Libraries
struct ReportData
{
int pro_sku;
double pro_price;
int cal_per_s;
double pro_wght_lbs;
double pro_wght_kg;
int pro_wght_gm;
double total_serv;
double cost_per_serv;
double cost_cal_per_serv;
};

// User-Defined Libraries
struct CatFoodInfo
{
int sku_number;
double product_price;
int calories_per_serving;
double product_weight;
};

// PART-1
// 1. Get user input of int type and validate for a positive non-zero number
//    (return the number while also assigning it to the pointer argument)
int getIntPositive(int* add);

// 2. Get user input of double type and validate for a positive non-zero number
//    (return the number while also assigning it to the pointer argument)
double getDoublePositive(double* add1);

// 3. Opening Message (include the number of products that need entering)
void openingMessage(const int nproducts);

// 4. Get user input for the details of cat food product
struct CatFoodInfo getCatFoodInfo(const int seqno);

// 5. Display the formatted table header
void displayCatFoodHeader(void)
{
printf("SKU         $Price    Bag-lbs Cal/Serv\n");
printf("------- ---------- ---------- --------\n");
}

// 6. Display a formatted record of cat food data
void displayCatFoodData(const int s_num, const double* pro_price, const int cal_ps,
const double* pro_wght);

// PART-2
// 8. convert lbs: kg (divide by 2.20462)
double convertLbsKg(const double* lbs_to_kg, double* kg_converted);

// 9. convert lbs: g (call convertKG, then * 1000)
int convertLbsG(const double* lbs_conv, int* g_result);

// 10. convert lbs: kg and g
void convertLbs(const double* lbs_con, double* kg_res, int* res);

// 11. calculate: servings based on gPerServ
double calculateServings(const int ser_gm, const int total_gm, double* result);

// 12. calculate: cost per serving
double calculateCostPerServing(const double* pro_pr, const double* serv, double*
result);

// 13. calculate: cost per calorie
double calculateCostPerCal(const double* pro_price, const double* total_cal,
double* cal_res);

// 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo pro_data);

// 15. Display the formatted table header for the analysis results
void displayReportHeader(void)
{
printf("Analysis Report (Note: Serving = %dg\n", ???);
printf("---------------\n");
printf("SKU         $Price    Bag-lbs     Bag-kg     Bag-g Cal/Serv Servings  $/Serv   $/Cal\n");
printf("------- ---------- ---------- ---------- --------- -------- -------- ------- -------\n");
}

// 16. Display the formatted data row in the analysis table
void displayReportData(const struct ReportData display, const int cheap_pro);

// 17. Display the findings (cheapest)
void displayFinalAnalysis(const struct CatFoodInfo data);

// 7. Logic entry point
void start(void)
{}

 

Summary

In the given question, we have errors in the code, and we have to fix the errors. In the above code, we have been provided the function prototypes and the corresponding functions are defined based on the structures which are declared above.

Here we have two structures defined, which are ReportData and CatFoodInfo. The structure ReportData has nine data members which report a particular cat food, and the structure CatFoodInfo has four data members to describe the characteristics of particular cat food.

ReportData members are pro_sku, pro_price, cal_per_s, pro_wght_lbs, pro_wght_kg, pro_wght_gm, total_serv, cost_per_serv, and cost_cal_per_serv. CatFoodInfo members are sku_number, product_price, calories_per_serving, and product_weight. we have to write the statements for the defined functions also.

Explanation

Here, we will talk about functions defined in this code.

The function getIntPositive() takes in a pointer and asks the user to enter a positive integer and assigns it to the pointer.
getDoublePositive() function takes in a double-pointer, gets double values from the user, which is positive and assigns it to that pointer.

The function getCatFoodInfo() get the info about a particular CatFoodInfo structure variable from user.
displayCatFoodData() function displays the details of a CatFoodInfo structure variable.

The function displayCatFoodHeader() displays a table containing all the details about all the CatFoods.
Similar functions are present for ReportData also, which are displayReportData and displayReportDataHeader.

The function convertLbs converts Lbs to Kg and also Lbs to gm, by calling two other functions convertLbsKg and convertLbsG.

The function calculateServings is defined to find the serving of the data. 

Code

We fix the errors. After fixing the code, the code is below.

#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS

// defines/macros
#define MAX_PRODUCTS 3
#define MAX_GRAMS 64
#define CONVERSION_FACTOR_LBS_TO_KG 2.20462

// System Libraries
struct ReportData
{
int pro_sku;
double pro_price;
int cal_per_s;
double pro_wght_lbs;
double pro_wght_kg;
int pro_wght_gm;
double total_serv;
double cost_per_serv;
double cost_cal_per_serv;
};

// User-Defined Libraries
struct CatFoodInfo
{
int sku_number;
double product_price;
int calories_per_serving;
double product_weight;
};

// 1. Get user input of int type and validate for a positive non-zero number
//    (return the number while also assigning it to the pointer argument)
int getIntPositive(int* add){
    int x=-1;
    while(x<=0){
        printf("Enter a positve non-zero number: ");
        scanf("%d",&x);
    }
    *add=x;
    return x;
}

// 2. Get user input of double type and validate for a positive non-zero number
//    (return the number while also assigning it to the pointer argument)
double getDoublePositive(double* add1){
    double x=-1;
    while(x<=0){
        printf("Enter a positve non-zero number: ");
        scanf("%lf",&x);
    }
    *add1=x;
    return x;
}

// 3. Opening Message (include the number of products that need entering)
void openingMessage(int nproducts){
    printf("Enter the number of products: ");
    scanf("%d", &nproducts);
}

// 4. Get user input for the details of cat food product
struct CatFoodInfo getCatFoodInfo(const int seqno){
    struct CatFoodInfo c;
    printf("Enter the sku number: ");
    scanf("%d",&c.sku_number);
    printf("Enter the product price: ");
    scanf("%lf",&c.product_price);
    printf("Enter the calories per serving: ");
    scanf("%d",&c.calories_per_serving);
    printf("Enter the product weight: ");
    scanf("%lf",&c.product_weight);
    return c;
}

void displayCatFoodData(const int s_num, const double pro_price, const int cal_ps,const double pro_wght); 

// 5. Display the formatted table header
void displayCatFoodHeader(struct CatFoodInfo c[2], int n)
{
printf("SKU       $Price      Bag-lbs  Cal/Serv\n");
printf("---------------------------------------\n");
for(int i=0;i<n;i++)
displayCatFoodData(c[i].sku_number, c[i].product_price, c[i].calories_per_serving, c[i].product_weight);
}

// 6. Display a formatted record of cat food data
void displayCatFoodData(const int s_num, const double pro_price, const int cal_ps,
const double pro_wght){
    printf("%d      %lf     %d       %lf\n", s_num,pro_price,cal_ps,pro_wght);
}

// 8. convert lbs: kg (divide by 2.20462)
double convertLbsKg(const double *lbs_to_kg, double* kg_converted){
    *kg_converted = *lbs_to_kg / 2.20462;
    return *kg_converted;
}

// 9. convert lbs: g (call convertKG, then * 1000)
int convertLbsG(const double *lbs_conv, int* g_result){
    double *kg=new double;
    convertLbsKg(lbs_conv, kg);
    *g_result = *kg*1000;
    return *g_result;
}

// 10. convert lbs: kg and g
void convertLbs(const double* lbs_con, double* kg_res, int* res){
    convertLbsKg(lbs_con, kg_res);
    convertLbsG(lbs_con, res);
}

// 11. calculate: servings based on gPerServ
double calculateServings(const int ser_gm, const int total_gm, double result){
    result = total_gm/ser_gm;
    return result;
}

// 12. calculate: cost per serving
double calculateCostPerServing(const double pro_pr, const double serv, double result){
    result = pro_pr * serv;
    return result;
}

// 13. calculate: cost per calorie
double calculateCostPerCal(const double pro_price, const double total_cal, double cal_res){
    cal_res = total_cal/ pro_price;
    return cal_res;
}

// 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo pro_data){
    struct ReportData r;
    r.pro_sku=pro_data.sku_number;
    r.pro_price=pro_data.product_price;
    r.cal_per_s=pro_data.calories_per_serving;
    r.pro_wght_lbs=pro_data.product_weight;
    convertLbs(&r.pro_wght_lbs,&r.pro_wght_kg,&r.pro_wght_gm);
    return r;
}

void displayReportData(struct ReportData display, int cheap_pro);

// 15. Display the formatted table header for the analysis results
void displayReportHeader(struct ReportData r[2], int n){
printf("---------------\n");
printf("SKU        $Price    Bag-lbs   Bag-kg    Bag-g     Cal/Serv  Servings  $/Serv    $/Cal\n");
printf("-------------------------------------------------------------------------------------------\n");
for(int i=0;i<n;i++)
displayReportData(r[i],0);

}

// 16. Display the formatted data row in the analysis table
void displayReportData(struct ReportData display, int cheap_pro){
printf("%d        %lf        %d        %lf        %lf        %d        %lf        %lf        %lf\n",
display.pro_sku, display.pro_price, display.cal_per_s, display.pro_wght_lbs, display.pro_wght_kg,
display.pro_wght_gm,display.total_serv, display.cost_per_serv,display.cost_cal_per_serv);
}

// 7. Logic entry point
int main()
{
    CatFoodInfo c[2];
    c[0]=getCatFoodInfo(0);
    displayCatFoodHeader(c,1);
}

Output

Output after fixing the code will be as follows.

Fixing the errors in C program output

 

Also read, How to delete a file or folder in Python?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *