Problem 8 solution
More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
int | main (int argc, char *argv[]) |
|
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Main function
13 int position = 0, num_bad_chars = 0;
16 unsigned char num, num_prev;
17 unsigned char *buffer = NULL;
18 long long int prod = 1, max_prod = 0;
25 num_digits = atoi(argv[1]);
28 buffer = calloc(num_digits,
sizeof(
unsigned char));
31 perror(
"Unable to allocate memory for buffer");
36 FILE *fp = fopen(
"digits.txt",
"rt");
39 perror(
"Unable to open file");
53 if (ch < 0x30 || ch > 0x39)
59 else if (num_bad_chars > 0)
68 memmove(buffer, buffer + 1, num_digits - 1);
74 buffer[num_digits - 1] = num;
88 for (
int i = 0; i < num_digits; i++)
103 position = ftell(fp) - num_bad_chars - num_digits - 1;
107 printf(
"Maximum product: %lld\t Location: %d^th position\n\t", max_prod,
112 for (; num_digits > 0; num_digits--)
116 if (ch < 0x30 || ch > 0x39)
121 printf(
"%c = %lld\n", ch, max_prod);