Problems
Using the insertion sorting method, sort the fruits in the table shown here by hand, from least to most expensive.
| Fruit | Price |
|---|---|
| Apple | $\(1.50\) |
| Orange | $\(1.00\) |
| Banana | $\(0.50\) |
| Pineapple | $\(3.00\) |
| Pear | $\(0.75\) |
To sort these fruits, we start start with the order as shown. The first step is to compare the price of the first two fruits. Since apple is more expensive than the orange, the order of these two fruits should be swapped resulting in a list:
| Fruit | Price |
|---|---|
| orange | $\(1.00\) |
| apple | $\(1.50\) |
| banana | $\(0.50\) |
| pineapple | $\(3.00\) |
| pear | $\(0.75\) |
With the first two fruits sorted we move on to the third fruit, the banana. Since the price of the banana is less than the price of the apple, the banana should come before the apple. As the banana is also less than the orange, it should also come before the orange. This results in the following list:
| Fruit | Price |
|---|---|
| banana | $0.50 |
| orange | $1.00 |
| apple | $1.50 |
| pineapple | $3.00 |
| pear | $0.75 |
The next fruit is the pineapple. As the pineapple is more than the apple, no changes are necessary. The final fruit is the pear. As the pear is less than the pineapple, it should come before it. It is also more than both the apple and banana, so it should also come before them. Therefore, the final sorted list is:
| Fruit | Price |
|---|---|
| banana | $0.50 |
| pear | $0.75 |
| orange | $1.00 |
| apple | $1.50 |
| pineapple | $3.00 |
The code here prints a string. What string is printed, and why? Assume that a and b are allocated to sequential blocks of memory. Note the number of characters allocated for the character array a.
char a[3] = "an ";
char b[] = "error";
printf("%s\n", a);This code prints an error. The reason that an is not printed is that all strings in C should be terminated with a null character. As the space allocated for the character array a is 3, there is no room for a null character. Therefore, when the printf() function starts reading the character array a, it doesn't read a null character until the end of character array b since character arrays a and b are located sequentially in memory.
The code here prints a series of numbers when run. What numbers are printed? Why?
int a[] = {1, 5};
int *b = a;
printf("%d\n", *b);
printf("%d\n", ++*b);
printf("%d\n", (*b)++);
printf("%d\n", *b);
printf("%d\n", *b++);
printf("%d\n", *b);This code does the following.
The first print statement prints
1. This is becausebis a pointer to the beginning of arraya. As the first entry inais1and the pointer is dereferenced,1is printed.The second print statement prints
2. This is because the pointerbwhich points to the beginning of the arrayais dereferenced, then the prefix increment operator increments it. The operators are applied in this order because they are both at the same precedence level and their associativity is right-to-left. It is important to note that the first entry inais now2.The next print statement prints
2. This is because the code inside the parenthesis is executed first, dereferencingb. Asbpoints to the start of arrayathe dereferenced value is2. The postfix increment is then applied to this value. Therefore2is printed, and after printing the first value in the arrayais3.The following print statement prints
3. This is because the first value in the arrayais now3.The second to last print statement prints
3. This is because the postfix increment is applied because it is at the highest precedence level. However, since it is a postfix increment, nothing is changed yet. Next, the dereference operator is applied and sincebpoints to the first entry in arraya,3is printed. After the print statement,bis incremented so it now points to the second entry ina.The last print statement prints
5. This is becausebnow points to the second value in arraya.
Your colleague is working on optimizing a function that populates a character array with every other character from an input array. The function takes three input arguments (an input array in, the length of the input array in_len, and the output array out), and returns the length of the output array. Your colleague asks you to determine which version of their function will run faster, func1() or func2(), without running the code. Explain your answer.
int func1(char* in,
int in_len,
char* out) {
int i;
char *out_ptr = out;
for (i=0; i<in_len; i++) {
if (i % 2 == 0) {
*out_ptr++ = in[i];
}
}
return out_ptr - out;
}
int func2(char* in,
int in_len,
char* out) {
int i;
char *out_ptr = out;
for (i=0; i<in_len; i+=2) {
*out_ptr++ = in[i];
}
return out_ptr - out;
}To determine which function runs faster, we will count the number of operations that need to be performed for an output array of length \(n\). The first function, func1(), does the following. Increment the loop counter for all \(2n\) characters in the original array, check if the loop counter is divisible by 2, and if it is, add the corresponding character to the output array. This is a total of \(5n\) operations that must be performed by this function. The second function, func2(), does the following. Add \(2\) to the loop counter for each of the \(n\) characters in the output array, and add each character to the output array. This is a total of \(2n\) operations that must be performed. Therefore, the second function should be faster because it needs to perform fewer operations to create the same output array.