Skip to content

Commit fc77ca4

Browse files
committed
Finished with geometric progression
1 parent 447654a commit fc77ca4

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

For loop/52. Sum of GP series.c

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ The Sum of the G.P. series : 93.000000
1212
1313
+ 1. Write tests (run tests function + body of a test)
1414
+ 2. Input
15-
3. Invalid input
16-
4. Finction to print out and find the sum
17-
5. Check the tests
18-
6. Cppcheck
19-
7. Add and push
15+
+ 3. Invalid input
16+
+ 4. Finction to print out and find the sum
17+
+ 5. Check the tests
18+
+ 6. Cppcheck
19+
+ 7. Add and push
2020
*/
2121

2222
#include <stdio.h>
@@ -33,49 +33,65 @@ int input_first_number(int *first_number);
3333
int input_number_of_terms(int *number_of_terms);
3434
int input_common_ratio(int *common_ratio);
3535
void print_invalid_input();
36+
void print_sum_gp_series(int sum);
3637

3738
int main() {
39+
// Regular run
3840
int first_number = 0;
3941
if (input_first_number(&first_number)) {
4042
int number_of_terms = 0;
4143
if (input_number_of_terms(&number_of_terms)) {
4244
int common_ratio = 0;
4345
if (input_common_ratio(&common_ratio)) {
44-
run_tests();
46+
int sum = find_gp_series(first_number, number_of_terms, common_ratio);
47+
print_sum_gp_series(sum);
4548
} else {
4649
print_invalid_input();
4750
}
4851
} else {
4952
print_invalid_input();
50-
}
53+
}
5154
} else {
5255
print_invalid_input();
5356
}
57+
// Tests to run
58+
printf("\n>>>Tests:\n");
59+
run_tests();
5460
return 0;
5561
}
5662

63+
void print_sum_gp_series(int sum) {
64+
printf("The Sum of the G.P. series: %d\n", sum);
65+
}
66+
5767
int input_common_ratio(int *common_ratio) {
68+
printf("Input the common ratio of G.P. series:\n");
5869
int is_valid = TRUE;
5970
char endline = '\0';
60-
if (!scanf("%d%c", common_ratio, &endline) || endline != '\n') {
71+
// Common ratio can't be zero as it will result in all elements being the same = 1, which is not gp
72+
if (!scanf("%d%c", common_ratio, &endline) || endline != '\n' || *common_ratio == 0) {
6173
is_valid = FALSE;
6274
}
63-
return is_valid;
75+
return is_valid;
6476
}
6577

6678
int input_number_of_terms(int *number_of_terms) {
79+
printf("Input the number or terms in the G.P. series:\n");
6780
int is_valid = TRUE;
6881
char endline = '\0';
69-
if (!scanf("%d%c", number_of_terms, &endline) || endline != '\n') {
82+
// Number of terms can't be negative or zero
83+
if (!scanf("%d%c", number_of_terms, &endline) || endline != '\n' || *number_of_terms <= 0) {
7084
is_valid = FALSE;
7185
}
72-
return is_valid;
86+
return is_valid;
7387
}
7488

75-
int input_first_number(int *first_number) {
89+
int input_first_number(int *first_number) {
90+
printf("Input the first number of the G.P. series:\n");
7691
int is_valid = TRUE;
7792
char endline = '\0';
78-
if (!scanf("%d%c", first_number, &endline) || endline != '\n') {
93+
// First element can't be zero as it will result in progression having only one element - zero
94+
if (!scanf("%d%c", first_number, &endline) || endline != '\n' || first_number == 0) {
7995
is_valid = FALSE;
8096
}
8197
return is_valid;
@@ -86,8 +102,14 @@ void print_invalid_input() {
86102
}
87103

88104
int find_gp_series(int first, int number_of_terms, int common_ratio) {
89-
int sum = 0;
90-
printf("%d%d%d",first, number_of_terms, common_ratio);
105+
printf("The numbers for the G.P. series:\n");
106+
int number_to_print = first, sum = 0;
107+
for (int i = 0; i < number_of_terms; ++i) {
108+
printf("%d ", number_to_print);
109+
sum += number_to_print;
110+
number_to_print *= common_ratio;
111+
}
112+
printf("\nThe Sum of the G.P. series: %d\n", sum);
91113
return sum;
92114
}
93115

@@ -96,12 +118,23 @@ void test(int first_number, int number_of_terms, int common_ratio, int expected_
96118
if (actual_result == expected_result) {
97119
printf("Test #%d: Success\n", test_number);
98120
} else {
99-
printf("Test #%d: Failed\nFirst_number = %d, number_of_terms = %d, common_ratio = %d, expected_result = %d, actual_result = %d\n",
100-
test_number, first_number, number_of_terms, common_ratio, expected_result, actual_result);
121+
printf("Test #%d: Failed\n", test_number);
122+
printf("First = %d, n_of_terms = %d, common_ratio = %d, expected = %d, actual = %d\n",
123+
first_number, number_of_terms, common_ratio, expected_result, actual_result);
101124
}
125+
printf("\n");
102126
}
103127

104128
void run_tests() {
105129
// Normal value tests
106130
test(3, 5, 2, 93, 1);
107-
}
131+
test(1, 10, 3, 29524, 2);
132+
// Negative start test
133+
test(-3, 5, 2, -93, 3);
134+
test(-1, 10, 3, -29524, 4);
135+
// Negative common ration tests
136+
test(10, 4, -2, -50, 5);
137+
test(51, 5, -5, 26571, 6);
138+
// Near-end of int range test
139+
test(1011, 21, 2, 2120219661, 7);
140+
}

0 commit comments

Comments
 (0)