zoukankan      html  css  js  c++  java
  • 数组

    数组是把具有相同数据类型的若干变量有序的组织起来的集合。

    数组属于构造数据类型

    编译器给数组分配空间的时候,按照数组长度给数组分配一片连续的内存空间

    声明数组:

    long numbers[10];

    中括号中的数字定义了要存放在数组中的元素的个数,也就是数组的长度,称为数组维

    如何区分数组中的变量?

    数组中的每个值都由索引值来识别。索引值是一个整数,放在数组名称后的方括号内。是从0开始的连续整数。所以取读数组里的元素例如: numbers[2];就是取读的在numbers数组中索引为2的元素

    使用数组:

    #include <stdio.h>
    
    int main(void)
    {
        /* 计算10个分数的平均值 */
        double scores[10];
        printf("
    Input ten number as student score:
    ");
        for(int i=0;i<10;i++)
        {
            scanf("%lf",&scores[i]);
        } 
        double result=0.0;
        for(int i=0;i<10;i++)
        {
            result+=scores[i];
        }
        double avg=result/10;
        printf("coun t= %.2lf , avg= %.2lf ",result,avg);
        return 0;
    }
    View Code

    内存:

    计算机的内存可以看作一排很整齐的盒子,每个盒子都有两个状态(满为1,空为0)。每一个盒子都包含一个二进制数,被称为位。

    字节的标记被称为地址

    寻址运算符

    在前面的scanf()函数中广泛使用。

    它放在变量名前,它返回的是变量的地址

    %p 输出16进制。

    输出变量的地址:

    printf("
    %p",&value);

    数组的初始化:

    1、直接确定的给数组指定元素赋初值

    2、

    double values[5]={1.5,2.5,3.5,4.5,5.5};

    ,也可以有选择的给数组初始化,那样的话没有初始化的元素的值为默认值

    double values[5]={1.5,2.5,3.5};

    3、这种数组的大小由元素的初始值来确定

    int primes[]={2,3,5,7,9,22,13};

    确定数组的大小:

    我们前面学过使用sizeof确定变量的所占字节数,在同一数组中,所有元素的所占字节相等。

    计算数组大小:

    #include <stdio.h>

    int main(void)
    {
        /* 使用sizeof确定数组的大小 */
        printf(" The size of a variable of type long is %d bytes.",sizeof(long));
        double value=1.0;
        printf(" The size of value is %d bytes." ,sizeof value);
        int numbers[]={
            12,45,23,66,34,243,98
        } ;
        int count=0;
        count=(sizeof numbers)/(sizeof numbers[0]);
        printf("the array has %d element.",count);
        return 0;
    }

    多维数组

    二维数组的声明:

    demo: float carrots[25][50];

    多维数组的初始化

    demo: int numbers[3][4]={

         {10,20,30,40},{15,25,35,45},{47,48,49,50}

    }

    如果提供的初始值少于行中的元素数,就必须给每一行的值加上括号。

    九宫格:

    #include <stdio.h>
    
    int main(void)
    {
        int player=0; /* player number - 1 or 2*/
        int winner=0; /* the winning player */
        int choice=0;
        int row=0; /* Row index for a square */
        int column=0; /* Column index for a square */
        int line=0;
            
        char board[3][3]={
            {'1','2','3'},
            {'4','5','6'},
            {'7','8','9'}
        };
        for(int i=0;i<9&&winner==0;i++)
        {
            printf("
    
    ");
            printf("%c | %c | %c
    ",board[0][0],board[0][1],board[0][2]);
            printf("---+---+---
    ");
            printf("%c | %c | %c
    ",board[1][0],board[1][1],board[1][2]);
            printf("---+---+---
    ");
            printf("%c | %c | %c
    ",board[2][0],board[2][1],board[2][2]);
            printf("---+---+---
    ");
            
            player=i%2+1; /* Stect player */
            
            do{
                printf("
    Player %d,please enter the number of the square where you want to place your %c:",player,(player==1)?'X':'O');
                scanf("%d",&choice);
                
                row=--choice/3;
                column=choice%3;
                
            }while(choice<0||choice>9||board[row][column]>'9');
            
            board[row][column]=(player==1)?'X':'O';
            
            if((board[0][0]==board[1][1]&&board[0][0]==board[2][2])||
               (board[0][2]==board[1][1]&&board[0][2]==board[2][0]))
            {
                   winner =player;
            }else{
                for(line=0;line<=2;line++)
                {
                    if((board[line][0]==board[line][1]&&board[line][0]==board[line][2])||
                       (board[0][line]==board[1][line]&&board[0][line]==board[2][line]))
                    {
                           winner =player;
                    }
                }
            }
            
        }
        
        if(winner==0)
        {
            printf("
    How boring,it is a draw
    ");
        }else
        {
            printf("
    Congratulations, player %d,YOU ARE THE WINNER!
    ",winner);
        }
        return 0;
    }
  • 相关阅读:
    Java中只有按值传递,没有按引用传递!(两种参数情况下都是值传递)
    最简单的struts实例介绍
    Spring中bean的五个作用域简介(转载)
    Spring配置文件
    轻松搞定面试中的二叉树题目 (转)
    二叉树
    稳定排序与非稳定排序判别方法
    Yii的缓存机制之动态缓存
    Yii的缓存机制之数据缓存
    Yii的缓存机制之页面缓存
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5436252.html
Copyright © 2011-2022 走看看