C<5>use arrays to manipulate mutiple data(note)

目录

 1,initialization of one-dimensional arrays

1,syntax:

2,some rules

2,algorithm:bubble sort algorithm(起泡排序in Chinese)

P.S. why does it called"bubble sort"?

2,Define and reference two-dimensional arrays(matrix)

1,define the type of a matrix 

2, initialization of matrix

 3,Multidimensional array

 4,character array

1,sample:

5,some functions for string manipulating

1,the functions below should be referenced

 2,puts

 3,gets

4,strcat

 5,strcpt

6,strcmp

7,strlen 

 8,strlwr/strupr


reasons:it is really inconvenient to define mutiple variblle

solution:You can store multiple variables of the same type in an array data structure

int a[10];
//10 :the element numbers in the array

E.g.

/*Assign the values of 10 array elements 
to 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 and require the output in reverse order*/
#include<stdio.h>
int main()
{
int i,a[10];
for(i=0;i<=9;i++)
    {
    a[i]=i;
    }
for(i=9;i>=0;i--)
    {
    printf("%d\n",a[i]);
    }
return 0;
}

Arrays always start at zero.

 1,initialization of one-dimensional arrays

1,syntax:
 

int a[5]={1,2,3,4,5};

2,some rules

1,the elements in an array are assigned sequentially

2,C# default unsigned varialble to null 

3,you can even just default the length of an array,if you assigned all of the elements

2,algorithm:bubble sort algorithm(起泡排序in Chinese)

eg.there are 10 places which is distinct in size,you need sort them from the smallest to the largest.

//there are 10 places which is distinct in size,
//you need sort them from the smallest to the largest.
#include<stdio.h>
int main()
{
int i,j,k,a[10];
printf("plz input 10 numbers")
for(i=0;i<10;i++)
    {
    scanf("%d\n",a[i]);
    }
for(i=0;i<10;i++)
    for(j=0;j=9-i;j++)
        {
        if(a[i]>a[i+1])
            {
            t=a[i];
            a[i]=a[i+1];
            a[i+1]=t
            }
        }
printf("the sorted numbers is:\n")
for(i=0;i<10;i++)
    printf("%d\n",a[i])
return 0;
}

P.S. why does it called"bubble sort"?

i think it is because every time the elements are compared with adjacent one from front ro back ,and the larger one gradually moved to the back just like  "bubbles".

2,Define and reference two-dimensional arrays(matrix)

1,define the type of a matrix 

float a[3][4]

a=

a[0] —— a[0][0] a[0][1] a[0][2] a[0][3] 
a[1] —— a[1][0] a[1][1] a[1][2] a[1][3] 
a[2] —— a[2][0] a[2][1] a[2][2] a[2][3]

2, initialization of matrix

1,syntax 

int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

or

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

 2,some tips:

1,Then the length of dimension 1 can be unassigned when defining the array, but the length of dimension 2 cannot be omitted.

2,You can assign initial values to some elements you want.

just like

int a[3][4]={{1},{0,6},{0,0,11}}

 3,Multidimensional array

float a[2, 3, 4];

the order of it:

a[0][0][0] → a[0][0][1] → a[0][0][2] → a[0][0][3] → 
a[0][1][0] → a[0][1][1] → a[0][1][2] → a[0][1][3] →
 a[0][2][0] → a[0][2][1] → a[0][2][2] → a[0][2][3] → 
a[1][0][0] → a[1][0][1] → a[1][0][2] → a[1][0][3] → 
a[1][1][0] → a[1][1][1] → a[1][1][2] → a[1][1][3] →
 a[1][2][0] → a[1][2][1] → a[1][2][2] → a[1][2][3]

 4,character array

1,sample:

char a[10];int i;
a[0]='f';a[1]='u';a[2]='c';a[3]='k';
for(i=0,i<4,i++)
    printf("%c",a[i]);
char c[]={′f′,′ ′ ,′u′,′c′,′ ′,′k′,′y′,′o′,′u′};

Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.print ()) to tell where the end of a string is. 

5,some functions for string manipulating

1,the functions below should be referenced

#include<string.h>

 2,puts

 you can use function <puts> to  output an string array.

#include <stdio.h>
int main()
{
	char str[]={"a\nb"};
	puts(str);
	return 0;
}
>>
a
b

 3,gets(不用调用string.h

and also , you can input some string in to the array.

E.G.

#include<stdio.h>
int main()
{
char string[100];
gets(string);
}

4,strcat

you can use fun. strcat to connect two string array.(注意内存问题)

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[30] = { "hello " };
	char str2[] = { "world" };
	strcat_s(str1, str2);
	printf("%s", str1);
	return 0;
}
>>
hello world

 例题:

C、 char str[10], *st="abcde"; strcat(str, st); 
D、 char str[10]="", *st="abcde"; strcat(str, st);

D正确 ,必须初始化

 5,strcpt

this function allow you to copy elements from one array to another.

E.G.

char str1[10], str2[]="a";
strcpy(str1, str2); 
//or
// strcpy(str1, "a");

6,strcmp

that function let you compare ascll value between two arrays

(from front to back)

1,syntax

strcmp(str1, str2);
strcmp("a", "b");
strcmp(str1, "b");

2,for the result

--if the char arrays is absolutely the same:it returns 0

--it is different:

1,a>b: return 1

2,a<b:return -1

7,strlen 

to get the length of a character array. before'\0'

#include <stdio.h>
#include <string.h>
int main()
{
	char str[10]="a";
	printf("%d,%d\n",strlen(str),strlen("a"));	
}
>>
0.5,0.5

 8,strlwr/strupr

to change low-case letters to capitals

-----strupr

to change capitals to low-case letters

-----strlwr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值