葛才璟

实验4 数组

实验4 数组

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型数据在内存中连续存放,每个元素占4个内存字节单元;

char型数据在内存中连续存放,每个元素占1个内存字节单元。

#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个内存字节单元。

2. 实验任务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;
for (i = 2; i < j; i++)
if (j%i==0)
{
flag = 0;
break;
}
if (flag==1)
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;
}

 

 

 

 3. 实验任务3

#include <stdio.h>

const int N = 5;
int find_max(int x[], int n);
void input(int x[], int n);
void output(int x[], int n);

int main()
{
    int a[N];
    int max;
    
    input(a, N); 
    output(a, N); 
    max = find_max(a, N); 
    printf("max = %d\n", max);
    
    return 0;
}

void input(int x[], int n)
{
    int i;
    
    for (i = 0; i < n; ++i)
        scanf("%d", &x[i]);
}

void output(int x[], int n)
{
    int i;
    
    for (i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}

int find_max(int x[], int n)
{
    int i,a;
    for(i=0;i<n;i++){
        if(x[i+1]>x[i]){
            a=x[i+1];
            x[i]=x[i+1];
        }
    } 
    return a;
}

 

 4. 实验任务4

#include <stdio.h>
void dec2n(int x, int n); 

int main()
{
    int x;
    
    printf("输入一个十进制整数: ");
    scanf("%d", &x);
    
    dec2n(x, 2); 
    dec2n(x, 8);
    dec2n(x, 16); 
    
    return 0;
}

void dec2n(int x, int n){
    int i,a[i];
    char b[i];
    
    for(i=0;x>0;i++){
        a[i]=x%n;
        
        if(a[i]>=10){
            switch(a[i]){
                case 10:b[i]='A';
                        break;
                case 11:b[i]='B';
                        break;
                case 12:b[i]='C';
                        break;
                case 13:b[i]='D';
                        break;
                case 14:b[i]='E';
                        break;
                case 15:b[i]='F';
                        break;
            }
        }else{
            b[i]=a[i]+'0';
        }
        x=x/n;
    }

    for(--i;i>=0;i--){
        printf("%c",b[i]);
    }
    printf("\n");
}

 

 

 

 5. 实验任务5

#include<stdio.h>
#define num(n) (n*n)
int main(){
    int i,n,j,x=0,y;
    printf("Enter n:");
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++){
       y = 1;
       x = 0;
             for(j=1;j<=n;j++){
                if(y<=i) {
                 x = x + 1;
                printf("%d ",x);
                y++;
                 }        
                else printf("%d ",x);
            } 
            printf("\n");
        }
        printf("\n");
         printf("Enter n:");
    }
}

 

posted on 2021-12-04 16:57  葛才璟  阅读(31)  评论(0编辑  收藏  举报

导航