C Primer Plus 10学习记录

C Primer Plus 10

数组与指针

Programing Test

(1)重点:用指针进行计算,初始化数组

#include<stdio.h>
#define MONTHS 12 //一年的月份数
#define YEARS 5 //年数
int main(void)
{
   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;
   const float (*ptr)[MONTHS] = rain;
   printf("YEAR         RAINFALL     (inchs)\n");
   for(year = 0,total = 0;year<YEARS;year++)
  {
       for(month = 0,subtot = 0;month<MONTHS;month++)
      {
           subtot += *(*(ptr + year)+month);
       printf("%5d %15.1f\n",2010+year,subtot);
       total += subtot;
      }
  }
   printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);
   printf("MONTHLY AVERAGES:\n\n");
   printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
   for(month = 0;month<MONTHS;month++)
  {
       for(year = 0;subtot = 0,year<YEARS;year++)
      {
           subtot += *(*(ptr+year)+month);
       printf("%4.1f",subtot/YEARS);
      }
  }
   printf("\n");
   getchar();
   return 0;
}

(2)编写一个程序,初始化一个double类型数组,然后把该数组的内容拷贝至其他3个数组中。设计三个函数,分别实现数组的复制功能。

#include<stdio.h>
#include<windows.h>
void copy_arr(double t[],double s[],int n);
void copy_ptr(double *t,double *s,int n);
void copy_ptrs(double *t,double *s_first,double *s_last);
int main(void)
{
   double source[5] = {1.1,2.2,3.3,4.4,5.5};
   double target1[5];
   double target2[5];
   double target3[5];
   copy_arr(target1,source,5);
   copy_ptr(target2,source,5);
   copy_ptrs(target3,source,source+5);
   return 0;
}
void copy_arr(double t[],double s[],int n)
{
   for(int i=0;i<n;i++)
  {
       t[i]=s[i];//数组作为函数的参数,使用下标访问数组
  }
}
void copy_ptr(double *t,double *s,int n)
{
   for(int i=0;i<n;i++)
  {
       *(t+i)=*(s+i);//指针作为函数的参数,需要表明指针的访问范围,不可越界
  }
}
void copy_ptrs(double *t,double*s_first,double *s_last)
{
   for(int i=0;(s_last-s_first)>i;i++)
  {
       *(t+i)=*(s_first+i);//指针作为函数的参数,首尾指针相等时停止循环
  }
}

(3)编写一个函数,返回存储在int类型数组中的最大值,并在一个简单的程序中测试该函数

#include<stdio.h>
int get_max(int number[],int n);
int main(void)
{
   int sourse[100]={1,2,3,4,5};
   printf("The largest number in array is %d",get_max(sourse,100));
   return 0;
}
int get_max(int number[],int n)
{
   int max = number[0];
   for(int i =0;i<n;i++)
  {
       if(max<number[i])
           max = number[i];
  }
   return max;
}

(4)编写一个程序,返回存储在double类型数组中的最大值下标。

#include<stdio.h>
int get_max_index(double number[],int n); //n表示number的长度
int main(void)
{
   double source[100]={2.5,3.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,1.7};
   printf("The largest number's index in array is %d",get_max_index(source,100));
   getchar();
   return 0;
}
int get_max_index(double number[],int n)
{
   double max = number[0];
   int index = 0;
   for(int i=0;i<n;i++){
       if(max<number[i]);{
           max = number[i];
           index = i;
      }
  }
   return index;
}

(5)编写一个程序,返回在double类型数组中的最大值和最小值的差。

#include<stdio.h>
double get_range(double number[],int n);
int main(void)
{
   double source[12] = {2.5,3.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,1.7};
   printf("The range is:%d",get_range(source,12));
   getchar();
   return 0;
}
double get_range(double number[],int n)
{
   double max = number[0];
   double min = number[0];
   for(int i=0;i<n;i++)
  {
       if(max<number[i]);max = number[i];
       if(min>number[i]);min = number[i];
  }
   return max-min;
}

 

上一篇:初识《C++ Primer Plus》


下一篇:[C++primer笔记]第二章:变量和基本类型