经典编程900例(c语言)(第七篇)

本文展示了C语言中数组的各种操作,包括访问一维、二维和三维数组元素,计算数组元素个数,以及通过函数交换变量值、计算数组元素之和。还探讨了字符串数组的使用和二级指针访问字符串数组的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例75:访问数组元素

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int scores[5] = {80, 70, 90, 85, 80};

    printf("Array Values\n");
    // 数组下标从0开始
    printf("scores[0] %d\n", scores[0]);    // scores[0] 80
    printf("scores[1] %d\n", scores[1]);    // scores[1] 70
    printf("scores[2] %d\n", scores[2]);    // scores[2] 90
    printf("scores[3] %d\n", scores[3]);    // scores[3] 85
    printf("scores[4] %d\n", scores[4]);    // scores[4] 80

    return 0;
}

例76:函数打印二维数组元素

#include <stdio.h>

/**
 * 控制台打印二维数组
 * @Author dust_fall
 * @param  array     二维数组
 * @param  rows      二维数组的行数
 */
void show_2d_array(int array[][10], int rows)
{
    int i, j;

	// 外层循环控制行
    for (i = 0; i < rows; i++)
    	// 内层循环控制列
        for (j = 0; j < 10; j++)
            printf("array[%d][%d] = %d\n", i, j, array[i][j]);
}


int main(int argc, char const *argv[])
{
    int a[1][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
    int b[2][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                   {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
    int c[3][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                   {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
                   {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};

    show_2d_array(a, 1);
    show_2d_array(b, 2);
    show_2d_array(c, 3);

    return 0;
}

例77:计算数组的元素个数

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int int_values[] = {51, 23, 2, 44, 45}; 
    float float_values[] = {21.1, 13.3, 22.2, 34.4, 15.5};  

    printf("Number of elements in int_values %d\n",sizeof(int_values) / sizeof(int_values[0]));         //Number of elements in int_values 5
    printf("Number of elements in float_values %d\n",sizeof(float_values) / sizeof(float_values[0]));   // Number of elements in float_values 5
    
    return 0;
}

例78:访问二维数组元素

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int row, column;

    float table[3][5] = {{1.0, 2.0, 3.0, 4.0, 5.0},
                    {6.0, 7.0, 8.0, 9.0, 10.0},
                    {11.0, 12.0, 13.0, 14.0, 15.0}};

    for (row = 0; row < 3; row++)
        for (column = 0; column < 5; column++)
            printf("table[%d][%d] = %f\n", row, column, table[row][column]);
        
    return 0;
}

例79:访问三维数组元素

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int row, column, table;

    float values[2][3][5] = {
                            {{1.0, 2.0, 3.0, 4.0, 5.0},
                             {6.0, 7.0, 8.0, 9.0, 10.0},
                             {11.0, 12.0, 13.0, 14.0, 15.0}},
                             
                            {{16.0, 17.0, 18.0, 19.0, 20.0},
                             {21.0, 22.0, 23.0, 24.0, 25.0},
                             {26.0, 27.0, 28.0, 29.0, 30.0}}
                           };
   
    for (row = 0; row < 2; row++)
        for (column = 0; column < 3; column++)
            for (table = 0; table < 5; table++)
                printf("values[%d][%d][%d] = %f\n", row, column, table, 
                    values[row][column][table]);
    return 0;
}

例80:访问一维数组元素

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int scores[5] = {80, 70, 90, 85, 80};
    int i;

    printf("Array Values\n");

    for (i = 0; i < 5; i++)
        printf("scores[%d] %d\n", i, scores[i]);

    return 0;
}

例81:计算数组所占字节数

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int box[3][3];

    float year_sales[52][5];

    char pages[40][60][20];

    printf("Bytes to hold int box[3][3] %d bytes\n", sizeof(box));
    printf("Bytes to hold float year_sales[52][5] %d bytes\n", sizeof(year_sales));
    printf("Bytes to hold char pages[40][60][20] %ld bytes\n", sizeof(pages));
    
    return 0;
}

例82:使用函数交换变量值

#include <stdio.h>

/**
 * 交换两个整型变量的值
 * @Author dust_fall
 * @param  a         变量1
 * @param  b         变量2
 */
void swap_values(int *a, int *b)
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;
}

int main(int argc, char const *argv[])
{
    int one = 1, two = 2;

    swap_values(&one, &two);

    printf("one contains %d two contains %d\n", one, two);	// one contains 2 two contains 1

    return 0;
}

例83:使用函数计算数组元素之和(想想这段代码对吗)

#include <stdio.h>

/**
 * 计算数组元素之和
 * @Author dust_fall
 * @param  array     要计算的数组
 * @param  elements  数组元素个数
 * @return           返回元素之和
 */
long sum_array(int array[], int elements)
{
    long sum = 0;

    int i;

    for (i = 0; i < elements; i++)
        sum += array[i];

    return(sum);
}


int main(int argc, char const *argv[])
{
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int b[2][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                   {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
    int c[3][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                   {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
                   {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};

    printf("Sum of first array elements %d\n", sum_array(a, 10));
    printf("Sum of second array elements %d\n", sum_array(b, 20));
    printf("Sum of third array elements %d\n", sum_array(c, 30));

    return 0;
}

例84:声明一个大的数组

int main(int argc, char const *argv[])
{
    char string[66000L];   // 66,000 bytes

    int values[33000L];    // 33,000 * 2 = 66,000 bytes

    float numbers[17000];  // 17,000 * 4 = 68,000 bytes

    return 0;
}

例85:字符串数组使用

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// 字符串 == 结尾为'\0'的字符数组
	// 声明有七个元素char*数组存放字符串的首地址
    char *weekdays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    int i;

    for (i = 0; i < 7; i++)
        printf("weekdays[%d] contains %s\n", i, weekdays[i]);

    return 0;
}

例86:使用二级指针访问字符串数组(这段程序有什么问题)

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char *workdays[] = {"Monday", "Tuesday", "Wednesday",
                        "Thursday", "Friday", "" };
    char **work_day;

    work_day = workdays; 

    while (*work_day)
        printf("%s\n", *work_day++);

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值