答案为本人自己编写,仅供大家学习参考,如有错误,欢迎大家在评论区留言指正。
ex6.1
// ex_6.1
#include
#define SIZE 26
int main(void)
{
char arr[SIZE];
char a = 'a';
for (int i = 0; i < SIZE; i++, a++)
{
arr[i] = a;
}
for (int i = 0; i < SIZE; i++)
printf("%c ", arr[i]);
return 0;
}
------------------------------------------------------------------------------------------------
ex6.2
// ex_6.2
#include
int main(void)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 0; j < i; j++)
printf("$");
printf("\n");
}
return 0;
}
------------------------------------------------------------------------------------------------
ex6.3
// ex_6.3
#include
int main(void)
{
char L = 'F';
for (int i = 1; i <= 6; i++)
{
for (int j = 0; j < i; j++)
printf("%c", L-j);
printf("\n");
}
return 0;
}
------------------------------------------------------------------------------------------------
ex6.4
// ex_6.4
#include
int main(void)
{
char L = 'A';
for (int i = 1; i <= 6; i++)
{
for (int j = 0; j < i; j++)
printf("%c", L++);
printf("\n");
}
return 0;
}
------------------------------------------------------------------------------------------------
ex6.5
// ex_6.5
#include
int main(void)
{
int c,r; // c means column, r means row
for (c = 1; c <= 5; c++)
{
for (int s = 5 ;s-c > 0; s--)printf(" "); // print space
for (r = 0; r<(c);r++)printf("%c",(r+64)+1); // ABDCE.....
for (r = c; r>(1);r--)printf("%c",(r+64)-1); // .....DCBA
printf("\n"); // newline
}
return 0;
}
------------------------------------------------------------------------------------------------
ex6.6
// ex_6.6
#include
int main(void)
{
int lower = 0;
int upper = 0;
printf("Enter the lower limit: ");
scanf("%d", &lower);
printf("Enter the upper limit: ");
scanf("%d", &upper);
for (lower; lower <= upper; lower++)
printf("%d\t%d\t%d\n",lower, lower * lower, lower * lower * lower);
return 0;
}
------------------------------------------------------------------------------------------------
ex6.7
// ex_6.7
#include
#include
int main(void)
{
char arr[40];
int index = 0;
printf("Enter a string: ");
scanf("%s", arr);
index = strlen(arr);
printf("Your string: %s\n", arr);
printf("Backward string: ");
for (index; index > 0;index--)
printf("%c", arr[index-1]);
return 0;
}
------------------------------------------------------------------------------------------------
ex6.8
// ex_6.8
#include
int main(void)
{
float num1 = 0;
float num2 = 0;
float r = 0;
printf("Enter two numbers(Enter q to quit): ");
while (scanf("%f%f", &num1, &num2) == 2)
{
r = (num1 - num2) / (num1 * num2);
printf("(%.2f - %.2f) / (%.2f * % .2f) = %.2f\n", num1, num2, num1, num2, r);
printf("Enter two numbers(Enter q to quit): ");
}
printf("quit");
return 0;
}
------------------------------------------------------------------------------------------------
ex6.9
// ex_6.9
#include
float f(float, float);
int main(void)
{
float num1 = 0;
float num2 = 0;
printf("Enter two numbers(Enter q to quit): ");
while (scanf("%f%f", &num1, &num2) == 2)
{
printf("(%.2f - %.2f) / (%.2f * % .2f) = %.2f\n", num1, num2, num1, num2, f(num1, num2));
printf("Enter two numbers(Enter q to quit): ");
}
printf("quit");
return 0;
}
float f(float a, float b)
{
return (a - b) / (a * b);
}
------------------------------------------------------------------------------------------------
ex6.10
// ex_6.10
#include
int square(int);
int sum(int, int);
int main(void)
{
int lower = 0;
int upper = 0;
printf("Enter lower and upper integer limits: ");
while (scanf("%d%d", &lower, &upper) == 2 && lower != upper)
{
printf("The sums of the squares from %d to %d is %d\n", square(lower), square(upper), sum(lower, upper));
printf("Enter next set of limits: ");
}
printf("Done\n");
return 0;
}
int square(int x)
{
return x * x;
}
int sum(int lower, int upper)
{
int s = 0;
for (int i = lower; i <= upper; i++)
s += square(i);
return s;
}
------------------------------------------------------------------------------------------------
ex6.11
// ex_6.11
#include
int main(void)
{
int arr[8];
printf("Enter eight integers: ");
for (int i = 0; i < 8; i++)scanf("%d", &arr[i]);
for (int i = 7; i >= 0;i--)printf("%d ", arr[i]);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.12
// ex_6.12
#include
#include
float s1(int);
float s2(int);
int main(void)
{
float sum1 = 0;
float sum2 = 0;
printf("\t100 terms\t1000 terms\t10,000 terms\n");
printf("s1\t%f\t%f\t%f\n", s1(100), s1(1000), s1(10000));
printf("s2\t%f\t%f\t%f\n", s2(100), s2(1000), s2(10000));
return 0;
}
float s1(int n)
{
float s = 0;
for (int i = 0; i < n; i++)
s += 1.0 / (i+1);
return s;
}
float s2(int n)
{
float s = 0;
for (int i = 0; i < n; i++)
s += pow(-1, i) * 1.0 / (i+1);
return s;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.13
// ex_6.13
#include
#include
int main(void)
{
int arr[8];
for (int i = 0; i < 8; i++)
{
arr[i] = pow(i, 2);
}
int index = 0;
do
{
printf("%d ", arr[index]);
index++;
}
while
(index < 8);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.14
// ex_6.14
#include
#define SIZE 8
int main(void)
{
double arr1[SIZE],arr2[SIZE];
double s = 0;
printf("Enter eight numbers: ");
for (int i = 0; i < SIZE; i++)scanf("%lf", &arr1[i]);
for (int i = 0; i < SIZE; i++)
{
s += arr1[i];
arr2[i] = s;
}
printf("ARRAY 1: ");
for (int i = 0; i < SIZE; i++)printf("%.2f ", arr1[i]);
printf("\n");
printf("ARRAY 2: ");
for (int i = 0; i < SIZE; i++)printf("%.2f ", arr2[i]);
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.15
// ex_6.15
#include
int main(void)
{
char arr[255];
int i = 0;
printf("Please enter the string: ");
do
{
scanf("%c", &arr[i]);
i++;
}
while (arr[i-1] != '\n');
for (int j = i - 1; j > -1; j--)
printf("%c" , arr[j]);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.16
// ex_6.16
#include
int main(void)
{
double Daphne = 100;
double Deirdre = 100;
int year = 0;
do
{
year++;
Daphne = Daphne + 10;
Deirdre = Deirdre * 1.05;
}
while (Daphne > Deirdre);
printf("It takes %d years. Daphne: %.2f Deirdre %.2f\n", year, Daphne, Deirdre);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.17
// ex_6.17
#include
int main(void)
{
double deposit = 1000000;
int year = 0;
do
{
year++;
deposit *= 1.08;
deposit -= 100000;
}
while (deposit > 0);
printf("It takes %d years for Chuckie to empty his account.\n", year);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
ex6.18
// ex_6.18
#include
int main(void)
{
int week = 0;
int total = 5;
do
{
week++;
total = total - week;
total = total * 2;
printf("Week: %d Number of friends: %d\n", week, total);
}
while (total < 150);
return 0;
}