Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. [80] Write a C program that defines the following struct: struct IntArray int lengthi int dataPtr and the following functions: (10) struct IntArray* mallocintArray(int
1. [80] Write a C program that defines the following struct: struct IntArray int lengthi int dataPtr and the following functions: (10) struct IntArray* mallocintArray(int length): allocates, initializes, and returns a pointer to a new struct IntArray. Hint: see example from class-also, you'll need two malloc calls, one for the instance and one for the instance's dataPtr (a pointer to an int array of size length). (10) void freelntArray(struct IntArray *arrayPtr): frees the instance's dataPtr and frees the instance (10) void readintArray(struct IntArray *array): prompts and reads positive ints from the user to fill the array (i.e., read one int for each array index). Your program should print an error message and prompt again if the user enters a value that cannot be parsed as a positive int. Hint: I recommend using fgets and strtol-you can Google for examples of these (cite your sources) and we'll cover them in labs. Hint: strategically, you might consider skipping the validation initially and coming back to it once the remainder of the program is functional. (15) void swap(int *xp, int *yp): swaps the int values stored at the xp and yp pointers (15) void sortintArray(struct IntArray *array): sorts the input array in ascending order using Bubble Sort (Google it, cite your sources) by repeatedly calling your swap function as appropriate (10) void printintArray(struct IntArray "array): prints the array (e.g., " 1, 3, 5,7]"). (10) int main(): prompt the user to input a positive int length from the user (print an error message and prompt again if the input cannot be parsed as a positive int), call mallocintArray to create an array, call readintArray to prompt the user to fill the array call sortintArray to sort it, call printArray to print the resulting sorted array, then call freelntArray to free the heap memory used by the array Name your source file 3-1.c Here is output from a sample run of the application (your output does not need to match exactly) Enter length: cat Invalid input Enter length: 5 Enter int:3 Enter int: puppy Invalid input Enter int: 7 Enter int: 5 Enter int: 8 Enter int: 2 2, 3, 5, 7,8
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started