Description
This assignment focuses on dynamic memory allocation.
Your code must compile without any warnings or errors and run without segmentation
faults to receive credit. Points will be taken for inconsistent formatting.
1. Create a contains 10 functions which accomplish the subtasks described below. You
must use dynamic memory allocation. No static arrays will be accepted.
DO NOT FORGET TO FREE ANY ALLOCATED MEMORY!
(a) Allocate space for a single integer and set the value to 10.
(b) Allocate space for an integer array of size 100 and initialize the values to 0.
(c) Allocate space for a 2D integer array of size (20, 20) and initialize the values
to 0.
(d) Allocate space for a 3D integer array of size (20, 20, 20) and initialize the values
to 0.
(e) Allocate space for a double and set the value to 0.5.
(f) Allocate space for a double array of size 200 and initialize the values to 0.
(g) Allocate space for a 2D double array of size (40, 40) and initialize the values
to 0.
(h) Allocate space for a char and set the value to A.
(i) Allocate space for an char array of size 1024 and initialize the values to 0.
(j) Allocate space for a 2D char array of size (512, 512) and initialize the values
to 0.
Save your code as problem1.c.
2. Create a program which takes two integers as input from the command line. In your
code, allocate space for a 2D array dependent on user input. For example, if they
enter 20 40 then your program should allocate space using double **.
Save your code as problem2.c.
3. Create a program that reads in flight data from a raw CSV file. Your program should
determine how many entries are in the file and allocate the appropriate amount of
memory. Each entry should be stored in a struct which matches the flight data
given. After the data is read and parsed, print the file contents in a formatted table
as seen in the example run. A sample file can be found on Canvas.
Requirements
CSE 1320: Assignment 7 Dillhoff
• Your Flight struct should be declared in a header file with a header guard.
• Read in file name from command line.
• Check that the file exists.
• List all contents as seen in the example run.
• Free any memory allocated.
Flight Data
• Flight ID char array of size 8
• Departure Code char array of size 4
• Destination Code char array of size 4
• Departure Time int
• Arrival Time int
Example Run
FLIGHT DCODE ACODE DTIME ATIME
SW1234 LAX DFW 100 300
AA9938 LGA DAL 300 500
Your code should be saved as problem3.h and problem3.c.
4. Create a program that allows the user to add strings to an array of strings (char **)
as well as the option to remove a string by name.
Requirements
• Create a menu with four options as seen below.
• The user should remain in the program until they choose to exit.
• When adding a string, use the prompts as seen in the example below. Trailing
newline characters should be removed.
• When removing a string, the user should type the string they want to remove.
You are to create a function which searches your array of strings for that string.
If it is found, it is removed and the array of strings is reallocated. In either
case, a statement should notify the user of the result (see example).
• When listing strings, print each string on a new line.
• Make sure to free any allocated memory!
Example Run
1. Add String
2. Remove String
3. View Strings
4. Exit
> 1
Enter string: test
test added!
1. Add String
2
CSE 1320: Assignment 7 Dillhoff
2. Remove String
3. View Strings
4. Exit
> 3
test
1. Add String
2. Remove String
3. View Strings
4. Exit
> 2
Enter string: test1
test1 not found.
1. Add String
2. Remove String
3. View Strings
4. Exit
> 2
Enter string: test
test removed!
1. Add String
2. Remove String
3. View Strings
4. Exit
> 3
No strings!
1. Add String
2. Remove String
3. View Strings
4. Exit
> 4
Save your code as problem4.c.
5. Create a program which collects a list of library books. The user should be able
to add as many book as they wish and view the collection through a menu. The
collection should be sorted by year published (newest first).
Book Struct
typedef struct {
char *name;
char *author;
int year_published;
}
Requirements
• The Book struct should be defined in a header file with a header guard.
• The user should stay in the program until they choose to exit.
3
CSE 1320: Assignment 7 Dillhoff
• No static memory for the Books should be used. It should be added dynamically as the user enters each one.
• After each book is added by the user, the array of Books should be sorted using
qsort().
• Make sure to free your memory.
Example Run
1. Add Book
2. View Books
3. Quit
> 1
Enter name: The Hobbit
Enter author: J.R.R. Tolkien
Enter year published: 1937
1. Add Book
2. View Books
3. Quit
> 2
1937 – The Hobbit by J.R.R. Tolkien
1. Add Book
2. View Books
3. Quit
> 1
Enter name: Leviathan Wakes
Enter author: James S. A. Corey
Enter year published: 2011
1. Add Book
2. View Books
3. Quit
> 2
2011 – Leviathan Wakes by James S. A. Corey
1937 – The Hobbit by J.R.R. Tolkien
1. Add Book
2. View Books
3. Quit
> 3
Save your code as problem5.h and problem5.c.
Create a zip file using the name template LASTNAME_ID_A7.zip which includes the all
required code files. Submit the zip file through Canvas.
4