利用数组,正序输入十个数,倒序输出;利用两个程序文件
#include <stdio.h>void main()
{
void reverse(int a[],int n);
int a,i;
printf("Please input 10 numbers:");
for(i = 0;i < 10;i++)
{
scanf("%d",&a);
}
reverse(a,10);
for(i = 0;i < 10;i++)
{
printf("%d ",a);
}
}
#include <stdio.h>
void reverse(int a[],int n)
{
int temp,m,*i,*j,*p;
m = (n - 1)/2;
j = a + n - 1;
i = a;
p = a + m;
for(;i < p+1;i++,j--)
{
temp = *i;
*i = *j;
*j = temp;
}
} 本帖最后由 bin554385863 于 2019-10-30 20:45 编辑
#include <stdio.h>
void InputArray(int arr[], size_t arr_size) //创建数组
{
for (size_t i = 0; i < arr_size; i++)
{
scanf("%d", &arr);
}
}
void SortArray(int arr[], size_t arr_size) //冒泡
{
for (size_t i = 0; i < arr_size; i++)
{
for (size_t j = 0; j < i; j++)
{
if (arr > arr)
{
int tmp = arr;
arr = arr;
arr = tmp;
}
}
}
}
void outputArray(int arr[], int arr_size, char flag) //输出
{
if ((flag != '+') && (flag != '-'))
{
printf("Parameter Error");
}
else if (flag == '+') //+号表示正序输出;
{
for (size_t i = 0; i < arr_size; i++)
{
printf("%d ", arr);
}
printf("\n");
}
else if (flag == '-')
{
for (size_t j = arr_size -1; j != -1; j--) //-号代表反序
{
printf("%d ", arr);
}
printf("\n");
}
}
void reverse(int arr[], int arr_size)//翻转数组
{
for (size_t i = arr_size -1; i != -1; i--)
{
int tmp = arr;
arr = tmp;
}
}
int main(int argc, char const *argv[])
{
int arr = {0};
InputArray(arr, 5);
SortArray(arr, 5);
printf("UP\n");
outputArray(arr, 5, '+');
printf("DOWN\n");
outputArray(arr, 5, '-');
return 0;
}
---------------------------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-fjbfyzrh.lgp --stdout=Microsoft-MIEngine-Out-lpm5sbkr.0tp --stderr=Microsoft-MIEngine-Error-1gv22jtx.vuy --pid=Microsoft-MIEngine-Pid-tyuk4jfi.rnc "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ytznha1m.a3y --stdout=Microsoft-MIEngine-Out-mr5tkgrt.ugy --stderr=Microsoft-MIEngine-Error-jey2wj1x.zvl --pid=Microsoft-MIEngine-Pid-dxs4cjcs.qee "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
6 5 3 2 0
UP
0 2 3 5 6
DOWN
6 5 3 2 0
E:\Users\86184\Documents\Code>
============================================
其实反序输出只要排序号的数组尾部遍历就行
页:
[1]