Classical C Programming array and programming Array

Source: Internet
Author: User

Classical C Programming array and programming Array

1. Element swapping

Define an integer array num [10], initialize the element values in the array at random, swap the elements at the beginning and end, and output the exchanged array values.

[Code]

#include <stdio.h>int main(void){    int num[10] = {};     int i = 0;    int temp;    for(i = 0; i < 10; i++)    {           scanf("%d", &num[i]);    }       for(i = 0; i < 10 / 2; i++)    {           temp = num[i];        num[i] = num[9 - i];         num[9 - i] = temp;    }       for(i = 0; i < 10; i++)    {           printf("%d ", num[i]);    }       printf("\n");    return 0;}


2. Matrix flip

Define a two-dimensional array a [3] [4], randomly initialize the element values in the array, and then flip array a to B [4] [3, and output B.

[Analysis]

Input:

3 5 1 4

7 2 6 8

0 9 4 6

Output:

3 7 0

5 2 9

1 6 4

4 8 6

[Code]

#include <stdio.h>int main(void){    int a[3][4] = {};     int b[4][3] = {};     int i = 0;    int j = 0;    int temp;    for(i = 0; i < 3; i++)    {           for(j = 0; j < 4; j++)        {               scanf("%d", &a[i][j]);        }       }       for(i = 0; i < 3; i++)    {           for(j = 0; j < 4; j++)        {               b[j][i] = a[i][j];        }       }       for(i = 0; i < 4; i++)    {           for(j = 0; j < 3; j++)        {               printf("%d ", b[i][j]);        }           printf("\n");    }       printf("\n");    return 0;}

3. Number of perimeter reports

A circle of m people starts to report data, report data to n, and exit. The last question is the number. (Starting from 1st)

[Analysis]

Store m individuals in an array a [m], and record all the elements as 1. When the number starts to report to n, the individual exits, set the value of this element to 0. When the number is reported to the last element of the array, the first element is returned. Repeat this until the last element with a value of 1 is left.

[Code]

# Include <stdio. h> int main (void) {int m = 0; // count int n = 0; // report int a [10] = {}; int I = 0, j = 0, k = 0; // k indicates the current number of people out of the circle // printf ("input:"); scanf ("% d", & m, & n); // printf ("input:"); for (I = 1; I <= m; I ++) {a [I] = 1 ;} I = 1; while (1) {if (a [I] = 1) {j ++;} if (j = n) {a [I] = 0; j = 0; k ++;} if (k = m-1) break; I ++; if (I> m) {I = 1 ;}} // printf ("output: \ n"); for (I = 1; I <= m; I ++) {if (a [I] = 1) printf ("% d \ n", I);} return 0 ;}

4. ascending and descending sequence

Input 10 numbers. If any two numbers are different, all the ascending and descending sequences are output.

[Analysis]

Input:

1 5 9 8 12 21 3 0-1 9

Output:

1 5 9

9 8

8 12 21

21 3 0-1

-1 9

[Code]

#include <stdio.h>int main(void){    int i, k = -1;     int a[10];    for(i = 0; i < 10; i++)    {           scanf("%d", &a[i]);    }       for(i = 1; i < 10; i++)    {           if(k == (a[i] > a[i - 1]))        {               printf(" %d", a[i]);        }           else        {               k = (a[i] > a[i - 1]);            printf("\n%d %d", a[i - 1], a[i]);        }       }       printf("\n");    return 0;}

5. Maximum number of occurrences

Enter 10 numbers to find the maximum number of occurrences (if there are multiple parallel entries, output them in the order of numbers)

[Analysis]

Input:

1 5 27 33 24 1 27 18 19 20

Output:

1 27

[Code]

#include <stdio.h>int main(void){    int a[10];    int i = 0, j = 0;    int count[10] = {1,1,1,1,1,1,1,1,1,1};    int max = 0;        for(i = 0; i < 10; i++)    {           scanf("%d", &a[i]);    }       for(i = 0; i < 9; i++)    {           //count[i] = 0;        for(j = i + 1; j < 10; j++)        {               if(a[i] == a[j])            {                   if(count[i] != 0)                {                       count[i] ++;                 }                   count[j] = 0;            }           }    /*    for(i = 0; i < 10; i++)    {        if(count[i] != 0)        {            printf("%d ", a[i]);        }    }*/    max = 0;    for(i = 1; i < 10; i++)    {        if(count[i] > count[max])        {            max = i;        }    }    for(i = 0; i < 10; i++)    {        if(count[i] == count[max])        {            printf("%d ", a[i]);        }    }    return 0;}

6. Spiral NxN Matrix

Enter two numbers. The first number determines the nXn matrix. The second number determines the value assignment starting from 1 and the upper limit of the value assignment.

[Analysis]

Input:

5 23

Output:

1 2 3 4 5

16 17 18 19 6

15 0 0 20 7

14 23 22 21 8

13 12 11 10 9

[Code]

#include <stdio.h>int main(void){    int i, j, r = 1;    int k = 1;    int n, m;    int a[20][20] = {};             scanf("%d %d", &n, &m);    for(j = 0; j < n; j++)    {           a[0][j] = k++;    }       for(i = n - 1; i >= n / 2; i --)     {           for(j = r; j <= i; j++)        {               a[j][i] = k++;        }           for(j = i; j >= r; j--)        {               a[i][j - 1] = k++;        }           for(j = i; j > r; j--)        {               a[j - 1][r - 1] = k++;        }           for(j = r; j < i; j++)        {               a[r][j] = k++;        }           r ++;     }     for(i = 0; i < n; i ++)    {        for(j = 0; j < n; j ++)        {            if(a[i][j] > m)            {                a[i][j] = 0;            }            printf("%3d", a[i][j]);        }        printf("\n");    }    return 0;}

7. array Interpolation

There is a sorted array. Enter a number to insert it into the array according to the original rule.

[Difficulty coefficient] ▲

[Analysis]

[Code]

# Include <stdio. h> int main (void) {int arr [5] = {1, 2, 3, 5, 6}; int I = 0; int index = 0; int num = 0; scanf ("% d", & num); // locate the position to be inserted for (I = 0; I <5; I ++) {if (num <arr [I]) {index = I; break ;}// if num should be inserted with a subscript of 0 ~ 4, then insert // otherwise do not insert if (I! = 5) {for (I = 4; I> index; I --) {arr [I] = arr [I-1];} arr [index] = num ;} for (I = 0; I <5; I ++) {printf ("% d", arr [I]);} printf ("\ n "); return 0 ;}

8. array Reverse Order

Returns an array in reverse order.

[Difficulty coefficient] ▲

[Analysis]

[Code]

#include <stdio.h>int main(void){int arr[] = {1,2,3,4,5,6};int i = 0;int temp = 0;int len = sizeof(arr)/sizeof(arr[0]);for(i = 0; i < len/2; i ++){temp = arr[i];arr[i] = arr[len - 1 - i];arr[len - 1 - i] = temp;}for(i = 0; i < len; i ++){printf("%d ", arr[i]);}printf("\n");return 0;}

9. Yang Hui triangle

Print out the Yang Hui triangle (10 rows are required, as shown in)

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

[Difficulty coefficient] ▲▲▲▲▲

[Analysis]

Yang Hui triangle:

Rule:

When the first column or the row and column are equal, the value is 1.

Otherwise, a [I] [j] = a [I-1] [j-1] + a [I-1] [j]

[Code]

#include <stdio.h>int main(void){int arr[10][10] = {};int i, j;for(i = 0; i < 10; i ++){for(j = 0; j <= i; j ++){if(j == 0 || i == j)arr[i][j] = 1;elsearr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];printf("%4d", arr[i][j]);}printf("\n");}return 0;}

10. Tail front

There are n integers, so that the previous numbers are shifted to the next m positions, and the last m number is changed to the first m number.

[Difficulty coefficient] ▲▲▲▲

[Analysis]

C language: When declaring an array, the length of the array must be a constant, but the gcc compiler allows the declaration of an array containing variables. However, before declaring an array, the variable must be assigned a value and once declared, the length of the array cannot be changed. The array cannot be initialized when it is declared, but can be assigned a value later.

[Code]

#include <stdio.h>void arr_move(int arr[], int len, int m){int i = 0, j = 0;int temp = 0;for(i = len - m; i < len; i ++){temp = arr[i];for(j = i; j > i - (len - m); j --){arr[j] = arr[j - 1];}arr[j] = temp;}}int main(void){int n = 0;int i = 0;int m = 0;scanf("%d", &n);int arr[n];for(i = 0; i < n; i ++){arr[i] = i + 1;printf("%d ", arr[i]);}printf("\n");scanf("%d", &m);arr_move(arr, n, m);for(i = 0; i < n; i ++){printf("%d ", arr[i]);}printf("\n");return 0;}

11. Statistical string

Enter a line of characters. The maximum length is 100. count the number of strings, not punctuation. For example:

Input: Hi, Welcome to saif !!

Output: 4

[Difficulty coefficient] ▲

[Analysis]

[Code]

#include <stdio.h>int is_alpha(char ch){if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') return 1;elsereturn 0;}int is_digit(char ch){if(ch >= '0' && ch <= '9')return 1;elsereturn 0;}int is_biaodian(char ch){if(ch < '0' || ch > '9' && ch < 'A' || ch > 'Z' && ch < 'a' || ch > 'z')return 1;else return 0;}int main(void){char str[100] = {};int counter = 0;int i = 0;scanf("%[^\n]", str);while(str[i] != '\0'){if((is_alpha(str[i]) || is_digit(str[i]))&& is_biaodian(str[i + 1])){counter ++;}i ++;}printf("\n%d\n", counter);return 0;}

12. Find the longest string

Enter a line of characters to find the word with the maximum length. Enter a line of characters from the keyboard to print the string with the maximum length. For example:

Input: when I was young I 'd listen to the radio waiting for my favorite songs

Output: favorite

[Difficulty coefficient] ▲▲▲▲

[Analysis]

[Code] 

#include <stdio.h>int main(void){char str[100] = {};int i = 0;int counter = 0;int max = 0;int index = 0;int pos = 0;scanf("%[^\n]", str);while(1){if(str[i] != ' '){counter ++;printf("counter = %d\n", counter);}if(str[i] == ' ' || str[i] == '\0'){if(max < counter){max = counter;index = pos;printf("max = %d, index = %d\n", max, index);}}if(str[i] == ' ' && str[i + 1] != ' '){pos = i + 1;counter = 0;printf("pos = %d\n", pos);}if(str[i] == '\0')break;i ++;}i = index;while(str[i] != ' ' && str[i] != '\0'){printf("%c", str[i]);i ++;}printf("\n");return 0;}

13. Matrix transpose

For example:

1 2 3 4 1 5 3 4

5 6 7 8-> 2 6 2 7

3 2 5 9 3 7 5 2

4 7 2 3 4 8 9 3

[Difficulty coefficient] ▲▲▲▲

[Analysis]

[Code]

#include <stdio.h>int main(void){int n = 0;int i, j;int temp = 0;scanf("%d", &n);int arr[n][n];for(i = 0; i < n; i ++){for(j = 0; j < n; j ++){scanf("%d", &arr[i][j]);}}for(i = 0; i < n; i ++){for(j = 0; j < n; j ++){printf("%d", arr[i][j]);}printf("\n");}for(i = 0; i < n; i ++){for(j = 0; j < i; j ++){temp = arr[i][j];arr[i][j] = arr[j][i];arr[j][i] = temp;}}for(i = 0; i < n; i ++){for(j = 0; j < n; j ++){printf("%d", arr[i][j]);}printf("\n");}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.