# c program using arrays

**Question description**

Problem1.

Write a program largeHalf.c that takes in a list of 8 integers, splits the list into two halves (ﬁrst 4 elements, last 4 elements), sums the elements in each half, selects the half with the larger sum, and then repeats this process with the selected list until there is only one element selected. The program should print out the half selected at each step. If the two halves have equal sum, the program should tell the user this and should always pick the ﬁrst half (the one with a smaller starting index, see the example output below).

Your program must include the following functions:

• int sumArray(int array[], int startIdx, int len) - Returns the sum of the elements in array with index startIdx to startIdx+len.

• void printArray(int array[], int startIdx, int len) - Print the elements in array with index startIdx to startIdx+len.

We suggest implementing and testing each of these functions before completing the full program.

(˜)$ a.out

Enter 8 numbers: 1 2 3 4 5 6 7 8

Larger half is 5 6 7 8

Larger half is 7 8

Larger half is 8

(˜)$ a.out

Enter 8 numbers: 1 2 3 4 5 4 1 0

The two halves are equal, picking 1 2 3 4

Larger half is 3 4

Larger half is 4

(˜)$ a.out Enter 8 numbers: 1 4 5 0 -10 10 1 2

Larger half is 1 4 5 0

The two halves are equal, picking 1 4

Larger half is 4

(˜)$

Problem2.

Write a program sortLen.c that takes in a set of user supplied words and sorts them by their length. Before taking in the words, the program will prompt the user for the total number of words they want to enter. The program will then prompt the user for each word, one by one. After taking in the words, the program will convert them all to lowercase. Then the program will sort the words by their length (from shortest to longest word). It will do so by using a simple sorting algorithm.

This algorithm works by repeatedly scanning through the list and checking if adjacent words are in the correct order (if the shorter word precedes the longer word). If the adjacent words are not in the correct order, it will swap them. After scanning through the list once, it is possible that not all words are sorted, for example, if we started with words of length 1 4 3 2, when we check the ﬁrst and second word, they have the appropriate order (1 < 4), so no swap is made. Then when we continue scanning, we see that the second and third words have lengths 4 and 3, respectively. Thus, we’ll swap these two words, and the lengths will now be 1 3 4 2. Next we need to check the third and fourth words, with lengths 4 and 2, and we’ll need to swap these words. After this scan of the list, the words have been reordered, so the lengths, in order, are now 1 3 2 4. The overall list of words is not yet sorted by length. However, if we repeatedly scan through the list in this manner until the algorithm does not need to swap any adjacent entries, then the whole list will be sorted.

Thus, the basic algorithm is as follows:

a. Scan through the list, starting from the ﬁrst word, comparing adjacent words. If the order of the two words is incorrect (if the longer word precedes the shorter word), swap them. Note: if the words are the same length, no swap is necessary.

b. Repeat step a until we can scan through the list without swapping any consecutive words. (At this point the list is sorted!)

Your program must include the following functions:

• void lowercaseString(char string[]) - Converts all of the letter characters in the array string into lowercase.

• int stringLen(char string[]) - Returns the length of the string array string.

• void swapStrings(char string1[], char string2[]) - Swaps the string ar- rays string1 and string2.

We suggest implementing and testing each of these functions before completing the full program.

(˜)$ ./a.out

How many words are you planning to enter? 3

Enter word 1: This

Enter word 2: is

Enter word 3: FUN

Sorted words:

is

fun

this

(˜)$ ./a.out

How many words are you planning to enter? 5

Enter word 1: Sorting

Enter word 2: is

Enter word 3: NOT

Enter word 4: too

Enter word 5: difficult

Sorted words:

is

not

too

sorting

difficult

(˜)$ ./a.out

How many words are you planning to enter? 4

Enter word 1: 1

Enter word 2: LAST

Enter word 3: example

Enter word 4: rUn

Sorted words:

1

run

last

example

Problem3.

Write a program integral.c that calculates rectangular and trapezoidal integral approximations for a function. Your program will ask the user for the starting and ending time-points of the integration and the number of time points (polygons) to use in the approximation. In addition to printing out the estimated integral, the program will provide the error in the approximation.

In the ﬁgure on the left, when we integrate a function we are taking the area under the curve between t = a and t = b (in blue), with the caveat that areas calculated for ranges of t for which f(t) < 0 are subtracted from areas calculated for ranges of t for which f(t) > 0. Thus, in the ﬁgure, the integral of f(t) over the range a to b is equal the blue area labeled 2 subtracted from the blue area labeled 1.

We can approximate this integral computationally (instead of using analytical integration for an exact answer, i.e. what you learn in calculus) by using a number of methods. Two straightforward methods of approximation are the rectangle and trapezoid methods. With these methods, the integration range is split into n segments and the area of these segments is approximated with a simple polygon. The estimated areas of all of the n segments are signed as either positive or negative (based upon whether they are above or below the t-axis) and are summed.

The center ﬁgure (green) demonstrates the rectangle method for n = 4. The width of the rectangle is 4t = (b − a)/n. The height of the rectangle is f(t) evaluated at the midpoint of the rectangle, so for the ﬁrst rectangle, the height is f(a + 4t/2). Thus, the area of the ﬁrst rectangle (with the appropriate +/- sign) is f(a + 4t/2) 4 t.

The right ﬁgure (yellow) demonstrates the trapezoid method for n = 4. Each trapezoid has two dif- ferent heights (h1 and h2) and one width (w). The width, w, is also 4t = (b − a)/n. The heights are found by evaluating f(t) at the vertical edges of the trapezoid. So for the ﬁrst trapezoid, heights are h1 = f(a) and h2 = f(a + 4t). For the second trapezoid the heights are h1 = f(a + 4t) and h2 = f(a + 2 ∗ 4t) (shown on ﬁgure). Thus, the area of each trapezoid (with the appropriate +/- sign) is (h1 + h2) ∗ w/2

For this program you will calculate these approximations for the function f(t) = t3 − 2t2 − 10t + 10, you’ll ask the user for the integral range (a and b) and the number of polygons (n) to use. To calculate the error, evaluate the analytical integral of f(t) and subtract each approximation from it. The integral is: R b a f(t)dt = (b4 4 − 2b3 3 − 10b2 2 + 10b) − (a4 4 − 2a3 3 − 10a2 2 + 10a) (plug in a and b to calculate the integral over the range a to b). Your program must include the following functions:

• double calcTrap(double t, double dt) - Calculates and returns the area of the trapezoid between t and t+dt.

• double f(double t) - Calculates and returns the value of f(t) evaluated at t and might be useful for implementing the above functions!

• double fIntegral(double a, double b) - Calculates and returns the integral of f(t) over the range a to b using the analytical integral described above (used to check the error in the approximations).

We suggest that you build and debug the basic functionality of these functions before using them to complete the full program. Example execution:

(˜)$ a.out

Enter the integral range (a,b): 0,1

Enter the number of polygons to use: 1

Rectangle Estimate of 4.625000 is off by 0.041667

Trapezoidal Estimate of 4.500000 is off by -0.083333

(˜)$ a.out Enter the integral range (a,b): 0,1

Enter the number of polygons to use: 10

Rectangle Estimate of 4.583750 is off by 0.000417

Trapezoidal Estimate of 4.582500 is off by -0.000833

(˜)$ a.out Enter the integral range (a,b): -1,1

Enter the number of polygons to use: 100

Rectangle Estimate of 18.666800 is off by 0.000133

Trapezoidal Estimate of 18.666400 is off by -0.000267

(˜)$ a.out Enter the integral range (a,b): 5,7

Enter the number of polygons to use: 100

Rectangle Estimate of 198.665600 is off by -0.001067

Trapezoidal Estimate of 198.668800 is off by 0.002133

(˜)$ a.out Enter the integral range (a,b): 10,100

Enter the number of polygons to use: 10000

Rectangle Estimate of 24282899.900980 is off by -0.099020

Trapezoidal Estimate of 24282900.198047 is off by 0.198047

## Tutor Answer

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors