Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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:

Functions

int get_number (FILE *fp, char *buffer, uint8_t *out_int)
 Function to read the number from a file and store it in array.
 
int add_numbers (uint8_t *a, uint8_t *b, uint8_t N)
 Function to add arbitrary length decimal integers stored in an array.
 
int print_number (uint8_t *number, uint8_t N, int8_t num_digits_to_print)
 Function to print a long number.
 
int main (void)
 Main function.
 

Detailed Description

Problem 13 solution

Author
Krishna Vedala

Function Documentation

◆ add_numbers()

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

Function to add arbitrary length decimal integers stored in an array.

a + b = c = new b

49{
50 int carry = 0;
51 uint8_t *c = b; /* accumulate the result in the array 'b' */
52
53 for (int i = 0; i < N; i++)
54 {
55 // printf("\t%d + %d + %d ", a[i], b[i], carry);
56 c[i] = carry + a[i] + b[i]; // NOLINT // This is a known false-positive
57 if (c[i] > 9) /* check for carry */
58 {
59 carry = 1;
60 c[i] -= 10;
61 }
62 else
63 {
64 carry = 0;
65 }
66 // printf("= %d, %d\n", carry, c[i]);
67 }
68
69 for (int i = N; i < N + 10; i++)
70 {
71 if (carry == 0)
72 {
73 break;
74 }
75 // printf("\t0 + %d + %d ", b[i], carry);
76 c[i] = carry + c[i];
77 if (c[i] > 9)
78 {
79 carry = 1;
80 c[i] -= 10;
81 }
82 else
83 {
84 carry = 0;
85 }
86 // printf("= %d, %d\n", carry, c[i]);
87 }
88 return 0;
89}

◆ 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 {
30 if (buffer[i] < 0x30 || buffer[i] > 0x39)
31 {
32 perror("found inavlid character in the number!");
33 return -1;
34 }
35 else
36 {
37 out_int[L - i - 1] = buffer[i] - 0x30;
38 }
39 }
40
41 return 0;
42}
Definition list.h:8
struct used to store character in certain times
Definition min_printf.h:35

◆ main()

int main ( void  )

Main function.

124{
125 /* number of digits of the large number */
126 const int N = 10;
127 /* number of digits in output number */
128 const int N2 = N + 10;
129
130 // const char N = 50, N2 = N+10; /* length of numbers */
131 char *txt_buffer =
132 (char *)calloc(N + 5, sizeof(char)); /* temporary buffer */
133 uint8_t *number = (uint8_t *)calloc(
134 N, sizeof(uint8_t)); /* array to store digits of a large number */
135 uint8_t *sum = (uint8_t *)calloc(
136 N2, sizeof(uint8_t)); /* array to store the sum of the large
137numbers. For safety, we make it twice the length of a number. */
138
139 FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
140 if (!fp)
141 {
142 perror("Unable to open file 'num.txt'.");
143 free(txt_buffer);
144 free(sum);
145 free(number);
146 return -1;
147 }
148
149 int count = 0;
150 get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */
151 do
152 {
153 count++;
154 if (get_number(fp, txt_buffer, number) != 0)
155 {
156 break;
157 }
158 add_numbers(number, sum, N);
159 } while (!feof(fp));
160
161 printf("\nSum : ");
162 print_number(sum, N2, -1);
163
164 printf("first 10 digits: \t");
165 print_number(sum, N2, 10);
166
167 fclose(fp); /* close file */
168 free(txt_buffer);
169 free(sum);
170 free(number);
171 return 0;
172}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22
int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
Function to print a long number.
Definition sol1.c:92
int get_number(FILE *fp, char *buffer, uint8_t *out_int)
Function to read the number from a file and store it in array.
Definition sol1.c:16
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
Function to add arbitrary length decimal integers stored in an array.
Definition sol1.c:48
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.

93{
94 uint8_t start_pos = N - 1;
95 uint8_t end_pos;
96
97 /* skip all initial zeros */
98 while (number[start_pos] == 0) start_pos--;
99
100 /* if end_pos < 0, print all digits */
101 if (num_digits_to_print < 0)
102 {
103 end_pos = 0;
104 }
105 else if (num_digits_to_print <= start_pos)
106 {
107 end_pos = start_pos - num_digits_to_print + 1;
108 }
109 else
110 {
111 fprintf(stderr, "invalid number of digits argumet!\n");
112 return -1;
113 }
114
115 for (int i = start_pos; i >= end_pos; i--) putchar(number[i] + 0x30);
116
117 putchar('\n');
118
119 return 0;
120}