Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a program (see code below) which allows the user to add items to a shoppinglist. My question is: How can I modify the

I have a program (see code below) which allows the user to add items to a shoppinglist. My question is: How can I modify the addItem()-function so the added items are displayed in alphabetical order?

Right now my list would be displayed in the same order as the items are entered (it does not take the alphabetical order in consideration):

1. coke 1 liter

2. apples 8 st

3. carrots 2 kg

So, even if this is the order the items are added, I would instead like the program to display them as follows:

1. apples 8 st

2. carrots 2 kg

3. coke 1 liter

Hence, checking all letters in each product name and display them in alphabetical order, and for the numbering to be correct as well!

My code:

oid addItem(struct ShoppingList* list) { printf("=============================== "); printf("Your list contains %d items ", list->length);

list->length++; struct GroceryList* newList = (struct GroceryItem*)realloc(list->itemList, sizeof(struct GroceryItem) * list->length);

if (newList == NULL) { printf("Error"); } else { list->itemList = newList;

while (getchar() != ' '); printf("Enter name: "); gets(list->itemList[list->length - 1].productName);//minus 1 fr att hamna p rtt plats

// Check if the item is already in the list int item_index = -1; for (int i = 0; i < list->length - 1; i++) { if (strcmp(list->itemList[i].productName, list->itemList[list->length - 1].productName) == 0) { item_index = i; break; } }

// If the item is already in the list, update the amount if (item_index != -1) { list->itemList[item_index].amount += list->itemList[list->length - 1].amount; list->length--; printf("This item is already in the list! Enter new amount: "); scanf_s("%f", &list->itemList[item_index].amount);

while (list->itemList[item_index].amount <= 0.0) //if the amount is negative, enter new/valid amount { printf("Invalid number. "); printf("Enter amount: "); scanf_s("%f", &list->itemList[item_index].amount); } } // Otherwise, add the new item to the list else { printf("Enter amount: "); scanf_s("%f", &list->itemList[list->length - 1].amount);

while (list->itemList[list->length - 1].amount <= 0.0) //om amount r negativ eller =0, be om ny amount { printf("Invalid number. "); printf("Enter amount: "); scanf_s("%f", &list->itemList[list->length].amount); }

while (getchar() != ' '); printf("Enter unit: "); gets(list->itemList[list->length - 1].unit); } }

printf("The list now contains %d items ", list->length); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions