Napisz program, który będzie liczył sumę ciągu arytmetycznego i geometrycznego. Obliczanie sum zaimplementuj w osobnych funkcjach, które będziesz wywoływał za pośrednictwem wskaźników.
Czy mój program działa zgodnie z poleceniem? Nie rozumiem do końca zagadnienia z tematu wątku.
#include <stdio.h>
#include <stdlib.h>
void enter(int *a, int *r, int *an){
printf("Podaj pierwszy element ciągu: ");
scanf("%d", a);
printf("Podaj różnice/iloraz: ");
scanf("%d", r);
printf("Podaj liczbe elementów: ");
scanf("%d", an);
}
int sumaatrytmetycznego(int a, int r, int an){
int suma = 0;
for(int i = 0; i < an; i++){
suma += a;
a += r;
}
return suma;
}
int sumageometrycznego(int a, int r, int an){
int suma = 0;
for(int i = 0; i < an; i++){
suma += a;
a *=r;
}
return suma;
}
int main() {
int a;
int r;
int an;
int nr;
printf("[1] suma ciągu gemetrycznego\n"
"[2] suma ciagu arytmetycznego\n"
"Podaj numer operacji: ");
scanf("%d", &nr);
if(nr == 1){
enter(&a, &r, &an);
int (*ptr_sumageom)(int, int, int) = sumageometrycznego;
printf("%d\n", ptr_sumageom(a, r, an));
}
else if(nr == 2){
enter(&a, &r, &an);
int (*ptr_sumaryt)(int, int, int) = sumaatrytmetycznego;
printf("%d\n", ptr_sumaryt(a, r, an));
}
return 0;
}