#include <stdio.h>
void exchange(int arr1[], int arr2[])
{
int t,j;
for (j = 0; j < 10; j++)
{
t = arr1[j];
arr1[j] = arr2[j];
arr2[j] = t;
}
}
int main()
{
int arr1[10] = { 1, 4, 5, 2, 6, 7, 9, 10, 3, 8 };
int arr2[10] = { 3, 2, 5, 4, 6, 7, 1, 8, 9, 10 };
int i;
exchange(arr1,arr2);
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
第2题??创建一个整形数组,完成对数组的操作
- 实现函数init()?初始化数组为全0
- 实现print()??打印数组的每个元素
- 实现reverse()??函数完成数组元素的逆置。
要求:自己设计以上函数的参数,返回值。
#include <stdio.h>
//初始化数组
void init(int arr1[],int s)
{
int i;
for (i = 0; i < s; i++)
{
arr1[i] = 0;
}
}
//打印数组每个元素
void print(int a[], int s)
{
int i;
for (i = 0; i < s; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
//元素逆置
void reverse(int arr2[], int s)
{
int t = 0;
int left = 0;
int right = s - 1;
while (left <= right)
{
t = arr2[left];
arr2[left] = arr2[right];
arr2[right] = t;
left++;
right--;
}
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int s = sizeof(arr) / sizeof(arr[0]);
init(arr,s);
print(arr,s);
reverse(arr,s);
return 0;
}
#include <stdio.h>
void paixu(int arr1[],int s)
{
int i;
int t = 0;
int flag = 1;
for (i = 0; i < s - 1; i++)
{
int j = 0;
for (j = 0; j < s - 1 - i; j++)
{
if (arr1[j] > arr1[j + 1])
{
t = arr1[j + 1];
arr1[j + 1] = arr1[j];
arr1[j] = t;
flag = 0;
}
}
if (flag == 1)
break;
}
}
int main()
{
int k, s;
int arr1[10] = { 10, 4, 2, 6, 7, 9, 12, 3, 5, 1 };
s = sizeof(arr1) / sizeof(arr1[0]);
paixu(arr1,s);
for (k = 0; k < s; k++)
{
printf("%d ", arr1[k]);
}
return 0;
}
|