10.2
#include
void copy_arr(double tr[], double s[], int n);
void copy_ptr(double * tr, double * s, int n);
void copy_ptrs(double * tr, double * s, int *p);
int main(void)
{
double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);
printf("target1: ");
for (int i = 0; i < 5; i++)
{
printf("%.2f ", target1[i]);
}
printf("\ntarget2: ");
for (int i = 0; i < 5; i++)
{
printf("%.2f ", target2[i]);
}
printf("\ntarget3: ");
for (int i = 0; i < 5; i++)
{
printf("%.2f ", target3[i]);
}
}
void copy_arr(double tr[], double s[], int n)
{
int i;
for (i = 0; i < n; i++)
{
tr[i] = s[i];
}
return 0;
}
void copy_ptr(double * tr, double * s, int n)
{
int i;
for (i = 0; i < n; i++)
*(tr + i) = *(s + i);
return 0;
}
void copy_ptrs(double * tr, double * s, int *p)
{
while(s < p)
{
*tr = *s;
tr++;
s++;
}
return 0;
}
10.3
//编写一个函数,返回储存在int类型数组中的最大值,并测试
#include
#define SIZE 5
int max(int s[], int n);
int main(void)
{
int nums[SIZE];
int i;
printf("Please enter %d integers: \n",SIZE);
for (i = 0; i < SIZE; i++)
scanf("%d", &nums[i]);
printf("The max number is %d.", max(nums,SIZE));
return 0;
}
int max(int s[],int n)
{
int max_num = 0;
for (int i = 1; i < SIZE; i++)
{
if (max_num < s[i])
max_num = s[i];
}
return max_num;
}
10.4
//编写一个函数,返回储存在double类型数组中的最大值的下标,并测试
#include
#define SIZE 5
int max(double nums[], int n);
int main()
{
int i;
double nums[SIZE];
printf("please enter %d double numbers: \n", SIZE);
for (i = 0; i < SIZE; i++)
scanf("%lf", &nums[i]);
printf("The index of the largest double number in arr a is %d\n", max(nums, SIZE));
return 0;
}
int max(double nums[], int n)
{
int i, j = 0;
double max = 0;
for (i = 0; i < n; i++)
if (max < nums[i])
{
max = nums[i];
j = i;
}
return j;
}
10.5
//编写函数,返回储存在double类型数组中最大值与最小值的差值
#include
#define SIZE 5
double d_value(double nums[], int n);
int main(void)
{
int i;
double nums[SIZE];
printf("Please enter %d integers: \n", SIZE);
for (i = 0; i < SIZE; i++)
scanf("%lf", &nums[i]);
printf("The different value between the max and min: %.2lf\n", d_value(nums, SIZE));
return 0;
}
double d_value(double nums[], int n)
{
double max = 0;
double min = nums[0];
int i;
for (i = 0; i < n; i++)
if (max < nums[i])
max = nums[i];
for (i = 0; i < n; i++)
if (min > nums[i])
min = nums[i];
return max - min;
}
10.6
//编写一个函数,把double类型数组中的数据倒序排列,并测试
#include
#define SIZE 5
void reverse(double n[], int m);
int main(void)
{
double nums[SIZE];
int i;
printf("Please enter %d integers: \n", SIZE);
for (i = 0; i < SIZE; i++)
scanf("%lf", &nums[i]);
printf("The reverse numbers is : \n");
reverse(nums, SIZE);
return 0;
}
void reverse(double n[], int m)
{
int i, j;
double temp;
for (i = 0, j = m-1; i < j; i++, j--)
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
for (i = 0; i < m; i++)
printf("%.2lf ", n[i]);
}
10.7
先发了,后面重复代码工作量太大了,思路跟同上面的题目
有空再更。