The array is initialized to empty, the inverse position (function method)

The array is initialized to empty, the inverse position (function method)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

//初始化数组
void init(int arr[], int sz, int x)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		arr[i] = x;

	}
}
//打印数组
void print_f(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i <sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
//清空数组
void empty(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		arr[i] = 0;
	}
}
int main()
{


	int arr[10] = { 2, 4, 6, 8, 0, 1, 3, 5, 7, 9 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	
	printf("请输入初始化的值\n");
	int n = 0;
	scanf("%d", &n);
	
	init(arr, sz, n);//初始化数组
	print_f(arr, sz);//打印数组
	printf("\n");
	printf("以下为清空数组\n");//自定义清空数组就是把数组的值改为0
	empty(arr, sz);
	print_f(arr, sz);
	
	system("pause");
	return 0;
}

Here Insert Picture Description

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

//逆置数组  两两交换
void reverse(int arr[], int sz)
{
    int left = 0;
	int right = sz - 1;
	while (left<right)
	{
		int temp = 0;
		temp = arr[left];
		arr[left] = arr[right];
		arr[right] = temp;
		left++;
		right--;
	}
	
	
}
int main()
{
    int arr[10] = { 2, 4, 6, 8, 0, 1, 3, 5, 7, 9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
   printf("以下为数组逆置\n");
	reverse(arr, sz);
	print_f(arr, sz); 
    system("pause");
	return 0;
}

Here Insert Picture Description
The core set of inverse array is twenty-two exchange

Explanation

1. Array transmission parameter groups actually passed is the address of the first element
so that parameter must be a pointer. int arr [] its essence is a pointer.
2. int arr [] on 32-bit platform is actually a four-byte
64-bit 8 bytes
if the size required within the function of
int SZ = the sizeof (ARR) / the sizeof (ARR [0]);
. 4 / =. 1. 4
thus length is 1 byte

Important conclusions

Internal function obtains number of array elements is not part of the parameters

Published 22 original articles · won praise 5 · Views 3202

Guess you like

Origin blog.csdn.net/weixin_45271990/article/details/104346814