首页 > 编程语言 > 详细

数组与指针的练习

时间:2015-12-03 19:11:44      阅读:604      评论:0      收藏:0      [点我收藏+]

复习题

1.1下面程序将打印出什么?

#include <stdio.h>
int main()
{
    int ref[] = { 8,4,0,2 };
    int *ptr;
    int index;

    for (index = 0, ptr = ref; index < 4; index++, ptr++)
        printf("%d %d\n", ref[index], *ptr);
    return 0;
}

【运行结果】

8 8
4 4
0 0
2 2

1.2数组ref包含多少个元素:4个

1.2ref是哪些数据的地址?ref+1呢?++ref指向什么?

A:数组名ref指向数组的第一个元素,表达式ref+1指向第二个元素。++ref不是合法的C表达式,因为ref是常量不是变量

2.下面每种情况中*ptr和(ptr+2)的值分别是什么?

a.

int *ptr;

int torf[2][2] ={12,14,16};

ptr =torf[0]

[*ptr =12,(ptr+2)=16]

[相当于{12,14},{16,0}]

b.

int *ptr;

int fort[2][2]={{12},{14,16}};

ptr =fort;

[*ptr =12,(ptr+2)=14]

[ptr+2代表第二行第一个元素]

3.下面每种情况中**ptr和**(ptr+1)的值分别是什么

a.

int (*ptr)[2];

int torf[2][2] ={12,14,16];

ptr =torf;

[**ptr=12   **(ptr+1)=16]

b.

int(*ptr)[2];

int fort[2][2] ={{12},{14,16}};

ptr =fort;

[**ptr=12   **(ptr+1)=14]

[ptr指向第一行,ptr+1指向第二行]

6.假如有如下定义

int grid [30][100]
a.用一种方法表示grid[22][56]的地址
&grid[22][56]

b.用两种方法表示grid[22][0]的地址

&grid[22][0]
grid[22]

c.用三种方法表示grid[0][0]

&grid[0][0] 
grid[0]
(int *)grid

4. 用适当的方法声明下面每个变量:

a.digits:一个包含10个int值得数组:int digits[10]

b.rates:一个包含6个float值得数组:float rates[6]

c.mat:一个包含3个元素的数组,其中每个元素是一个包含5个整数的数组: int mat[3][5]

d.psa: 一个包含20个指向char的指针的数组: char *psa[20]

e.pstr:一个指向数组的指针,其中数组由20个char值构成:char (*pstr)[20]

8.a.定义一个包含6个int值得数组,并且数值1、2、4、8、16和32进行初始化。

int array[6] ={1,2,4,8,16,32};

b.用数组符号表示a部分中数组的第三个元素

array[2];

c.假设系统支持C99规则,定义一个包含100个int值得数组并且初始化它,使它的末元素为-1,其他元素的值不考虑.

int arrc[100]={[99]=-1};

5.包含10个元素的数组的索引范围是什么?

0~9

6.假设有如下声明

float rootbeer[10],things[10][5],*pf,value = 2.2;
int i = 3;

则下列语句中哪些是正确的,哪些是错误的?

a.rootbeer[2] = value;      正确

b.scanf(“%f”,&rootbeer);  不正确,rootbeer不是一个float变量

c.rootbeer = value;  不正确

d.printf(“%f”,rootbeer); 不正确

e.things[4][4] =rootbeer[3];   正确

f.things[5] = rootbeer; 不正确,不能使用数组赋值

g.pf =value;不正确,value不是一个地址

h.pf = rootbeer; 正确

7.声明一个800*600的int数组

int screen[800][600]

8.以下是3个数组声明:

double trot[20];
short clops[10][30];
long shots[5][10][15];

a.以传统的void函数方式,写出处理数组trots的函数原型和函数调用;然后以变长数组方式,写出处理数组trots的函数原型和函数调用。

void process(double ar[],int n);
void processvla(int n,double ar[n]);
process (trots,20);
processvla(20,trots);

b.以传统的void函数方式,写出处理数组clops的函数原型和函数调用;然后以变长数组方式,写出处理数组clops的函数原型和函数调用。

void process2(short ar2[30],int n);
void process2vla(int n,int m,short ar2[n][m]);
process2(clops,10);
process2vla(10,30,clops);

c.以传统的void函数方式,写出处理数组shots的函数原型和函数调用;然后以变长数组方式,写出处理数组shots的函数原型和函数调用。

void process3(long ar3[10][15],int n);
void process3vla(int n,int m,int k ,long ar3[n][m][k]);
process3(shots,5);
process3vla(5,10,15,shots);

9.下面是两个函数原型:

void show(double ar[],int n);            //n是元素数
void show2(double ar2[][3],int n);       //n是行数

a.编写一个函数调用,把包含数值8、3、9和2的复合文字传递给函数shows()。

show((int [4]){8,3,9,2},4};

b.编写一个函数调用,把包含2行3列数值的复合文字传递给函数show2(),其中第一行为8、3、9;第二行为5、4、1。

show2((int[][3]{8,3,9}{5,4,1}),2};

编程题

1.修改rain程序,使它不适用数组下标,而是使用指针进行计算

 

/*rain.c --针对若干年的降水量数据,计算年降水总量、年降水平均量,以及月降水平均量*/
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main()
{
    //把数组初始化为2000年到2004年的降水量数据
    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,4.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,7.4,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("YEAR  RAINFALL(inches) \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", 2000 + year, subtot);
        total += subtot;
    }
    printf("\n The 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 += rain[year][month];
        printf("%4.1f", subtot / YEARS);

    }
    printf("\n");
    return 0;
}
 

.

/*rain.c --针对若干年的降水量数据,计算年降水总量、年降水平均量,以及月降水平均量*/
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main()
{
    //把数组初始化为2000年到2004年的降水量数据
    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,4.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,7.4,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("YEAR  RAINFALL(inches) \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", 2000 + year, subtot);
        total += subtot;
    }
    printf("\n The 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 +=*(*(rain+year)+month);
        printf("%4.1f", subtot / YEARS);

    }
    printf("\n");
    return 0;
}

数组与指针的练习

原文:http://www.cnblogs.com/thenewone/p/5017171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!