|
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
#include <stdio.h> #include <stdlib.h>
double *gordulo(double *, int, int);
int main() { double t[3][4] = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0}, {9.0, 10.0, 11.0, 12.0}}; double *p = gordulo((double *)t, 3, 4); int i; for (i = 0; i < 3; ++i) printf("%.3f\n", p[i]); putchar('\n'); free(p); return EXIT_SUCCESS; }
download as text file
Makefile
SRCS = main.c gordulo.c OBJS = $(SRCS:%.c=%.o) TARGETS = main
.PHONY: clean
all: $(TARGETS)
main: $(OBJS) $(CC) $(OBJS) -o main
%.o: %.c $(CC) -Wall -c $< -o $@
clean: rm -rf $(OBJS) *~ $(TARGETS)
download as text file
Output of the Sample Test Program
10.000 36.000 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.
|
|