# Artuino Assembly Code UCLA

**Question description**

ARDUINO CODE

Problem 1.

Write a program largeHalf.c that takes in a list of 8 integers, splits the list into two halves (first 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 first 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.

EXAMPLE:

(˜)$ 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

(˜)$

Problem 2.

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 first 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 first 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 arrays

string1 and string2.

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

EXAMPLE:

(˜)$ ./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

Problem 3.

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. The figure below provides

an illustration of integration and the approximation methods you will be implementing.

...

...

f(t)

t

t=a t=b

Δt

...

...

f(t)

t

t=a t=b

f(a+Δt/2)

Δt

...

...

f(t)

t

t=a t=b

f(a+Δt) f(a+2*Δt)

∫f(t)dt

a

b ∫f(t)dt

a

b

Rectangle Approx of ∫f(t)dt

a

b

Trapezoid Approx of

0 2

1

In the figure 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 figure, 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 figure (green) demonstrates the rectangle method for n = 4. The width of the rectangle is

4t = (b

## 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