First knowledge of C language = = > array exercise sharing

Keywords: C

Learning record DAY21

I haven't recorded my blog for a long time. Today, I'll record the contents of the array exercise I learned yesterday

Array exercises

I learned a lot of exercises yesterday. Here are only a few important and typical contents that I personally think are recorded

(1) Bad reference array

Title:

int arr[10]={0};

The following incorrect references are:

A. arr[0]=1
B. arr[0]=5*2
C. arr[10]=2
D. arr[1]=arr[2]*arr[0]

arr[10] represents that there are 10 elements in the array, but the reference of arr[10] is a subscript, which refers to the 11th element

This is outside the range of 10 elements of the array

The answer is C

(2) Comma expression in array

What is the result of the following code output?

#include<stdio.h>
int main()
{
	int arr[]={1,2,(3,4),5};
    printf("%d\n", sizeof(arr));
    return 0;
}

First, each element in the arr array is an integer element, and the occupied bytes are 4 bit s

So sizeof calculates the number of elements in the array and adds the space they occupy

  • The comma expression (3,4) ---- > produces only one result, 4

Just in case, let's mention the concept of comma expression

comma expression

The contents of a comma expression can be numbers or expressions

#include<stdio.h>
int main()
{
	int a =3;
    int b=5;
    int c=0;
    //comma expression 
    int d=(c=1,a=c+3,b=a-4,c+=b);
    printf("%d\n",d);
    return 0;
}

The result of this code is 1

The operation process is as follows

  • c=1
  • a=c+3=1+3=4
  • b=a-4=4-4=0
  • c+=b ----> c=c+b=1+0=1

The comma expression evaluates each expression from left to right

The output result is the result of the last expression in the comma expression

Therefore, the comma expression (3,4) in the above array produces a result of 4

The result of printf output is 16 (4 elements)

(3) The difference between string and character elements in an array

int arr1[]={"abcdef"};
int arr2[]={'a','b','c','d','e','f'};
  • Arrays arr1 and arr2 are equivalent ×

One is a string and the other is a character element. Their meanings are different

  • Arrays arr1 and arr2 have the same length ×

The two are actually different in length

Because there is a string end flag \ 0 after the string "abcdef"

  • The length of array arr1 is greater than the length of arr2 √

(I may have made a mistake here. Please correct me mercilessly)

(4) Simple exercises for custom functions and arrays

The title is as follows

Create an integer array to do the following

1. The implementation function init() initializes the array to all zeros

2. Implement print() to print each element in the array

3. Implement the reverse() function to complete the inverse of array elements

The implementation of the first two custom functions is very similar

We only need to use a for loop, use i to access each element in the array in turn, print them or assign a value of 0 to initialize

void print(int arr[], int sz)//Prints each element in the array
{
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
void init(int arr[], int sz)//Initializes each element in the array to 0
{
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        arr[i] = 0;
    }
}

One point to note here is that sz (number of array elements) must be calculated in the main function, not in the user-defined function

Because the passed array is the address of the first element, the length of the array element calculated by sizeof is inaccurate

The main function main is as follows

int main()
{
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int sz = sizeof (arr)/sizeof( arr[0]);
    printf("%d\n", sz);

    print(arr, sz);
    reverse(arr, sz);
    print(arr, sz);

    init(arr, sz);
    print(arr, sz);

    return 0;
}

The basic idea of the reverse function is actually similar to the other two functions

Here we will still use the "empty box" tmp to complete our exchange

At the same time, we need to use the loop to confirm when to stop the exchange

That is, when the subscripts of the left and right array elements are the same (or the values are the same), the exchange will stop

void reverse(int arr[], int sz)//Inverse of array elements
{
    int left = 0;
    int right = sz - 1;
    while(left < right)
    {
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left++;
        right--;
    }
}

When we exchange elements between two arrays, we also need to exchange the corresponding subscript elements of each array one by one in the way of for loop + tmp empty box

  • Premise: the number of elements of the two arrays is the same

The following code is wrong

int tmp=arr1
arr1=arr2
arr2=tmp

The implementation form is the same as that of the user-defined functions printed and initialized above, which will not be repeated here!

If this blog is helpful to you, please like it!
This is my greatest encouragement

Posted by Bhaal on Mon, 13 Sep 2021 19:28:17 -0700