Create an array, initialization is complete, assignment, sorting, empty, to reverse the operation (C language)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/zz070/article/details/102685666

Topics requirements:
Create an array,
to achieve the function init () to initialize an array,
to achieve the function input () to array assignment,
achieve function swap () to sort the array,
to achieve reverse (inverse transpose function complete array elements),
implement empty () Clear array.
Requirements: own design function parameters, return values.

Ideas:
the main () function, call function has different functions complete the corresponding function; is branched by a switch statement, different operations controlled by the while loop continues.

Create Menu () operation function, this procedure can be listed, and the main () function in the while loop continues to call for the user to select.

After creating the print () function, operation function for performing various functions

Creating Init () function to initialize the array using a for loop, for all elements in the array initial value 0, and then call a print (after the array initialization function output).

Creating Input () function to assign an array, also use a for loop, scanf statement with input from the keyboard, all the elements in the array assignment, and then call a print (output array after the function assignment).

After creating Swap () function to sort the array, using two for loops, all elements in the array were bubble sort (ascending or descending order depending on the determination condition if statement), then call a print () function to sort the output array.

Creating Reverse () function, the array is reversed, use a while loop, all elements in the array were reversed (reversal operation carried out simply and last element in the array in turn exchange, is about the first and last exchange, the second in the penultimate exchange ······), then call a print () function output arrays after reversal.

Create Empty () function, empty array, so that the number of elements of the array is 0, then a call to print (the function output array empty).

Menu () function:

void Menu()
{
	printf("\t\t\t\t***********************************************\n");
	printf("\t\t\t\t*************1、Init    2、Input***************\n");
	printf("\t\t\t\t*************3、Swap    4、Reverse*************\n");
	printf("\t\t\t\t*************5、Empty   6、Exist***************\n");
	printf("\t\t\t\t***********************************************\n");
}

print () function:

void print(int a[], int n)
{
	for (int i = 0; i < n; i++){
		printf("%d ", a[i]);
	}
	printf("\n");
}

Init () function:

void Init(int a[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++){
		a[i] = 0;
	}
	printf("输出初始化后的数组:");
	print(a, n);

Input () function:

void Input(int a[], int n)
{
	printf("请输入您要输入的数组元素:");
	for (int i = 0; i < n; i++){
		scanf_s("%d", &a[i]);
	}
	printf("您输入的数组是:");
	print(a, n);
}

Swap () function:

void Swap(int a[], int n)
{
	int i;
	int j;
	for (j = 0; j < n - 1 ; j++){
		for (i = 0; i < n - j - 1; i++){
			if (a[i]>a[i + 1]){
				int temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
			}
		}
	}
	printf("输出排序后的数组:");
	print(a, n);
}

Reverse () function:

void Reverse(int a[], int n)
{
	int left=0;
	int right = n-1;
	printf("输出逆转前的数组:");
	print(a, n);
	while(left<right){
		int temp = a[left];
		a[left] = a[right];
		a[right] = temp;
		left++;
		right--;
	}
	printf("输出逆转后的数组:");
	print(a, n);
}

Empty () function:

void Empty(int a[], int n)
{
	n = 0;
	printf("输出初始化后的数组:");
	print(a, n);
	printf("此时数组为空!");
}

Source:

#include<stdio.h>
#include<windows.h>
void Menu()
{
	printf("\t\t\t\t***********************************************\n");
	printf("\t\t\t\t*************1、Init    2、Input***************\n");
	printf("\t\t\t\t*************3、Swap    4、Reverse*************\n");
	printf("\t\t\t\t*************5、Empty   6、Exist***************\n");
	printf("\t\t\t\t***********************************************\n");
}
void print(int a[], int n)
{
	for (int i = 0; i < n; i++){
		printf("%d ", a[i]);
	}
	printf("\n");
}
void Init(int a[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++){
		a[i] = 0;
	}
	printf("输出初始化后的数组:");
	print(a, n);
}
void Input(int a[], int n)
{
	printf("请输入您要输入的数组元素:");
	for (int i = 0; i < n; i++){
		scanf_s("%d", &a[i]);
	}
	printf("您输入的数组是:");
	print(a, n);
}
void Swap(int a[], int n)
{
	int i;
	int j;
	for (j = 0; j < n - 1 ; j++){
		for (i = 0; i < n - j - 1; i++){
			if (a[i]>a[i + 1]){
				int temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
			}
		}
	}
	printf("输出排序后的数组:");
	print(a, n);
}
void Reverse(int a[], int n)
{
	int left=0;
	int right = n-1;
	printf("输出逆转前的数组:");
	print(a, n);
	while(left<right){
		int temp = a[left];
		a[left] = a[right];
		a[right] = temp;
		left++;
		right--;
	}
	printf("输出逆转后的数组:");
	print(a, n);
}
void Empty(int a[], int n)
{
	n = 0;
	printf("输出初始化后的数组:");
	print(a, n);
	printf("此时数组为空!");
}
void main()
{
	int a[50];
	int n;
	int choose;
	printf("请输入数组元素的个数(不大于50个):");
	scanf_s("%d", &n);
	do{
		Menu();
		printf("请输入你的选择:");
		scanf_s("%d", &choose);
		switch (choose){
		case 1:
			Init(a, n);
			break;
		case 2:
			Input(a, n);
			break;
		case 3:
			Swap(a, n);
			break;
		case 4:
			Reverse(a, n);
			break;
		case 5:
			Empty(a, n);
			break;
		case 6:
			choose = 0;
			break;
		deflaut:
			printf("选择错误请重新选择!");
			break;
		}
	} while (choose);
	system("pause");
}

The result:
the implementation of his six feature from the start function 1 sequentially

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zz070/article/details/102685666