Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C program :Your program will print all Cube Sums with integers 0 . . N in order from smallest to largest. ( Notice there was

C program :Your program will print allCube Sumswith integers 0.. N in order from smallest to largest. (Notice there was a tie above, figure out how it was broken.) You cannot generate all values and sort them. This would require making (N+1)^2 values, which would use excessive space. Instead you will use a Heap to generate them in order.
The basic algorithm is described below.
Create a new Heap
For integers i=0 up to N do
Add (i^3+0^3, i,0) to the Heap
End For
While Heap is not Empty do
Get the Minimum Sum
Print the Sum
If the sum's j value is less than N:
Put a new sum in the heap (i^3+(j+1)^3, i, j+1)
End If
End Do
Delete the Heap
You may make a one assumptions.
* The user will always give you a positive integer as input
You must implement the following in your solution.
You must use the following Structure to store theCube Sums.
/**
Stores a sum i^3+ j^3= k
*/
typedef struct CubeSum CubeSum;
struct CubeSum{
int sum;/**< result of i^3+ j^3*/
int i;/**< First integer in sum */
int j;/**< Second integer in sum */
};
You must implement a Heap withat leastthe following methods. You may create additional methods, but youmustuse these.
/**
Create a new heap with starting size 10.
@return the heap that was created.
*/
Heap *newHeap();
/**
Delete the heap. Free all memory used.
@param myHeap is the heap to delete
*/
void deleteHeap(Heap *myHeap);
/**
Determine if the heap is empty.
@param myHeap is the heap to check
@return true if the heap is empty and false otherwise
*/
bool isEmpty(Heap *myHeap);
/**
Insert a new sum into the heap.
@param myHeap is the heap to insert into
@param sum is a pointer to the sum to insert
*/
void insert(Heap *myHeap, CubeSum* sum);
/**
Remove the minimum from the heap.
@param myHeap is the heap to remove from
*/
void removeMin(Heap *myHeap);
/**
Get the pointer to the smallest CubeSum.
@param myHeap is the heap to get the min of
@return a pointer to the smallest CubeSum
*/
CubeSum *getMin(Heap *myHeap);
Example 1
Cube Sum Program.
Computes Cube Sums from 0... N
Enter max value of N:
2
0^3+0^3=0
0^3+1^3=1
1^3+0^3=1
1^3+1^3=2
0^3+2^3=8
2^3+0^3=8
1^3+2^3=9
2^3+1^3=9
2^3+2^3=16

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

Students also viewed these Databases questions