ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
8 marks · Data types and structures
Using a programming language of your choice, declare a variable that can store the data for 750 recipes. Your answer should include the record data structure defined above. Users want to search for recipes by entering the main ingredient and the maximum cooking time (in minutes). The titles and cooking times of any matching recipes should be displayed. For example: Inputs: Main ingredient: Pasta Maximum cooking time: 60 Output: Lasagne requiring 45 minutes. Macaroni Cheese requiring 25 minutes. … If no recipes match the criteria the following message is displayed. ‘No matches found.’ Using a programming language of your choice, write the code to display this information. Your answer should use the record data structure provided. Recipe Finder is an app which allows users to search a list of stored recipes and displays the matching recipes. The app stores the following details about each recipe: • recipe title • main ingredient • cooking time (in minutes) • cost per portion • average rating (out of 5). The app uses the record data structure shown below for the recipe data. RECORD Recipe IS {STRING title, STRING ingredient, INTEGER minutes, REAL cost, REAL avgRating}
• array of 750 elements. • use of record structure (Recipe) Allow indexing from 1 to 750. Example answers: DECLARE allRecipes AS ARRAY OF Recipe[]*750 DECLARE allRecipes AS ARRAY OF Recipe INITIALLY[NULL]*750 Award 1 mark for allRecipes=[Recipe()]*750 allRecipes=[Recipe]*750 Python allRecipes = [Recipe() for x in range(750)] VB Dim allRecipes(749) as Recipe (accept 750) Java Recipe[] allRecipes = new Recipe[750]; • initialisation of found flag/result and update • condition = search ingredient and <= searchTime using an array • array name matching to (a) • use of field names in IF condition (ingredient,minutes) • display message with two values inside loop that traverses entire array • single output of no match displayed after the loop If incorrect field names are used, only penalise once for bullets 2,4 and 5 If parallel arrays are used do not award bullets 3 and 4 At bullet 2 do not penalise if parallel arrays are used Example answer: SET Found TO False RECEIVE searchIngred FROM KEYBOARD RECEIVE searchTime FROM KEYBOARD FOR n FROM 0 TO LEN(allRecipes) DO IF allRecipes[n].ingredient = searchIngred AND allRecipes[n].minutes <= searchTime THEN SEND allRecipes[n].title &” requires “ & allRecipes[n].minutes & “ minutes.” TO DISPLAY Found = True END IF END FOR IF Found =FALSE SEND “No matches for that search, try again.” TO DISPLAY END IF