Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol2.c File Reference

Problem 8 solution More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for sol2.c:

Functions

int main (int argc, char *argv[])
 

Detailed Description

Problem 8 solution

Author
Krishna Vedala

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main function

12 {
13  int position = 0, num_bad_chars = 0;
14  int num_digits = 4;
15  char ch;
16  unsigned char num, num_prev;
17  unsigned char *buffer = NULL;
18  long long int prod = 1, max_prod = 0;
19 
20  /* if second command-line argument is given,
21  * use it as the number of digits to compute
22  * successive product for
23  */
24  if (argc == 2)
25  num_digits = atoi(argv[1]);
26 
27  /* allocate memory to store past values */
28  buffer = calloc(num_digits, sizeof(unsigned char));
29  if (!buffer)
30  {
31  perror("Unable to allocate memory for buffer");
32  return -1;
33  }
34 
35  /* open file to read digits from */
36  FILE *fp = fopen("digits.txt", "rt");
37  if (!fp)
38  {
39  perror("Unable to open file");
40  free(buffer); /* free allocated memory */
41  return -1;
42  }
43 
44  /* loop through all digits in the file */
45  do
46  {
47  /* get character from file */
48  ch = getc(fp);
49 
50  /* the ASCII codes of digits is between 0x30 and 0x39.
51  * any character not in this range implies an invalid character
52  */
53  if (ch < 0x30 || ch > 0x39)
54  {
55  num_bad_chars++; /* this is used to get the bad characters in the
56  sequence of 13 characters */
57  continue;
58  }
59  else if (num_bad_chars > 0)
60  num_bad_chars--;
61 
62  num = ch - 0x30; /* convert character digit to number */
63  num_prev = buffer[0]; /* previous n^th digit */
64 
65  /* left shift the buffer -
66  * using a for loop or a faster memory move
67  */
68  memmove(buffer, buffer + 1, num_digits - 1);
69  /*
70  for (int i = 1; i < num_digits; i++)
71  buffer[i-1] = buffer[i];
72  */
73 
74  buffer[num_digits - 1] = num; /* save the latest number in buffer */
75 
76  if (num_prev != 0)
77  {
78  /* since product is accumulated, the new product can be obtained by
79  * simply multiplying the new digit and dividing with the oldest
80  * digit
81  */
82  prod /= num_prev; /* divide first to avoid over-flows */
83  prod *= num;
84  }
85  else
86  {
87  prod = 1;
88  for (int i = 0; i < num_digits; i++)
89  {
90  if (buffer[i] == 0)
91  {
92  prod = 0;
93  break; /* break innermost for-loop */
94  }
95  prod *= buffer[i];
96  }
97  }
98 
99  /* check if a new maxima was found */
100  if (prod > max_prod)
101  {
102  max_prod = prod;
103  position = ftell(fp) - num_bad_chars - num_digits - 1;
104  }
105  } while (!feof(fp)); /* loop till end of file is reached */
106 
107  printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod,
108  position);
109  fseek(fp, position,
110  SEEK_SET); /* move cursor to identified position in file */
111  /* loop through all digits */
112  for (; num_digits > 0; num_digits--)
113  {
114  char ch = getc(fp); /* get character */
115  /* skip invalid character */
116  if (ch < 0x30 || ch > 0x39)
117  continue;
118  if (num_digits > 1)
119  printf("%c x ", ch);
120  else
121  printf("%c = %lld\n", ch, max_prod);
122  }
123 
124  fclose(fp); /* close file */
125  free(buffer); /* free allocated memory */
126 
127  return 0;
128 }