|
|
|
|
|
|
![]() Chapter 8Answers to Selected Exercises
1. [was #4] The problem with
2. [was #8] To use a digit 7. [was #10]
const int segments[10][7] = {{1, 1, 1, 1, 1, 1},
{0, 1, 1},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1}};
Answers to Selected Programming Projects2. [was #2]
#include <stdio.h>
int main(void)
{
int digit_count[10] = {0};
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
while (n > 0) {
digit = n % 10;
digit_count[digit]++;
n /= 10;
}
printf ("Digit: ");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit);
printf("\nOccurrences:");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit_count[digit]);
printf("\n");
return 0;
}
5. [was #6]
#include <stdio.h>
#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))
#define INITIAL_BALANCE 100.00
int main(void)
{
int i, low_rate, month, num_years, year;
double value[5];
printf("Enter interest rate: ");
scanf("%d", &low_rate);
printf("Enter number of years: ");
scanf("%d", &num_years);
printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%6d%%", low_rate + i);
value[i] = INITIAL_BALANCE;
}
printf("\n");
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
for (i = 0; i < NUM_RATES; i++) {
for (month = 1; month <= 12; month++)
value[i] += ((double) (low_rate + i) / 12) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
8. [was #12]
#include <stdio.h>
#define NUM_QUIZZES 5
#define NUM_STUDENTS 5
int main(void)
{
int grades[NUM_STUDENTS][NUM_QUIZZES];
int high, low, quiz, student, total;
for (student = 0; student < NUM_STUDENTS; student++) {
printf("Enter grades for student %d: ", student + 1);
for (quiz = 0; quiz < NUM_QUIZZES; quiz++)
scanf("%d", &grades[student][quiz]);
}
printf("\nStudent Total Average\n");
for (student = 0; student < NUM_STUDENTS; student++) {
printf("%4d ", student + 1);
total = 0;
for (quiz = 0; quiz < NUM_QUIZZES; quiz++)
total += grades[student][quiz];
printf("%3d %3d\n", total, total / NUM_QUIZZES);
}
printf("\nQuiz Average High Low\n");
for (quiz = 0; quiz < NUM_QUIZZES; quiz++) {
printf("%3d ", quiz + 1);
total = 0;
high = 0;
low = 100;
for (student = 0; student < NUM_STUDENTS; student++) {
total += grades[student][quiz];
if (grades[student][quiz] > high)
high = grades[student][quiz];
if (grades[student][quiz] < low)
low = grades[student][quiz];
}
printf("%3d %3d %3d\n", total / NUM_STUDENTS, high, low);
}
return 0;
}
Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |