神秘博客

C Primer Plus [第六版]第10章编程练习题

课后:数组这一章说难不难,说简单不简单,知识点有点多,需要慢慢消化,很多东西都理解,但是看到题目后不是很懂哪个意思,明白之后一点就通,主要还是需要多练习,数组的表示法和指针表示法方法有点多。

1

//计算每年的总降水量、年平均降水量和5年中每月的平均降水量
#include <stdio.h>
#define YEARS 5
#define MONTHS 12

int main()
{
  const float rain[YEARS][MONTHS] =
  {
  { 4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6 },
  { 8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3 },
  { 9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4 },
  { 7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2 },
  { 7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2 }
  };

  int year, month;
  float subtot, total;

  printf("年\t \t降雨量(英寸)\n");
  for (year = 0, total = 0; year < YEARS; year++)
  {       //每一年,各月的降水量总和
    for (month = 0, subtot = 0; month < MONTHS; month++)
      subtot += *(*(rain + year) + month);
    printf("%5d %15.1f\n", 2010 + year, subtot);
    total += subtot; //5年的总降水量
  }
  printf("\n年平均值为 %.1f 英寸.\n\n",
    total / YEARS);
  printf("每月平均:\n\n");
  printf("01月  02月  03月  04月  05月  06月  07月  08月  09月  10月  11月  12月\n");


  for (month = 0; month < MONTHS; month++)
  {            //每个月 5年的总降水量
    for (year = 0, subtot = 0; year < YEARS; year++)
      subtot += *(*(rain + year) + month);
    printf("%-5.1f ", subtot / YEARS);
  }
  printf("\n");

  system("pause");
  return 0;
}

2

#include <stdio.h>

void copy_arr(double ar1[], double ar2[], int n);

void copy_ptr(double ar1[], double ar2[], int n);

void copy_ptrs(double ar1[], double ar2[], double ar3[]);

int main()
{
  int i;
  double source[5] = { 1.1,2.2,3.3,4.5,5.5 };
  double target1[5];
  double target2[5];
  double target3[5];

  copy_arr(target1, source, 5);
  for (i = 0; i < 5; i++)
  {
    printf("%g ", target1[i]);
  }
  putchar('\n');

  copy_arr(target2, source, 5);
  for (i = 0; i < 5; i++)
  {
    printf("%g ", target2[i]);
  }
  putchar('\n');

  copy_ptrs(target3, source, source + 5);
  for (i = 0; i < 5; i++)
  {
    printf("%g ", target3[i]);
  }
  putchar('\n');

  system("pause");
  return 0;
}

void copy_arr(double ar1[], double ar2[], int n)
{
  int i;

  for (i = 0; i < n; i++)
    ar1[i] = ar2[i];
    

}
void copy_ptr(double ar1[], double ar2[], int n)
{
  int i;

  for (i = 0; i < n; i++)
  {
    *(ar1 + i) = *ar2++;
  }
}
void copy_ptrs(double ar1[], double ar2[], double ar3[])
{
  while (ar2 < ar3)
  {
    *ar1++ = *ar2++;
  }
}

3

#include <stdio.h>

int max(int ar[], int n);

int main()
{
  int sum[10] = { 5,6,3,12,13,5,32,12,6,18 };
  int MAX, i;
  
  MAX = max(sum, 10);
  
  for (i = 0; i < 10; i++)
    printf("%-3d ", sum[i]);

  printf("\n这些数组中最大的是: %d\n", MAX);

  system("pause");
  return 0;
}

int max(int ar[], int n)
{
  int i, max = 0;

  for (i = 0; i < n; i++)
  {
    if (max < ar[i])
      max = ar[i];
  }

  return max;
}

4

#include <stdio.h>
//返回double数组中最大值的下标
int max(double ar[], int n);

int main()
{
  double sum[10] = { 2.6,45.2,123.6,22.6,55.66,132.321,22.2,37.6,15.69,61.2 };
  int i, MAX;

  MAX = max(sum, 10);

  for (i = 0; i < 10; i++)
    printf("%g   ", sum[i]);

  printf("\n这些数组中最大的值的下标是: %d\n", MAX);

  system("pause");
  return 0;

}

int max(double ar[], int n)
{
  int i, j, max = 0;

  for (i = 0; i < n; i++)
  {
    if (max < ar[i])
    {
      max = ar[i];
      j = i;
    }
  }

  return j;
}

5

#include <stdio.h>

double D_value(double ar[], int n);

int main()
{
  double sum[10] = { 2.6,45.2,123.6,22.6,55.66,132.321,22.2,37.6,15.69,61.2 };
  double d_value;
  int i;

  d_value = D_value(sum, 10);
  for (i = 0; i < 10; i++)
    printf("%g   ", sum[i]);

  printf("\n这些数组中最大的值与最小值的差值是: %g\n", d_value);

  system("pause");
  return 0;
}

double D_value(double ar[], int n)
{
  int i;
  double min, max = 0;
  min = ar[0];

  for (i = 0; i < n; i++)
  {
    if (max < ar[i])
    {
      max = ar[i];
    }
    if (min > ar[i])
    {
      min = ar[i];
    }
  }

  return max - min;
}

6

#include <stdio.h>

void sort(double ar[], int n);


int main()
{
  double sum[10] = { 2.6,45.2,123.6,22.6,55.66,132.321,22.2,37.6,15.69,61.2 };

  sort(sum, 10);

  system("pause");
  return 0;
}

void sort(double ar[], int n)
{
  int i;


  for (i = n - 1; i >= 0; i--)
  {
    printf("%g   ", ar[i]);
  }
  putchar('\n');

}

7

#include <stdio.h>

void copy(double ar1[], double ar2[], double ar3[]);

int main()
{
  double sum[2][5] = { {2.6,45.2,123.6,22.6,55.66},{132.321,22.2,37.6,15.69,61.2} };
  double sum_1[2][5];
  int i, j;

  copy(sum_1, sum, sum + 2);
  for (i = 0; i < 2; i++)
  {
    for (j = 0; j < 5; j++)
    printf("%g ", sum_1[i][j]);
  }
  putchar('\n');

  system("pause");
  return 0;

}

void copy(double ar1[], double ar2[], double ar3[])
{
  while (ar2 < ar3)
  {
    *ar1++ = *ar2++;
  }
}

8

#include <stdio.h>

void copy(double ar1[], double ar2[], int n);

int main()
{
  double sum[7] = { 1.1,2.2,3.3,4.5,5.5,6.6,7.7 };
  double sum_1[3];
  int i;
  
  copy(sum_1, &sum[2], 3);
  for (i = 0; i < 3; i++)
  {
    printf("%g ", sum_1[i]);
  }
  putchar('\n');

  system("pause");
  return 0;
}

void copy(double ar1[], double ar2[], int n)
{
  int i;

  for (i = 0; i < n; i++)
  {
    *ar1++ = *ar2++;
  }
}

9

#include <stdio.h>

void copy(int n, int m, double ar1[n][m],double ar2[n][m]);

void show(int n, int m, double ar[n][m]);

int main()
{
  double sum[3][5] = { { 2.6,45.2,123.6,22.6,55.66 },{ 132.321,22.2,37.6,15.69,61.2 },{ 5.6,4.2,23.6,12.6,5.66 } };
  double sum_1[3][5];

  copy(3, 5, sum_1, sum);
  printf("sum 数组: \n");
  show(3, 5, sum);
  printf("sum_1 数组: \n");
  show(3, 5, sum_1);

  return 0;

}

void copy(int n, int m, double ar1[n][m], double ar2[n][m])
{
  int i, j;

  for (i = 0; i < n; i++)
  {
    for (j = 0; j < m; j++)
      ar1[i][j] = ar2[i][j];
  }
}

void show(int n, int m, double ar[n][m])
{
  int i, j;

  for (i = 0; i < n; i++)
  {
    for (j = 0; j < m; j++)
      printf("%g   ", ar[i][j]);
  }
  putchar('\n');
}

10

#include <stdio.h>

void copy(int ar1[], int ar2[], int ar3[], int n);

int main()
{
  int num[4];
  int num_1[4] = { 2,4,5,8 };
  int num_2[4] = { 1,0,4,6 };
  int i;

  copy(num, num_1, num_2, 4);
  for (i = 0; i < 4; i++)
  {
    printf("%d ", num[i]);
  }
  putchar('\n');

  system("pause");
  return 0;

}

void copy(int ar1[], int ar2[], int ar3[], int n)
{
  int i;

  for (i = 0; i < n; i++)
  {
    *ar1++ = (*ar2++) + (*ar3++); //可以直接在这里递增也可以在下面递增
    //*ar2++;
    //*ar3++;
  }
}

11

#include <stdio.h>
//打印数组各元素的值
void show(int ar[][5], int n);
//数组各元素的值翻2倍
void sum(int ar[][5], int n);

int main()
{
  int num[3][5] = { {2,6,1,8,7},{4,3,11,12,9},{14,10,16,20,11} };

  show(num, 3);
  sum(num, 3);
  show(num, 3);

  system("pause");
  return 0;
}

void show(int ar[][5], int n)
{
  int i, j;
  for (i = 0; i < n; i++)
  {
    for (j = 0; j < 5; j++)
      printf("%-3d  ", ar[i][j]);
  }
  putchar('\n');
}

void sum(int ar[][5], int n)
{
  int i, j;

  for (i = 0; i < n; i++)
  {
    for (j = 0; j < 5; j++)
    {
      ar[i][j] *= 2;
    }
  }

}

12

//计算每年的总降水量、年平均降水量和5年中每月的平均降水量
#include <stdio.h>
#define YEARS 5
#define MONTHS 12
//计算每年总降雨量和年平均降雨量
void rain_a(const float ar[][MONTHS], int n);
//计算5年来每月平均降雨量
void rain_b(const float ar[][MONTHS], int n);


int main()
{
  const float rain[YEARS][MONTHS] =
  {
  { 4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6 },
  { 8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3 },
  { 9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4 },
  { 7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2 },
  { 7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2 }
  };



  printf("年\t \t降雨量(英寸)\n");
  rain_a(rain, YEARS);

  printf("每月平均:\n\n");
  printf("01月  02月  03月  04月  05月  06月  07月  08月  09月  10月  11月  12月\n");
  rain_b(rain, YEARS);
  printf("\n");

  system("pause");
  return 0;
}

void rain_a(const float ar[][MONTHS], int n)
{
  int i, j;
  float tot, total = 0;

  for (i = 0; i < n; i++)
  {

    for (j = 0, tot = 0; j < MONTHS; j++)
    {
      tot += ar[i][j];

    }
    printf("%5d %15.1f\n", 2010 + i, tot);
    total += tot;
  }
  printf("\n年平均值为 %.1f 英寸.\n\n", total / n);

}

void rain_b(const float ar[][MONTHS], int n)
{
  int i, j;
  float subtot;

  for (j = 0; j < MONTHS; j++)
  {
    for (i = 0, subtot = 0; i < n; i++)
      subtot += ar[i][j];
    printf("%-5.1f ", subtot / YEARS);
  }
}

13

#include <stdio.h>

void input(double ar[][5], int n);

void count_a(double ar[], int n);

void count_b(double ar[][5], int n);

void max(double ar[][5], int n);

int main()
{
  double num[3][5];
  int i;

  printf("请输入3组包含5个 double 类型的数: \n");
  input(num, 3);
  putchar('\n');
  for (i = 0; i < 3; i++)
  {
    count_a(&num[i][0], 5);
  }
  count_b(num, 3);
  max(num, 3);

  system("pause");
  return 0;


}

void input(double ar[][5], int n)
{
  int i, j;

  for (i = 0; i < n; i++)
  {
    printf("请输入第 %d 组: \n",i+1);
    for (j = 0; j < 5; j++)
    {
    bk:if (scanf_s("%lf", &ar[i][j]) == 1);
        
      else
      {
        scanf_s("%*s");
        printf("输入有误,请重新输入: \n");
        goto bk;
      }
    }
  }
}
void count_a(double ar[], int n)
{
  int i;
  double tot = 0;

  for (i = 0; i < n; i++)
  {
    printf("%g  ", *ar);
    tot += *ar++;
    
  }
  printf("\n该组数据中的平均值为: %g\n", tot / n);
}

void count_b(double ar[][5], int n)
{
  int i, j;
  double total = 0;

  for (i = 0; i < n; i++)
  {
    
    for (j = 0; j < 5; j++)
    {
      total += ar[i][j];
      
    }
    
  }
  printf("\n所有数据的平均值为: %g\n", total / (n * 5));
}

void max(double ar[][5], int n)
{
  int i, j;
  double max = 0;

  for (i = 0; i < n; i++)
  {

    for (j = 0; j < 5; j++)
    {
      if (max < ar[i][j])
        max = ar[i][j];
    }

  }
  printf("\n这 %d 个数据中最大的是: %g\n", n * 5, max);
}

14

#include <stdio.h>

void input(int n, int m, double ar[n][m]);

void count(int n, int m, double ar[n][m]);

void max(int n, int m, double ar[n][m]);

int main()
{
  double num[3][5];

  printf("请输入3组包含5个 double 类型的数: \n");
  input(3, 5, num);
  putchar('\n');
  count(3, 5, num);
  max(3, 5, num);

  //system("pause");
  return 0;


}

void input(int n, int m, double ar[n][m])
{
  int i, j;

  for (i = 0; i < n; i++)
  {
    printf("请输入第 %d 组: \n", i + 1);
    for (j = 0; j < m; j++)
    {
    bk:if (scanf("%lf", &ar[i][j]) == 1);

       else
       {
         scanf("%*s");
         printf("输入有误,请重新输入: \n");
         goto bk;
       }
    }
  }
}
void count(int n, int m, double ar[n][m])
{
  int i, j;
  double tot, total = 0;

  for (i = 0; i < n; i++)
  {

    for (j = 0, tot = 0; j < m; j++)
    {
      tot += ar[i][j];
      printf("%g  ", ar[i][j]);
    }
    printf("\n第 %d 组数据的平均值为: %g\n", i + 1, tot / m);
    total += tot;
  }
  printf("\n所有数据的平均值为: %g\n", total / (n * m));
}
void max(int n, int m, double ar[n][m])
{
  int i, j;
  double max = 0;

  for (i = 0; i < n; i++)
  {

    for (j = 0; j < m; j++)
    {
      if (max < ar[i][j])
        max = ar[i][j];
    }

  }
  printf("\n这 %d 个数据中最大的是: %g\n", n * m, max);
}

 

版权说明:
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏