Starting from:

$24.99

CIS2500 Lab Assignment 2 Solution


1. Create function double rand_double(double a, double b) that produces a random number between a and b inclusive
• The formula is: ((double) rand() / (double) MAX_INT) * (b – a) + a assuming b > a
• If b < a swap the values of a and b then call the above formula
• You need to #include <stdlib.h> to use the rand function

2. Create a structure foobarbaz that holds an int called foo and a double called bar and a second int called baz

3. Create a function with no arguments called rand_foobarbaz() that produces a foobarbaz struct using dynamic memory
• foo is initialized to a random value between 0 and 49,
• bar is initialized to a random value between 0.0 and 100.0
• baz is initialized to a random value between 50 and 99
• rand_foobarbaz() should return the pointer to this struct

4. Create a function with no arguments called many_foobarbaz()
• This function should produce an array of 20 foobarbaz structures using dynamic memory, and return it as a pointer.

5. Create a function that takes an array of foobarbaz struct pointers and prints them out, one structure per line
• Make sure it is appropriately formatted so that the foo, bar and baz values line up vertically

6. Create a function that takes a foobarbaz array and two integers, where each integer is less than the number of foobarbaz structures in the array, and swaps the two structures being pointed at

7. Create a main() that performs the following:
• Creates a foobarbaz array
• Prints the foobarbaz array
• Randomly creates two swap points
• Prints them on their own line with blank lines above and below
• Swaps the foobarbaz values at the two indices in the foobarbaz array • Prints out the foobarbaz array again

Make sure you free all malloced memory before exiting the program.

More products