206 Lab G - Struct and Dynamic memory

pdf

School

McGill University *

*We aren’t endorsed by this school

Course

206

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

2

Uploaded by CaptainGooseMaster868

Report
Lab G Software Systems McGill Vybihal Dynamic Memory Page 1 of 2 Dynamic Memory Lab G TA teaching instructions can be found at the end of this document. The labs, for this course, are designed to be completed on your own at home or in the 3 rd floor Trottier labs. These labs are not graded. You do not hand in these labs. If you prefer to work on a lab with your TA tutorial group, then check the schedule for your TA ’s tutorial session. You will find this schedule in our MyCourses page under Content/Course Information/TA Information. Since the university has limited lab space, your TA might ask you to bring your laptop and work in a classroom instead of a lab. This lab is about programming with struct / union and malloc / calloc. Some labs will have a question zero. These questions will not be covered by the TA during the tutorial. It is extra content meant for you to do on your own. QUESTION ZERO: Optional problem If you like, review these online resources: 1. Struct and union: https://www.programiz.com/c-programming/c-structure-examples 2. Dynamic memory: https://www.programiz.com/c-programming/c-dynamic-memory-allocation QUESTION ONE: Struct and Union Given the following structure: struct BANK_ACCOUNT { char type; // ‘S’=savings, ‘C’=checking double balance; union ACCOUNT_SPECIFIC { double charge; // for withdrawal from savings accounts int credit_score; // for checking account } specific; }; Do the following: a) Create a single source file called bank.c and put into that file the above structure and a main() function. b) Create a local array, called accounts, in main(), of 100 cells, type BANK_ACCOUNT. Use this pattern: struct BANK_ACCOUNT accounts[100]; c) Create a local integer variable, called nextSpot , and initialize it to zero. This variable will be used to track where in the array accounts the next account will be created. In other words, to the left of nextSpot are array cells with account information. At nextSpot and to the right, those cells are not used. d) Prompt the user for the number of accounts they would like to create. Store that in an integer variable called newAccounts .
Lab G Software Systems McGill Vybihal Dynamic Memory Page 2 of 2 e) Using a for-loop with the newAccounts variable, nextSpot , and the array accounts , prompt the user for the information to input into the bank accounts. You will need to first ask the user the type of account, then the remaining fields depending on the account type. f) The program ends by displaying all the cells of the array from 0 to nextSpot , to confirm that the information was input correctly. NOTE : Sample code is provided showing one way you might program QUESTION ONE. I suggest you try doing question one on your own before looking at the sample code. The TA will review the sample code during their mentoring time. QUESTION TWO: Dynamic Memory Redo Question One, but replace the array with: struct BANK_ACCOUNT *accounts; Your are still building an array, so use calloc() to allocate n cells, for example: calloc(n,sizeof(struct BANK_ACCOUNT)); The program must prompt the user for the number n. Then modify all the array references to pointer references. NOTE : since the array you calloc d is contiguous, you can still use the array notation. Experiment with the program using both array notation and pointer notation. Call your source file Bank2.c. You have completed your lab. TA Teaching Instructions During this 60-minute lab do the following with the students: Ask the students if they would like you to review struct and union. Review the basic syntax of programming with malloc() and calloc(). Review the example code provided in this lab for question 1. Do question 2 together with the students. Show them how using calloc() allows the students to continue using array notation. Show how they can do question 2 with pointer notation. If time permits: Talk about why we must use pointer notation when using linked lists. Why does array notation fail?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help