实验四

task 1-1

#include <stdio.h>
const int N = 4;
int main()
{
	int a[N] = {2, 0, 2, 1};
	char b[N] = {'2', '0', '1', '1'};
	int i;
	printf("sizeof(int) = %d\n", sizeof(int));
	printf("sizeof(char) = %d\n", sizeof(char));
	printf("\n");
	for (i = 0; i < N; ++i)
		printf("%x: %d\n", &a[i], a[i]);
		printf("\n");
	for (i = 0; i < N; ++i)
		printf("%x: %c\n", &b[i], b[i]);
	return 0;
}

实验四

答:

int型数组a,在内存中是连续存放的;每个元素占用4个内存字节单元

char型数组b,在内存中是连续存放的;每个元素占用1个内存字节单元

task 1-2

#include <stdio.h>
int main()
{
	int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
	char b[2][3] = {{'1', '2', '3'}, {'4', '5', '6'}};
	int i, j;
	for (i = 0; i < 2; ++i)
		for (j = 0; j < 3; ++j)
			printf("%x: %d\n", &a[i][j], a[i][j]);
	printf("\n");
	for (i = 0; i < 2; ++i)
		for (j = 0; j < 3; ++j)
			printf("%x: %c\n", &b[i][j], b[i][j]);
}

实验四

答:

int型二维数组a,在内存中是按行连续存放的;每个元素占用4个内存字节单元

char型二维数组b,在内存中是按行连续存放的;每个元素占用1个内存字节单元

task 2

#include <stdio.h>
#define N 1000
int fun(int n, int m, int bb[N])
{
	int i, j, k = 0, flag;
	for (j = n; j <= m; j++)
	{
		flag=1;//考虑到可能在某次循环时flag的值会变为0,所以每次循环时重新赋一个非0的数给flag 
		for (i = 2; i < j; i++)
		if (j%i==0)
		{
			flag = 0;
			break;
		}
	if (flag!=0)
	bb[k++] = j;
	}
	return k;
}
int main()
{
	int n = 0, m = 0, i, k, bb[N];
	scanf("%d", &n);
	scanf("%d", &m);
	for (i = 0; i < m - n; i++)
		bb[i] = 0;
	k = fun(n,m,bb);
	for (i = 0; i < k; i++)
		printf("%4d", bb[i]);
	return 0;
}

实验四

task 3

 

  

  

 

  

上一篇:sizeof的概念


下一篇:C语言中sizeof()的用法