|
An Average Task
Write a function that takes as parameters the address of a two-dimensional array containing double-precision real numbers and the number of rows and columns.
The function should create a new one-dimensional array of double-precision real numbers that contains the average of the numbers in the columns of the original array and return the address of that array. The original array must not be changed.
The Specification of the Function
double *average( double [], int, int );
Note
Place the function in a file named average.c and submit this file as a solution to the evaluation system. You can test your solution using the following files. The evaluation system does not necessarily perform the evaluation using these files.
myheader.h
#ifndef _MYHEADER_H #define _MYHEADER_H 1
double *average( double [], int, int );
#endif /* myheader.h */
download as text file
main.c
#include <stdio.h> #include <stdlib.h> #include "myheader.h"
#define N 4 #define M 5 int main() { double t[ N ][ M ] = { { 2.25, -4.5, 8.0, 5.75, -13.25 }, { 6.75, 1.5, 2.25, 3.75, 8.0 }, { -3.5, 7.0, -1.75, -6.25, -3.75 }, { 12.0, -6.25, 10.25, 0.0, 10.5 } }; int i, j; double *p;
printf( "%d %d\n\n", N, M ); for ( i = 0; i < N; ++i ) { for ( j = 0; j < M; ++j ) { if ( j > 0 ) putchar( ' ' ); printf( "%.3f", t[ i ][ j ] ); } putchar( '\n' ); } putchar( '\n' ); p = average( &t[0][0], N, M ); for ( i = 0; i < N; ++i ) { for ( j = 0; j < M; ++j ) { if ( j > 0 ) putchar( ' ' ); printf( "%.3f", t[ i ][ j ] ); } putchar( '\n' ); } putchar( '\n' ); for ( j = 0; j < M; ++j ) { if ( j > 0 ) putchar( ' ' ); printf( "%.4f", p[ j ] ); } putchar( '\n' ); free( p ); return EXIT_SUCCESS; }
download as text file
Makefile
CC = gcc OBJS = main.o average.o TARGETS = main
all: $(TARGETS)
main: main.o average.o $(CC) main.o average.o -o main
main.o: main.c $(CC) -c main.c
average.o: average.c $(CC) -c average.c
clean: rm -rf $(OBJS) *~ $(TARGETS)
download as text file
Output for Sample Input
4 5
2.250 -4.500 8.000 5.750 -13.250 6.750 1.500 2.250 3.750 8.000 -3.500 7.000 -1.750 -6.250 -3.750 12.000 -6.250 10.250 0.000 10.500
2.250 -4.500 8.000 5.750 -13.250 6.750 1.500 2.250 3.750 8.000 -3.500 7.000 -1.750 -6.250 -3.750 12.000 -6.250 10.250 0.000 10.500
4.3750 -0.5625 4.6875 0.8125 0.3750
download as text file
Acknowledgement
This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
|
|