@@ -12,11 +12,11 @@ The Sum of the G.P. series : 93.000000
12
12
13
13
+ 1. Write tests (run tests function + body of a test)
14
14
+ 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
20
20
*/
21
21
22
22
#include <stdio.h>
@@ -33,49 +33,65 @@ int input_first_number(int *first_number);
33
33
int input_number_of_terms (int * number_of_terms );
34
34
int input_common_ratio (int * common_ratio );
35
35
void print_invalid_input ();
36
+ void print_sum_gp_series (int sum );
36
37
37
38
int main () {
39
+ // Regular run
38
40
int first_number = 0 ;
39
41
if (input_first_number (& first_number )) {
40
42
int number_of_terms = 0 ;
41
43
if (input_number_of_terms (& number_of_terms )) {
42
44
int common_ratio = 0 ;
43
45
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 );
45
48
} else {
46
49
print_invalid_input ();
47
50
}
48
51
} else {
49
52
print_invalid_input ();
50
- }
53
+ }
51
54
} else {
52
55
print_invalid_input ();
53
56
}
57
+ // Tests to run
58
+ printf ("\n>>>Tests:\n" );
59
+ run_tests ();
54
60
return 0 ;
55
61
}
56
62
63
+ void print_sum_gp_series (int sum ) {
64
+ printf ("The Sum of the G.P. series: %d\n" , sum );
65
+ }
66
+
57
67
int input_common_ratio (int * common_ratio ) {
68
+ printf ("Input the common ratio of G.P. series:\n" );
58
69
int is_valid = TRUE;
59
70
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 ) {
61
73
is_valid = FALSE;
62
74
}
63
- return is_valid ;
75
+ return is_valid ;
64
76
}
65
77
66
78
int input_number_of_terms (int * number_of_terms ) {
79
+ printf ("Input the number or terms in the G.P. series:\n" );
67
80
int is_valid = TRUE;
68
81
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 ) {
70
84
is_valid = FALSE;
71
85
}
72
- return is_valid ;
86
+ return is_valid ;
73
87
}
74
88
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" );
76
91
int is_valid = TRUE;
77
92
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 ) {
79
95
is_valid = FALSE;
80
96
}
81
97
return is_valid ;
@@ -86,8 +102,14 @@ void print_invalid_input() {
86
102
}
87
103
88
104
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 );
91
113
return sum ;
92
114
}
93
115
@@ -96,12 +118,23 @@ void test(int first_number, int number_of_terms, int common_ratio, int expected_
96
118
if (actual_result == expected_result ) {
97
119
printf ("Test #%d: Success\n" , test_number );
98
120
} 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 );
101
124
}
125
+ printf ("\n" );
102
126
}
103
127
104
128
void run_tests () {
105
129
// Normal value tests
106
130
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