Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 8 solution More...

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

Functions

long long int get_product (FILE *fp, long start_pos, int num_digits)
 Compute the product of two numbers in a file. More...
 
int main (int argc, char *argv[])
 Main function. More...
 

Detailed Description

Problem 8 solution

Author
Krishna Vedala

Function Documentation

◆ get_product()

long long int get_product ( FILE *  fp,
long  start_pos,
int  num_digits 
)

Compute the product of two numbers in a file.

Parameters
[in]fppointer to file that is already open
[in]start_posline number of the first numer
[in]num_digitsnumber of digits on the line to multiply
Returns
expected product
17{
18 char ch = ' '; /* temporary variable to store character read from file */
19 unsigned char num = 0; /* temporary variable to store digit read */
20 long long int prod = 1; /* product accumulator */
21 int count =
22 0; /* we use this variable to count number of bytes of file read */
23
24 /* accumulate product for num_digits */
25 for (int i = 0; i < num_digits; i++, count++)
26 {
27 /* get character from file */
28 ch = getc(fp);
29
30 /* the ASCII codes of digits is between 0x30 and 0x39.
31 * any character not in this range implies an invalid character
32 */
33 if (ch < 0x30 || ch > 0x39)
34 {
35 if (ch == EOF)
36 return 0;
37 i--;
38 continue;
39 }
40
41 num = ch - 0x30; /* convert character digit to number */
42 if (num == 0)
43 {
44 /* If number is zero, we can skip the next 'num_digits'
45 * because this '0' will repeat in the next 'num_digit'
46 * multiplications. Hence, we also do not update the file position
47 */
48 /* NOTE: this is not needed but helps get results faster :) */
49 return 0;
50 }
51
52 prod *= num; /* accumulate product */
53 }
54
55 /* set file position to the next starting character + 1 */
56 fseek(fp, -count + 1, SEEK_CUR);
57
58 return prod;
59}

◆ main()

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

Main function.

63{
64 int position = 0;
65 int num_digits = 4;
66 long long int prod, max_prod = 0;
67
68 /* if second command-line argument is ge=iven,
69 * use it as the number of digits to compute
70 * successive product for
71 */
72 if (argc == 2)
73 num_digits = atoi(argv[1]);
74
75 /* open file to read digits from */
76 FILE *fp = fopen("digits.txt", "rt");
77 if (!fp)
78 {
79 perror("Unable to open file");
80 return -1;
81 }
82
83 /* loop through all digits in the file */
84 do
85 {
86 /* get product of 'num_digits' from current position in file */
87 prod = get_product(fp, ftell(fp), num_digits);
88
89 if (prod > max_prod)
90 {
91 max_prod = prod;
92 position = ftell(fp) - 1;
93 }
94 } while (!feof(fp)); /* loop till end of file is reached */
95
96 printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod,
97 position);
98 fseek(fp, position,
99 SEEK_SET); /* move cursor to identified position in file */
100 /* loop through all digits */
101 for (; num_digits > 0; num_digits--)
102 {
103 char ch = getc(fp); /* get character */
104 /* skip invalid character */
105 if (ch < 0x30 || ch > 0x39)
106 continue;
107 if (num_digits > 1)
108 printf("%c x ", ch);
109 else
110 printf("%c = %lld\n", ch, max_prod);
111 }
112
113 fclose(fp); /* close file */
114
115 return 0;
116}
long long int get_product(FILE *fp, long start_pos, int num_digits)
Compute the product of two numbers in a file.
Definition: sol1.c:16
Here is the call graph for this function: