Question

In: Computer Science

1) Define a C struct that can be used to represent an ingredient in a recipe....

1) Define a C struct that can be used to represent an ingredient in a recipe. You must include the ingredient, the amount to use, and the unit of measurement. When allocated, the struct must be self-contained; do not rely on information being stored anywhere else in memory.

2) Define a C function that will print an array of ingredients to the standard output stream, one per line. You must use the struct definition from the first part.

3) Define a C function that will sort an array of ingredients, alphabetically, using the selection sort algorithm. You must use the struct definition from the first part, and you must use the selection sort algorithm.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ingredient {
  char name[100];
  int amount;
  char unit[100];
};

void print(struct ingredient arr[], int n) {
  for (int i = 0; i < n; ++i) {
    printf("%s : %d %s\n", arr[i].name, arr[i].amount, arr[i].unit);
  }
}

void swap(struct ingredient *xp, struct ingredient *yp) {

  char temp1[100];
  strcpy(temp1, xp->name);
  int temp2 = xp->amount;
  char temp3[100];
  strcpy(temp3, xp->unit);

  strcpy(xp->name, yp->name);
  xp->amount = yp->amount;
  strcpy(xp->unit, yp->unit);

  strcpy(yp->name, temp1);
  yp->amount = temp2;
  strcpy(yp->unit, temp3);
}

void sort(struct ingredient arr[], int n) {

  int i, j, min_idx;

  // One by one move boundary of unsorted subarray
  for (i = 0; i < n - 1; i++)
  {
    // Find the minimum element in unsorted array
    min_idx = i;
    for (j = i + 1; j < n; j++) {

      if (strcmp(arr[j].name, arr[min_idx].name) < 0)
        min_idx = j;
    }

    // Swap the found minimum element with the first element
    swap(&arr[min_idx], &arr[i]);
  }
}



int main() {

  struct ingredient arr[2];

  strcpy(arr[0].name, "pepper");
  arr[0].amount = 40;
  strcpy(arr[0].unit, "gms");

  strcpy(arr[1].name , "chilli");
  arr[1].amount = 20;
  strcpy(arr[1].unit , "gms");

  printf("Before Sorting:\n");
  print(arr,2);
  sort(arr,2);
  printf("\nAfter Sorting:\n");
  print(arr,2);

  return 0;
}

OUTPUT:


Related Solutions

Write a program in C language 1- Define a struct for students with the aforementioned attributes,...
Write a program in C language 1- Define a struct for students with the aforementioned attributes, test it by populating one initialized struct variable with arbitrary input and take screenshots of the output. 2- For each student, struct add an array of number grades for a class the students are enrolled in such as S E 185. Then write functions which find the max, average, and minimum score for a specified assignment identified by a number, for example, Assignment 0...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
in unix(bash) Write a Makefile with a recipe which can be used to compile myprog.c into...
in unix(bash) Write a Makefile with a recipe which can be used to compile myprog.c into myprog. Be sure to indicate that "myprog" depends on myprog.c!
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct ListNode *list_prepend(struct ListNode *list, long value); int list_length(struct ListNode *list); struct ListNode *list_remove(struct ListNode *list, long value); #endif /* LIST_H_ */ given.c file #include #include "list.h" struct ListNode *list_prepend(struct ListNode *list, long value) { struct ListNode *node = malloc(sizeof(struct ListNode)); node->value = value; node->next = list; return node; } list.c file #include #include "list.h" /* Counts and returns the number of nodes in the...
1-Define UDP? 2-What voltage levels are used in RS232 to represent logic 0 and 1
1-Define UDP? 2-What voltage levels are used in RS232 to represent logic 0 and 1
1. Define a struct for a soccer player that stores their name, jersey number, and total...
1. Define a struct for a soccer player that stores their name, jersey number, and total points scored. 2. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the average number of points scored by the players. 3. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the index of the player who scored...
Define two C structures, one to represent rectangular coordinates and one to represent polar coordinates. Rewrite...
Define two C structures, one to represent rectangular coordinates and one to represent polar coordinates. Rewrite the rec_to_polar function to use variables declared using the new structures.
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
In this assignment you will build a small C# project that uses… • A struct •...
In this assignment you will build a small C# project that uses… • A struct • A method with a reference parameter • A while-loop • A switch statement and block instructions: Inside the StringHandler struct add a public void method named Abbreviate. The Abbreviate method must take a string parameter that is passed by reference. This method will take the name of a month (January, February, etc.) as its input and convert it to a 3-letter abbreviation of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT