Arrays and functions
1. Given an array of characters, count all the English characters in the array, such as 3 in "123FDD".
Array as the interface parameter of the function, the length of the array is missing
#include <string.h>
int Numberofcharacter (char src[], int len)
{
int cnt=0;
for (int i=0; i<len; i++) {
if ((src[i]>= ' A ' && src[i]<= ' Z ') | | (src[i]>= ' A ' && src[i]<= ' z ')) {
cnt++;
}
}
return CNT;
}
/*int Main (int argc, const char * argv[]) {
Char str[200];
int len=0;
for (int i=0; i<200; i++) {
scanf ("%c", &str[i]);
if (str[i]== ' \ n ') {
str[i]= ' + ';
Break
}
len++;
}
scanf ("%s", str);
int len = (int) strlen (str);//Find the number of valid characters in a string
printf ("%d\n", Numberofcharacter (Str,len));
return 0;
}*/
Determines whether an integer array is a symmetric array, for example {1,2,3,3,2,1} and {1,6,8,1,8,6,1} are symmetric arrays.
#include <stdbool.h>
BOOL Issymmetricarray (int src[], int len)
//{
int i=0;
for (i=0; i<len/2; i++) {
if (Src[i]!=src[len-i-1]) {
Break
// }
// }
if (I==LEN/2) {
return true;
// }
return false;
//}
int main (int argc,const char *argv[])
//{
int a[]={1,6,8,1,8,6,1};
printf ("%d\n", Issymmetricarray (a,7));
//
return 0;
//}
Array sorting
Bubble sort from small to large
9 7 8 5 6--5 6 7 8 9
First time sort
7 9 8) 5 6
7 8 9) 5 6
7 8 5) 9 6
7 8 5) 6 9
Second sort
7 8 5) 6 9
7 5 8) 6 9
7 5 6) 8 9
Third sort
5 7 6) 8 9
5 6 7) 8 9
Fourth time sort
5 6 7) 8 9
/*int Main (int argc,const char *argv[])
{
int a[5]={9,7,8,5,6};
for (int i=0; i<5-1; i++) {//controls the number of sorts
for (int j=0; j<5-1-i; J + +) {//Control interchange number of times
if (A[j]>a[j+1]) {
int temp = A[j];
A[J]=A[J+1];
A[j+1]=temp;
// }
// }
for (int j=1; j<5-i; j + +)
{
if (A[j-1]>a[j]) {
int temp = a[j-1];
A[J-1]=A[J];
A[j]=temp;
}
}
}
for (int i=0; i<5; i++) {
printf ("%d", a[i]);
}
printf ("\ n");
}
*/
Select sort
9 7 8) 5 6
Find the maximum value
First time sort
6 7 8) 5 9
Second sort
6 7 5) 8 9
Third sort
6 5 7) 8 9
Fourth time sort
5 6 7) 8 9
int main (int argc,const char *argv[])
//{
int a[5]={9,7,8,5,6};
//
for (int i=5-1; i>0; i--) {
int k = i;
for (int j=i-1; j>=0; j--) {
if (A[k]<a[j]) {
K=j;
// }
// }
if (k!=i) {
int temp = A[k];
A[k]=a[i];
A[i]=temp;
// }
// }
for (int i=0; i<5; i++) {
printf ("%d", a[i]);
// }
Putchar (' \ n ');
//
return 0;
//}
//
9 7 8) 5 6
Find Minimum value
K = i;
First time sort
5 7 8) 9 6
Second sort
5 6 8) 9 7
Third sort
5 6 7) 9 8
Fourth time sort
5 6 7) 8 9
int main (int argc,const char *argv[])
//{
int a[5]={9,7,8,5,6};
//
for (int i=0; i<5-1; i++) {
int k=i;
for (int j=i+1; j<5; J + +) {
if (A[k]>a[j]) {
K=j;
// }
// }
if (k!=i) {
int temp = A[k];
A[k]=a[i];
A[i]=temp;
// }
// }
//
for (int i=0; i<5; i++) {
printf ("%d", a[i]);
// }
Putchar (' \ n ');
return 0;
//}
Insert Sort
9 7 8) 5 6
First time sort
9 9 8) 5 6
7 9 8) 5 6
Second sort
7 9 9) 5 6
7 8 9) 5 6
Third sort
7 8 9) 9 6
7 8 8) 9 6
7 7 8) 9 6
5 7 8) 9 6
Fourth time sort
5 7 8) 9 9
5 7 8) 8 9
5 7 7) 8 9
5 6 7) 8 9
//
int main (int argc,const char *argv[])
//{
int a[5]={9,7,8,5,6};
for (int i=1; i<5; i++) {
int temp = A[i];
int j=i-1;
while (j>=0 && a[j]>temp) {
A[J+1]=A[J];
j--;
// }
A[j+1]=temp;
// }
for (int i=0; i<5; i++) {
printf ("%d", a[i]);
// }
Putchar (' \ n ');
//
return 0;
//}
How to enter an indefinite number of array elements
int main (int argc,const char *argv[])
//{
int a[100];
int cnt=0;
for (int i=0; i<100; i++) {
scanf ("%d", &a[i]);
cnt++;
Method One
if (getchar () = = ' \ n ') {
Break
//// }
Method Two
Char ch;
if (scanf ("%c", &ch), ch== ' \ n ') {
Break
// }
// }
for (int i=0; i<cnt; i++) {
printf ("%d", a[i]);
// }
//
return 0;
//}
A two-dimensional array can be seen as an array of one-dimensional array elements
int a[3][2];
//
Initialization of two-dimensional arrays
Method One
int main (int argc,const char *argv[])
//{
int a[3][2]={1,2,3,4,5,6};
//
for (int i=0; i<3; i++) {
for (int j=0; j<2; J + +) {
printf ("%d", a[i][j]);
// }
printf ("\ n");
// }
return 0;
//}
Method Two
int main (int argc,const char *argv[])
//{
int a[3][2]={{1,2},
{3,4},
{5,6}};
//
for (int i=0; i<3; i++) {
for (int j=0; j<2; J + +) {
printf ("%d", a[i][j]);
// }
printf ("\ n");
// }
return 0;
//}
Print Yang Hui triangle
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1
/*int Main (int argc,const char *argv[])
{
int a[10][10]={};
for (int i=0; i<10; i++) {
for (int j=0; j<i; J + +) {
if (j==0 | | j==i) {
A[i][j]=1;
}
Else
{
A[I][J]=A[I-1][J]+A[I-1][J-1];
}
}
}
for (int i=0; i<10; i++) {
for (int j=0; j<i; J + +) {
printf ("%4d", A[i][j]);
}
printf ("\ n");
}
return 0;
}*/
int main (int argc,const char *argv[])
{
int a[10][10]={};
for (int i=0; i<10; i++) {
for (int j=0; j<=i; J + +) {
if (j==0 | | j==i) {
A[i][j]=1;
}
Else
{
A[I][J]=A[I-1][J]+A[I-1][J-1];
}
}
}
for (int i=0; i<10; i++) {
printf ("%*d", 26-2*i,a[i][0]);
for (int j=1; j<=i; J + +) {
printf ("%4d", A[i][j]);
}
printf ("\ n");
}
return 0;
}
ios-c_day7___ arrays and two-dimensional arrays