Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming There are N candy boxes, and each box has Ai candy. The candy box will eventually be combined into one, combining only two

C Programming There are N candy boxes, and each box has Ai candy. The candy box will eventually be combined into one, combining only two boxes at a time and combining the two boxes with the largest number of candy in each step. If the number of boxes is an odd number, two of them are combined except one box with the smallest number of candies. For example, given N = 6 and 1, 2, 3, 4, 5, 6, the first stage is 11,7,3, the second stage is 18, and the third stage is 21. Exit because the boxes are merged into one. (** Candy boxes are given in ascending order based on the number of candies **)

Input: The first line is given the integer N (2

If N is 6, the number of candy boxes is 6, so malloc is used to assign a dynamic array of size 6. Then, when the number of candy in the candy box is given as 1,2,3,4,5,6, the first step is obtained as follows.

image text in transcribed

In the first step, when two pairs are paired, the number of candy boxes in the next step becomes three, so the pointer of the next step is allocated and stored as a dynamic array of size 3, and this process is repeated to produce a dynamic array of size 1 do.

image text in transcribed

Please help me.

Can't get the right answer when input three numbers.

Thanks in advance.

My code:

#include #include

void sort(int *a, int n)/*{{{*/ { int temp, i, j; for (i = 0; i

//if size is odd place last element of prev in last of next if (size % 2) { next_pointer[next_size - 1] = prev_pointer[size - 1]; int k = 0, i; for (i = 0; i

sort(next_pointer, next_size); } else { int k = 0, i; for ( i = 0; i

sort(next_pointer, next_size); }

free(prev_pointer); return next_pointer; } /*}}}*/

int main()/*{{{*/ { int n, i; scanf("%d", &n);

int *a = (int *)malloc(sizeof(int) * n); for (i = 0; i

int prev_size = n; int new_size = (n / 2) + (n % 2);

while (new_size >= 1) { a = get_next_process(a, prev_size); for (i = 0; i

Prev pointer Next pointen mi3

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 Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

=+8. How can you differentiate your product in their eyes?

Answered: 1 week ago

Question

7. General Mills

Answered: 1 week ago

Question

3. Describe the strategic training and development process.

Answered: 1 week ago