Skip to content

Commit e95b249

Browse files
committed
for loop (how often do you want your print and computations?)
1 parent 9ef0acd commit e95b249

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

01_SimpleCApp/SimpleCApp.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
#include <stdint.h>
33

44
int32_t f(int32_t x);
5-
int scan_x(int32_t* x);
6-
void execute_print(int32_t x);
5+
int scan_int(int32_t* x);
6+
void execute_print(int32_t x, int32_t count);
77
void print_fx(int32_t x, int32_t y);
88

99
int main()
1010
{
11-
int32_t x;
12-
int result = scan_x(&x);
13-
if (result == 1)
11+
int32_t x, count;
12+
int r0 = scan_int(&x);
13+
int r1 = scan_int(&count);
14+
if (r0 == 1 && r1 == 1 && count > 0)
1415
{
15-
execute_print(x);
16+
execute_print(x, count);
1617
}
17-
else if (result == EOF)
18+
else if (r0 == EOF || r1 == EOF)
1819
{
1920
printf("IO Error!");
2021
}
22+
else if (r1 == 1 && count <= 0)
23+
{
24+
printf("The count (second value) needs to be bigger than 0!");
25+
}
2126
else
2227
{
23-
printf("The input must be a number!");
28+
printf("The input(s) must be a number!");
2429
}
2530
}
2631

@@ -29,23 +34,19 @@ int32_t f(int32_t x)
2934
return x * x + 5 * x + 5;
3035
}
3136

32-
int scan_x(int32_t* x)
37+
int scan_int(int32_t* x)
3338
{
34-
printf("Enter the value x: ");
39+
printf("Enter an integer value: ");
3540
return scanf_s("%i", x);
3641
}
3742

38-
void execute_print(int32_t x)
43+
void execute_print(int32_t x, int32_t count)
3944
{
40-
int32_t y1 = f(1 * x);
41-
int32_t y2 = f(2 * x);
42-
int32_t y3 = f(3 * x);
43-
int32_t y4 = f(4 * x);
44-
45-
print_fx(1 * x, y1);
46-
print_fx(2 * x, y2);
47-
print_fx(3 * x, y3);
48-
print_fx(4 * x, y4);
45+
for (int32_t i = 1; i <= count; i++)
46+
{
47+
int32_t y = f(i * x);
48+
print_fx(i * x, y);
49+
}
4950
}
5051

5152
void print_fx(int32_t x, int32_t y)

0 commit comments

Comments
 (0)