1. Write a program that creates an array with 26 elements and stores the 26 lowercase letters in it. Also have it show the array contents.
/* this implementation assumes the character codes */
/* are sequential, as they are in ASCII. */
#include <stdio.h>
#define SIZE 26
int main(void) {
char lcase[SIZE];
int i;
for (i = 0; i < SIZE; i++)
lcase[i] = 'a' + i;
for (i = 0; i < SIZE; i++)
printf("%c ", lcase[i]);
printf("
");
return 0;
}
3. Use nested loops to produce the following pattern:
F
FE
FED
FEDC
FEDCB
FEDCBA
/* this implementation assumes the character codes */
/* are sequential, as they are in ASCII. */
#include <stdio.h>
int main(void) {
char let = 'F';
char start;
char end;
for (end = let; end >= 'A'; end--) {
for (start = let; start >= end; start--)
printf("%c", start);
printf("
");
}
return 0;
}
6. Write a program that prints a table with each line giving an integer, its square, and its cube. Ask the user to input the lower and upper limits for the table. Use a for loop.
#include <stdio.h>
int main(void) {
int lower, upper, index;
int square, cube;
printf("Enter starting integer: ");
scanf("%d", &lower);
printf("Enter ending integer: ");
scanf("%d", &upper);
printf("%5s %10s %15s
", "num", "square", "cube");
for (index = lower; index <= upper; index++) {
square = index * index;
cube = index * square;
printf("%5d %10d %15d
", index, square, cube);
}
return 0;
}
8. Write a program that requests two floating-point numbers and prints the value of their difference divided by their product. Have the program loop through pairs of input values until the user enters nonnumeric input.
#include <stdio.h>
int main(void) {
double n, m;
double res;
printf("Enter a pair of numbers: ");
while (scanf("%lf %lf", &n, &m) == 2) {
res = (n - m) / (n * m);
printf("(%.3g - %.3g)/(%.3g * %.3g) = %.5g
", n, m, n, m, res);
printf("Enter next pair (non-numeric to quit): ");
}
return 0;
}
11. Write a program that reads eight integers into an array and then prints them in reverse order.
#include <stdio.h>
int main(void) {
int numbers[8];
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Enter the values of the elements in the array:
");
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
printf("The elements in the array in an inverse order:
");
for (int i = size - 1; i >= 0; i--)
printf("%d ", numbers[i]);
return 0;
}
13. Write a program that creates an eight-element array of ints and sets the elements to the first eight powers of 2 and then prints the values. Use a for loop to set the values, and, for variety, use a do while loop to display the values.
/* This version starts with the 0 power */
#include <stdio.h>
#define SIZE 8
int main(void) {
twopows[SIZE];
int
int i; /* 2 to the 0 */
int value = 1;
for (i = 0; i < SIZE; i++) {
twopows[i] = value;
}
value *= 2;
i = 0;
do {
printf("%d ", twopows[i]);
i++;
} while (i < SIZE);
printf("
");
return 0;
}
14. Write a program that creates two eight-element arrays of doubles and uses a loop to let the user enter values for the eight elements of the first array. Have the program set the elements of the second array to the cumulative totals of the elements of the first array. For example, the fourth element of the second array should equal the sum of the first four elements of the first array, and the fifth element of the second array should equal the sum of the first five elements of the first array. (It’s possible to do this with nested loops, but by using the fact that the fifth element of the second array equals the fourth element of the second array plus the fifth element of the first array, you can avoid nesting and just use a single loop for this task.) Finally, use loops to display the contents of the two arrays, with the first array displayed on one line and with each element of the second array displayed below the corresponding element of the first array.
#include <stdio.h>
#define size 8
int main(void) {
double nums1[size], nums2[size];
printf("Please enter the values of the elements in nums1:
");
for (int i = 0; i < size; i++)
scanf("%lf", &nums1[i]);
double sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + nums1[i];
nums2[i] = sum;
}
printf("The elements in the first array: ");
for (int i = 0; i < size; i++)
printf("%7.2lf ", nums1[i]);
printf("
");
printf("The elements in the second array: ");
for (int i = 0; i < size; i++)
printf("%7.2lf ", nums2[i]);
return 0;
}
16. Daphne invests $100 at 10% simple interest. (That is, every year, the investment earns an interest equal to 10% of the original investment.) Deirdre invests $100 at 5% interest compounded annually. (That is, interest is 5% of the current balance, including previous addition of interest.) Write a program that finds how many years it takes for the value of Deirdre’s investment to exceed the value of Daphne’s investment. Also show the two values at that time.
#include <stdio.h>
#define RATE_SIMP 0.10
#define RATE_COMP 0.05
#define INIT_AMT 100.0
int main(void) {
double daphne = INIT_AMT;
double deidre = INIT_AMT;
int years = 0;
while (deidre <= daphne) {
daphne += RATE_SIMP * INIT_AMT;
deidre += RATE_COMP * deidre;
++years;
}
printf("Investment values after %d years:
", years);
printf("Daphne: $%.2f
", daphne);
printf("Deidre: $%.2f
", deidre);
return 0;
}