Programming contests

50 Programming Exercise for Beginners

January 2, 2019, 12:00 AM – December 31, 2019, 12:00 AM

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

  1. #ifndef _MYHEADER_H
  2. #define _MYHEADER_H 1
  3. double *average( double [], int, int );
  4. #endif /* myheader.h */
download as text file

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "myheader.h"
  4. #define N 4
  5. #define M 5
  6.  
  7. int main()
  8. {
  9.   double t[ N ][ M ] =
  10.     { {  2.25, -4.5,   8.0,   5.75, -13.25 },
  11.       {  6.75,  1.5,   2.25,  3.75,   8.0 },
  12.       { -3.5,   7.0,  -1.75, -6.25,  -3.75 },
  13.       { 12.0,  -6.25, 10.25,  0.0,   10.5 } };
  14.   int i, j;
  15.   double *p;
  16.   printf( "%d %d\n\n", N, M );
  17.   for ( i = 0; i < N; ++i )
  18.   {
  19.     for ( j = 0; j < M; ++j )
  20.     {
  21.       if ( j > 0 )
  22.         putchar( ' ' );
  23.       printf( "%.3f", t[ i ][ j ] );
  24.     }
  25.     putchar( '\n' );
  26.   }
  27.   putchar( '\n' );
  28.   p = average( &t[0][0], N, M );
  29.   for ( i = 0; i < N; ++i )
  30.   {
  31.     for ( j = 0; j < M; ++j )
  32.     {
  33.       if ( j > 0 )
  34.         putchar( ' ' );
  35.       printf( "%.3f", t[ i ][ j ] );
  36.     }
  37.     putchar( '\n' );
  38.   }
  39.   putchar( '\n' );
  40.   for ( j = 0; j < M; ++j )
  41.   {
  42.     if ( j > 0 )
  43.       putchar( ' ' );
  44.     printf( "%.4f", p[ j ] );
  45.   }
  46.   putchar( '\n' );
  47.   free( p );
  48.   return EXIT_SUCCESS;
  49. }
download as text file

Makefile

  1. CC = gcc
  2. OBJS = main.o average.o
  3. TARGETS = main
  4. all: $(TARGETS)
  5. main: main.o average.o
  6. $(CC) main.o average.o -o main
  7. main.o: main.c
  8. $(CC) -c main.c
  9. average.o: average.c
  10. $(CC) -c average.c
  11. clean:
  12. rm -rf $(OBJS) *~ $(TARGETS)
download as text file

Output for Sample Input

  1. 4 5
  2. 2.250 -4.500 8.000 5.750 -13.250
  3. 6.750 1.500 2.250 3.750 8.000
  4. -3.500 7.000 -1.750 -6.250 -3.750
  5. 12.000 -6.250 10.250 0.000 10.500
  6. 2.250 -4.500 8.000 5.750 -13.250
  7. 6.750 1.500 2.250 3.750 8.000
  8. -3.500 7.000 -1.750 -6.250 -3.750
  9. 12.000 -6.250 10.250 0.000 10.500
  10. 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.
University of Debrecen; Faculty of Informatics; v. 09/30/2024