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

Problem 13 solution More...

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

Macros

#define N   10
 
#define N2   (N + 10)
 

Functions

int get_number (FILE *fp, char *buffer, uint8_t *out_int)
 
int add_numbers (uint8_t *a, uint8_t *b, uint8_t N)
 
int print_number (uint8_t *number, uint8_t N, int8_t num_digits_to_print)
 
int main (void)
 

Detailed Description

Problem 13 solution

Author
Krishna Vedala

Macro Definition Documentation

◆ N

#define N   10

number of digits of the large number

◆ N2

#define N2   (N + 10)

number of digits in output number

Function Documentation

◆ add_numbers()

int add_numbers ( uint8_t *  a,
uint8_t *  b,
uint8_t  N 
)

Function to add arbitraty length decimal integers stored in an array. a + b = c = new b

45 {
46  int carry = 0;
47  uint8_t *c = b; /* accumulate the result in the array 'b' */
48 
49  for (int i = 0; i < N; i++)
50  {
51  // printf("\t%d + %d + %d ", a[i], b[i], carry);
52  c[i] = carry + a[i] + b[i];
53  if (c[i] > 9) /* check for carry */
54  {
55  carry = 1;
56  c[i] -= 10;
57  }
58  else
59  carry = 0;
60  // printf("= %d, %d\n", carry, c[i]);
61  }
62 
63  for (int i = N; i < N + 10; i++)
64  {
65  if (carry == 0)
66  break;
67  // printf("\t0 + %d + %d ", b[i], carry);
68  c[i] = carry + c[i];
69  if (c[i] > 9)
70  {
71  carry = 1;
72  c[i] -= 10;
73  }
74  else
75  carry = 0;
76  // printf("= %d, %d\n", carry, c[i]);
77  }
78  return 0;
79 }

◆ get_number()

int get_number ( FILE *  fp,
char *  buffer,
uint8_t *  out_int 
)

Function to read the number from a file and store it in array.
index 0 of output buffer => units place
index 1 of output buffer => tens place and so on i.e., index i => 10^i th place

17 {
18  long l = fscanf(fp, "%s\n", buffer);
19  if (!l)
20  {
21  perror("Error reading line.");
22  return -1;
23  }
24  // printf("Number: %s\t length: %ld, %ld\n", buffer, strlen(buffer), l);
25 
26  long L = strlen(buffer);
27 
28  for (int i = 0; i < L; i++)
29  if (buffer[i] < 0x30 || buffer[i] > 0x39)
30  {
31  perror("found inavlid character in the number!");
32  return -1;
33  }
34  else
35  out_int[L - i - 1] = buffer[i] - 0x30;
36 
37  return 0;
38 }

◆ main()

int main ( void  )

Main function

115 {
116  // const char N = 50, N2 = N+10; /* length of numbers */
117  char txt_buffer[N + 5]; /* temporary buffer */
118  uint8_t number[N]; /* array to store digits of a large number */
119  uint8_t sum[N2]; /* array to store the sum of the large numbers. For
120  safety, we make it twice the length of a number. */
121 
122  memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */
123 
124  FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
125  if (!fp)
126  {
127  perror("Unable to open file 'num.txt'.");
128  return -1;
129  }
130 
131  int count = 0;
132  get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */
133  do
134  {
135  count++;
136  if (get_number(fp, txt_buffer, number) != 0)
137  break;
138  add_numbers(number, sum, N);
139  } while (!feof(fp));
140 
141  printf("\nSum : ");
142  print_number(sum, N2, -1);
143 
144  printf("first 10 digits: \t");
145  print_number(sum, N2, 10);
146 
147  fclose(fp); /* close file */
148  return 0;
149 }
Here is the call graph for this function:

◆ print_number()

int print_number ( uint8_t *  number,
uint8_t  N,
int8_t  num_digits_to_print 
)

Function to print a long number

83 {
84  uint8_t start_pos = N - 1;
85  uint8_t end_pos;
86 
87  /* skip all initial zeros */
88  while (number[start_pos] == 0) start_pos--;
89 
90  /* if end_pos < 0, print all digits */
91  if (num_digits_to_print < 0)
92  end_pos = 0;
93  else if (num_digits_to_print <= start_pos)
94  end_pos = start_pos - num_digits_to_print + 1;
95  else
96  {
97  fprintf(stderr, "invalid number of digits argumet!\n");
98  return -1;
99  }
100 
101  for (int i = start_pos; i >= end_pos; i--) putchar(number[i] + 0x30);
102 
103  putchar('\n');
104 
105  return 0;
106 }
L
Definition: list.h:8
N2
#define N2
Definition: sol1.c:111
N
#define N
Definition: sol1.c:109
add_numbers
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
Definition: sol1.c:44
print_number
int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
Definition: sol1.c:82
get_number
int get_number(FILE *fp, char *buffer, uint8_t *out_int)
Definition: sol1.c:16