Programming contests

50 Programming Exercise for Beginners

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

Rolling Sum

Write a function that takes the address of a two-dimensional array containing double-precision real numbers and the number of rows and columns as parameters. The function should create a new one-dimensional array of double-precision real numbers with as many elements as the number of rows in the original array without changing the original array. The function should fill the new array with the rolling sums of the sums of elements in each row of the original array and then return the address of the new array. In other words, the ith element of the new array should contain the sum of the elements in the first i rows of the original array.

The specification of the Function

double *gordulo(double *, int, int);

Note

Place the function in a file named gordulo.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.

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. double *gordulo(double *, int, int);
  4. int main()
  5. {
  6.   double t[3][4] = {{1.0,  2.0,  3.0,  4.0},
  7.                     {5.0,  6.0,  7.0,  8.0},
  8.                     {9.0, 10.0, 11.0, 12.0}};
  9.   double *p = gordulo((double *)t, 3, 4);
  10.   int i;
  11.   for (i = 0; i < 3; ++i)
  12.     printf("%.3f\n", p[i]);
  13.   putchar('\n');
  14.   free(p);
  15.   return EXIT_SUCCESS;
  16. }
download as text file

Makefile

  1. SRCS = main.c gordulo.c
  2. OBJS = $(SRCS:%.c=%.o)
  3. TARGETS = main
  4. .PHONY: clean
  5. all: $(TARGETS)
  6. main: $(OBJS)
  7. $(CC) $(OBJS) -o main
  8. %.o: %.c
  9. $(CC) -Wall -c $< -o $@
  10. clean:
  11. rm -rf $(OBJS) *~ $(TARGETS)
download as text file

Output of the Sample Test Program

  1. 10.000
  2. 36.000
  3. 78.000
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