I wrote this code in c programming.  what this code should do is ask how many times its going to run and run that many times(this works), ask how many books there are in a place(this works), then ask the max number of pages you want to read(this works), then asks how many pages there are in a book and put that into an array(works), then sorts the array of pages(works), and then adds the number in the array until you cant which is less than or equal to the max number of pages you want to read, and puts how many books you read(does not work), and then prints out how many books you read.   a sample input is 3 5 20 6 12 3 10 2 5 21 12 3 6 10 2 10 31 9 6 6 3 8 2 12 15 13 7  sample out put should be 3 4 5    #include #include void MergeSort(int values[], int start, int end); void Merge(int values[], int start, int middle, int end); void add(int pages[], int count[], long long maxPages, long long total); void Print_Array(int count[], int cases); int main(void) { int *pages; int *count; int numBooks, books, cases; longlong total = 0, maxPages; scanf("%d", &cases); count = malloc(cases * sizeof(int)); for(int c; c != cases; c++) { scanf("%d", &numBooks); pages = malloc(numBooks * sizeof(int)); scanf("%lld", &maxPages);   for(int i = 0; i < numBooks; i++) { scanf("%d",&books); pages[i] = books; }   MergeSort(pages, 0, numBooks-1); add(pages, count, maxPages, total); } Print_Array(count, cases); return0; } void MergeSort(int values[], int start, int end) { int mid;   // Check if our sorting range is more than one element. if (start < end) { mid = (start+end)/2;   // Sort the first half of the values. MergeSort(values, start, mid);   // Sort the last half of the values. MergeSort(values, mid+1, end);   // Put it all together. Merge(values, start, mid+1, end); } } void Merge(int values[], int start, int middle, int end) { //printf("merge %d, %d, %d\n", start, middle, end);   int *temp, i, length, count1, count2, mc;   // Allocate the proper amount of space for our temp array. length = end - start + 1; temp = (int*)calloc(length, sizeof(int)); // These will be our indexes into our two sorted lists. count1 = start; count2 = middle;   // Keeps track of our index into our temp array. mc = 0; // Here we copy values into our temp array, so long as there are // numbers from both lists to copy. while ((count1 < middle) || (count2 <= end)) { // Next value to copy comes from list one - make sure list // one isn't exhausted yet. Also make sure we don't access index // count2 if we aren't supposed to. if (count2 > end || (count1 < middle && values[count1] < values[count2])) { temp[mc] = values[count1]; count1++; mc++; }   // We copy the next value from list two. else { temp[mc] = values[count2]; count2++; mc++; } } // Copy back all of our values into the original array. for (i=start; i<=end; i++) values[i] = temp[i - start]; // Don't need this space any more! free(temp); } void add(int pages[], int count[], long long maxPages, long long total) { int i = 0, c = 0; for(int z = 0; total <= maxPages; z++) { total += pages[z]; c++; } count[i] = c; i++; } void Print_Array(int count[], int cases) { int i; for (i=0; i

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I wrote this code in c programming

what this code should do is ask how many times its going to run and run that many times(this works), ask how many books there are in a place(this works), then ask the max number of pages you want to read(this works), then asks how many pages there are in a book and put that into an array(works), then sorts the array of pages(works), and then adds the number in the array until you cant which is less than or equal to the max number of pages you want to read, and puts how many books you read(does not work), and then prints out how many books you read.

 

a sample input is

3
5 20
6 12 3 10 2
5 21
12 3 6 10 2
10 31
9 6 6 3 8 2 12 15 13 7 

sample out put should be

3
4

 

#include <stdio.h>
#include <stdlib.h>


void MergeSort(int values[], int start, int end);
void Merge(int values[], int start, int middle, int end);
void add(int pages[], int count[], long long maxPages, long long total);
void Print_Array(int count[], int cases);


int main(void)
{
int *pages;
int *count;
int numBooks, books, cases;
longlong total = 0, maxPages;

scanf("%d", &cases);
count = malloc(cases * sizeof(int));

for(int c; c != cases; c++)
{

scanf("%d", &numBooks);
pages = malloc(numBooks * sizeof(int));

scanf("%lld", &maxPages);
 
for(int i = 0; i < numBooks; i++)
{
scanf("%d",&books);
pages[i] = books;
}
 
MergeSort(pages, 0, numBooks-1);

add(pages, count, maxPages, total);
}

Print_Array(count, cases);

return0;
}


void MergeSort(int values[], int start, int end) {

int mid;
 
// Check if our sorting range is more than one element.
if (start < end) {

mid = (start+end)/2;
 
// Sort the first half of the values.
MergeSort(values, start, mid);
 
// Sort the last half of the values.
MergeSort(values, mid+1, end);
 
// Put it all together.
Merge(values, start, mid+1, end);
}
}


void Merge(int values[], int start, int middle, int end) {

//printf("merge %d, %d, %d\n", start, middle, end);
 
int *temp, i, length, count1, count2, mc;
 
// Allocate the proper amount of space for our temp array.
length = end - start + 1;
temp = (int*)calloc(length, sizeof(int));

// These will be our indexes into our two sorted lists.
count1 = start;
count2 = middle;
 
// Keeps track of our index into our temp array.
mc = 0;

// Here we copy values into our temp array, so long as there are
// numbers from both lists to copy.
while ((count1 < middle) || (count2 <= end)) {

// Next value to copy comes from list one - make sure list
// one isn't exhausted yet. Also make sure we don't access index
// count2 if we aren't supposed to.
if (count2 > end || (count1 < middle && values[count1] < values[count2])) {
temp[mc] = values[count1];
count1++;
mc++;
}
 
// We copy the next value from list two.
else {
temp[mc] = values[count2];
count2++;
mc++;
}
}

// Copy back all of our values into the original array.
for (i=start; i<=end; i++)
values[i] = temp[i - start];

// Don't need this space any more!
free(temp);
}

void add(int pages[], int count[], long long maxPages, long long total)
{
int i = 0, c = 0;
for(int z = 0; total <= maxPages; z++)
{
total += pages[z];
c++;
}
count[i] = c;
i++;
}

void Print_Array(int count[], int cases) {

int i;
for (i=0; i<cases; i++)
printf("%d ", count[i]);
printf("\n");
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education