Made some adjustments to my original program. Here's a link to it: original. Also, here's a link to the problem statement: problem statement.
The assignment is past due; The code I turned in is stupid simple. Which is great, but I didn't learn anything, so I am testing the many different ways to do things in C, and hoping some will have some hints, tips, tricks, advice, suggestions, etc.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
int read_positive_int(const char* prompt)
{
printf("%s\n\nInput Specifications: Please type a positive integer and press the 'Enter' or the 'return' key when finished.\n", prompt);
int n;
while (true)
{
char line[1024]; /* the input line */
fgets(line, sizeof(line), stdin);
int sscanf_result = sscanf(line, "%d", &n);
if (sscanf_result == 1 && n > 0)
{
return n;
}
puts("\nInput Error: Please carefully read the input specifications that are provided after each question prompt and then try again.\n");
}
}
float read_real_positive_float(const char* prompt)
{
printf("%s\n\nInput Specifications: Please type a real number in currency format, i.e., XXX.XX, and press the 'Enter' or the 'return' key when finished.\n", prompt);
float n;
while (true)
{
char line[1024];
fgets(line, sizeof(line), stdin);
int sscanf_result = sscanf(line, "%f", &n);
if (sscanf_result == 1 && n > 0 )
{
return n;
}
puts("\nInput Error: Please carefully read the input specifications that are provided after each question prompt and then try again.\n");
}
}
int main(void)
{
int total_shops = read_positive_int("How many shops will be visited?");
float **cost_ingredients_ptr = malloc(total_shops * sizeof(float*)); /* allocate an array of pointers */
if (!cost_ingredients_ptr)
{
fprintf(stderr, "Memory allocation failure!\n");
exit(1);
}
float **total_cost_ingredients_ptr = malloc(total_shops * sizeof(float*));
memset(total_cost_ingredients_ptr, 0, total_shops * sizeof(float));
if (!total_cost_ingredients_ptr)
{
fprintf(stderr, "Memory allocation failure!\n");
exit(1);
}
for (int i = 0; i < total_shops; i++)
{
printf("\nYou are at shop #%d.\n\n", i+1);
int quantity_ingredients = read_positive_int("How many ingredients are needed?");
cost_ingredients_ptr[i] = malloc(quantity_ingredients * sizeof(float));
if (!cost_ingredients_ptr[i])
{
fprintf(stderr, "Memory allocation failure!\n");
exit(1);
}
total_cost_ingredients_ptr[i] = malloc(sizeof(float));
if (!total_cost_ingredients_ptr)
{
fprintf(stderr, "Memory allocation failure!\n");
exit(1);
}
for (int j = 0; j < quantity_ingredients; j++)
{
printf("\nWhat is the cost of ingredient #%d", j+1);
cost_ingredients_ptr[i][j] = read_real_positive_float("?");
*total_cost_ingredients_ptr[i] += cost_ingredients_ptr[i][j];
}
printf("\nThe total cost at shop #%d is $%0.2f.\n", i+1, *total_cost_ingredients_ptr[i]);
if (i == total_shops-1)
{
float cheapest_order = *total_cost_ingredients_ptr[0];
int location_cheapest_order = 1;
for (int k = 1; k < total_shops; k++)
{
if (*total_cost_ingredients_ptr[k] < cheapest_order)
{
cheapest_order = *total_cost_ingredients_ptr[k];
location_cheapest_order = k + 1;
}
printf("\nThe cheapest order placed was at shop #%d, and the total cost of the order placed was $%0.2f.\n", location_cheapest_order, cheapest_order);
}
}
}
free(cost_ingredients_ptr);
free(total_cost_ingredients_ptr);
cost_ingredients_ptr = NULL;
total_cost_ingredients_ptr = NULL;
return 0;
}