All about dynamic memory allocation! In C language.
COP 3502C Programming Assignment # 1 Dynamic Memory Allocation Read all the pages before starting to write your code Overview This assignment is intended to make you work with dynamic memory allocation, pointers, and arrays of pointers. The difficulty level is not actually very high, but it might take sometimes to do it depending on your understanding of Dynamic Memory Allocation - don't wait until the weekend it's due to start it! Your solution should follow a set of requirements to get credit. Please include the following commented lines in the beginning of your code to declare your authorship of the code: /* COP 3502C Assignment 1 This program is written by: Your Full Name */ Compliance with Rules: UCF Golden rules apply towards this assignment and submission. Assignment rules mentioned in syllabus, are also applied in this submission. The TA and Instructor can call any students for explaining any part of the code in order to better assess your authorship and for further clarification if needed. Caution!!! Sharing this assignment description (fully or partly) as well as your code (fully or partly) to anyone/anywhere is a violation of the policy. I may report to office of student conduct and an investigation can easily trace the student who shared/posted it. Also, getting a part of code from anywhere will be considered as cheating. Deadline (updated): See the deadline in Mimir. The assignment will accept late submission up to 48 hours after the due date time without any penalty. However, if you submit it by the due date time, you will get 5% extra based on your regular score. After 2 days of the deadline, the submission will be locked. An assignment submitted by email will not be graded and such emails will not be replied according to the course policy. What to do if you need clarification on the problem? I will create a discussion thread in webcourses and I highly encourage you to ask your question in the discussion board. Maybe many students might have same question like you. Also, other students can reply and you might get your answer faster. Also, you can write an email to the TAs and put the course teacher in the cc for clarification on the requirements. How to get help if you are stuck? According to the course policy, all the helps should be taken during office hours. Occasionally, we might reply in email. Problem Description: Making Smoothies A smoothie is a blended drink concoction, with several ingredients. Each different type of smoothie has a different ratio of its ingredients. For example, the "StrawberryBreeze" has the following ratios of items: 2 units strawberry 1 unit banana 1 unit kiwi 2 units yogurt If a store manager sells 18 pounds of Strawberry Breeze smoothie a week, then his weekly order ought to have 6 lbs of strawberries, 3 lbs of banana, 3 lbs of kiwi and 6 lbs of yogurt. Of course, there are other types of smoothies a store manager must sell, so to figure out her weekly order, she has to go through how much of each smoothie she'll sell and each smoothie's recipe and put together these calculations. For your program, you'll read in information about several smoothie recipes and several stores expected sales for the week, and determine what raw ingredients (and how much of each) each store should order. Note: This assignment test dynamic memory allocation. Later in the write up, requirements will be given stating which memory needs to be dynamically allocated and freed. The Problem Given a list of possible smoothie ingredients, a list of smoothie recipes, and lists of sales from several stores, determine how much of each ingredient each store must order. Input Specification The first line will contain a single positive integer, n (n ≤ 105), representing the number of possible smoothie ingredients. The following n lines will each contain a single string of in between 1 and 20 characters (all letters, digits or underscores). The ith (0 ≤ i ≤ n-1) of these will contain the name of the ith smoothie ingredient. (Thus, the ingredients are numbered from 0 to n-1.) The next line of input will contain a positive integer, s (s ≤ 105), representing the number of different smoothie recipes. (Use the same numbering convention for the recipes. The recipes are numbered from 0 to s-1.) The next s lines will contain the smoothie recipes, one per line. Each of these lines will be formatted as follows: m I1 R1 I2 R2 … Im Rm m represents the number of different ingredients in the smoothie (1 ≤ m ≤ 100) I1 represents the ingredient number of the first ingredient (0 ≤ I1 < n)="" r1="" represents="" the="" number="" of="" parts="" (ratio)="" of="" the="" first="" ingredient="" (1="" ≤="" r1="" ≤="" 1000)="" in="" the="" smoothie="" recipe="" the="" rest="" of="" the="" i="" and="" r="" variables="" represent="" the="" corresponding="" information="" for="" the="" rest="" of="" the="" smoothie="" ingredients.="" for="" example,="" if="" strawberries="" were="" ingredient="" 7,="" bananas="" ingredient="" 3,="" kiwis="" ingredient="" 6="" and="" yogurt="" was="" ingredient="" 0,="" then="" the="" following="" line="" would="" store="" a="" recipe="" for="" the="" strawberry="" breeze="" previously="" mentioned:="" 4="" 7="" 2="" 3="" 1="" 6="" 1="" 0="" 2="" the="" following="" line="" of="" input="" will="" contain="" a="" single="" positive="" integer,="" k="" (1="" ≤="" k="" ≤="" 100),="" representing="" the="" number="" of="" stores="" making="" orders="" for="" smoothie="" ingredients.="" (number="" the="" stores="" 1="" to="" k,="" in="" the="" order="" they="" appear="" in="" the="" input.)="" the="" last="" k="" lines="" of="" input="" will="" contain="" each="" store's="" order,="" one="" order="" per="" line.="" each="" of="" these="" lines="" will="" be="" formatted="" as="" follows:="" r="" s1="" w1="" s2="" w2="" …="" sr="" wr="" r="" represents="" the="" number="" of="" different="" smoothies="" the="" store="" offers="" (1="" ≤="" r="" ≤="" s)="" s1="" represents="" the="" smoothie="" number="" of="" the="" first="" smoothie="" (0="" ≤="" s1="">< s)="" w1="" represents="" the="" weight="" sold="" of="" the="" first="" smoothie="" (1="" ≤="" w1="" ≤="" 1000),="" in="" pounds.="" the="" rest="" of="" the="" s="" and="" w="" variables="" represent="" the="" corresponding="" information="" for="" the="" rest="" of="" the="" smoothie="" ingredients.="" note:="" the="" sum="" of="" all="" the="" number="" of="" different="" smoothies="" all="" the="" stores="" offer="" won't="" exceed="" 106.="" output="" specification="" the="" output="" of="" the="" program="" must="" be="" written="" to="" out.txt="" file="" with="" exact="" same="" format="" as="" specified="" in="" the="" sample="" output.="" the="" output="" also="" needs="" to="" be="" displayed="" in="" the="" standard="" output="" console.="" for="" each="" store="" order,="" print="" the="" following="" header="" line:="" store="" #x:="" where="" x="" is="" the="" 1-based="" number="" of="" the="" store.="" this="" will="" be="" followed="" by="" a="" list="" of="" each="" ingredient="" the="" store="" must="" order="" and="" the="" amount="" of="" that="" ingredient="" (in="" pounds),="" rounded="" to="" 6="" decimal="" places.="" the="" ingredients="" must="" be="" listed="" in="" their="" numeric="" order="" (order="" in="" the="" input),="" one="" ingredient="" per="" line,="" but="" instead="" of="" printing="" out="" the="" number="" of="" the="" ingredient,="" print="" out="" its="" name.="" for="" example,="" in="" the="" previously="" listed="" example,="" if="" a="" store="" only="" sold="" 18="" pounds="" of="" the="" strawberry="" breeze="" and="" the="" numbers="" of="" the="" ingredients="" were="" 7="" -="" strawberry,="" 3="" -="" banana,="" 6="" -="" kiwi,="" and="" 0="" -="" yogurt,="" then="" the="" corresponding="" output="" (minus="" the="" header="" line)="" would="" be:="" yogurt="" 6.000000="" banana="" 3.000000="" kiwi="" 3.000000="" strawberry="" 6.000000="" thus,="" the="" format="" of="" each="" line="" is:="" ingredient_name="" weight_to_order="" with="" weight="" to="" order="" rounded="" to="" exactly="" 6="" decimal="" places.="" print="" blank="" lines="" between="" each="" store.="" the="" last="" part="" of="" the="" output="" should="" have="" two="" blank="" lines.="" example="" input="" and="" output="" with="" explanation="" (note="" that="" you="" should="" prepare="" more="" test="" cases="" and="" test="" your="" code="" with="" more="" inputs)="" example="" in.txt="" file="" 8="" ingredients.="" sequence="" starts="" from="" 0="" yogurt="" sequence="" 0="" chocolate="" 1="" raspberry="" 2="" banana="" 3="" mango="" 4="" spinach="" 5="" kiwi="" 6="" strawberry="" 7="" 3="" recipes="" 4="" 7="" 2="" 3="" 1="" 6="" 1="" 0="" 2//="" recipe#0="" strawberry="" breeze="" has="" 4="" ingredients.="" (ingredient="" number#,="" unit="" or="" ratio),="" …...="" 3="" 0="" 2="" 5="" 1="" 4="" 2="" recipe#1="" veggie="" mango="" has="" 3="" ingredients="" (yogurt="" 2="" unit,="" spinach="" 1="" unit,="" mango="" 4="" units)="" 4="" 0="" 2="" 4="" 4="" 3="" 1="" 1="" 2="" recipe#2="" fruity="" chocolate="" has="" 4="" ingredients="" 3="" stores.="" sequence="" starts="" from="" 1="" 2="" 1="" 10="" 2="" 20="" store="" 1="" offers="" 2="" different="" recipes="" (recipe="" number,="" total="" unit="" in="" pounds)="" 1="" 0="" 18="" store="" two="" offers="" 1="" recipe="" (strawberry="" breeze="" 18="" lbs="" (there="" was="" a="" typo="" in="" the="" last="" version)="" 3="" 0="" 10="" 1="" 5="" 2="" 15="" store="" three="" offers="" 3="" recipes="" example="" output="" (out.txt="" file="" as="" well="" as="" to="" the="" standard="" output)="" store="" #1:="" yogurt="" 8.444444="" chocolate="" 4.444444="" banana="" 2.222222="" mango="" 12.888889="" spinach="" 2.000000="" store="" #2:="" yogurt="" 6.000000="" banana="" 3.000000="" kiwi="" 3.000000="" strawberry="" 6.000000="" store="" #3:="" yogurt="" 8.666667="" chocolate="" 3.333333="" banana="" 3.333333="" mango="" 8.666667="" spinach="" 1.000000="" kiwi="" 1.666667="" strawberry="" 3.333333="">
Calculation hints: Here is an example calculation of the numbers for store #1 Store#1 offers 2 types of recipes. They want to make 10LBs of recipe#1 and 20 LBs or recipe#2 Based on recipe#1 we need the following ingredients for Store#1: Yogurt: (2 * 10)/(2+1+2) = 4 LBs Chocolate: 0LB Raspberry: 0LB Banana: 0LB Mango: (2 * 10)/(2+1+2) = 4 LBs Spinach: (1 * 10)/(2+1+2) = 2 LBs kiwi 0LB strawberry 0LB Based on recipe#2 we need the following ingredients for Store#1 : Yogurt: (2 * 20)/(2+4+1+2) = 4.444444 LBs Chocolate: : (2 * 20)/(2+4+1+2) = 4.444444 LBs Raspberry: 0LB Banana: : (1 * 20)/(2+4+1+2) = 2.222222 LBs Mango: (4 * 20)/(2+4+1+2) = 8.888889 LBs Spinach: 0LB kiwi 0LB strawberry 0LB So, the total amount of non zero ingredients needed for store#1 yogurt 8.444444 ( calculated from 4 + 4.444444) chocolate 4.444444 ( calculated from 0 + 4.444444) banana 2