C语言,做4个题,用数组和指针

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:14:43
第一题
Write a program that initialises an array of double and then copies the contents of the array into two other arrays. Declare all arrays in the main program.
To make the first copy, make a function, which uses the array notation (the square brackets []) to access the elements of the array.
To make the second copy, write a function that uses the pointer notation and pointer incrementing to access the elements of the arrays.
Have each function take as function arguments the name of the target array and the number of elements to be copied.
Here is an example showing how the functions should be called, given the following declarations:
double source[4] = {1,2.3,4.5,6.7};
double destination1[4];
double destination2[4];
copy_array(source, destination, 4);
copy_ptr(source, destination, 4);
第二题
Write a function that returns the difference between the largest and smallest elements in an array of double. Test the function in a prog

#include<stdio.h>

//第一题函数,数组形参的复制
void copy_array( double source[], double destination[], int len)
{
int i=0;
for(; i<len; i++)
{
destination[i]=source[i];
}
}

//第一题函数,指针形参的复制
void copy_ptr( double *source, double *destination, int len)
{
int i=0;
for(; i<len; i++)
{
*(destination+i) = *(source+i);
}
}

//第二题函数,最大值最小值之差
double dif( double array[], int len )
{
double max=*array, min=*array;
int i=0;
for(; i<len; i++)
{
if( max<*(array+i) )
max = *(array+i);
else
min = *(array+i);
}
return (max-min);
}

//第四题函数,两个等长数组相加,保存在第三个数组
void add( double result[], double arg1[], double arg2[], int len )
{
int i=0;
for(; i<len; i++)
{
*(result+i)=*(arg1+i)+*(arg2+i);
}
}

int main(void)
{
double s