Problem 13 solution
More...
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
int | get_number (FILE *fp, char *buffer, uint8_t *out_int) |
| Function to read the number from a file and store it in array. More...
|
|
int | add_numbers (uint8_t *a, uint8_t *b, uint8_t N) |
| Function to add arbitrary length decimal integers stored in an array. More...
|
|
int | print_number (uint8_t *number, uint8_t N, int8_t num_digits_to_print) |
| Function to print a long number. More...
|
|
int | main (void) |
| Main function. More...
|
|
◆ 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
53 for (
int i = 0; i < N; i++)
56 c[i] = carry + a[i] + b[i];
69 for (
int i = N; i < N + 10; i++)
◆ 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
18 long l = fscanf(fp,
"%s\n",
buffer);
21 perror(
"Error reading line.");
28 for (
int i = 0; i <
L; i++)
32 perror(
"found inavlid character in the number!");
37 out_int[
L - i - 1] =
buffer[i] - 0x30;
struct used to store character in certain times
Definition: min_printf.h:31
◆ main()
Main function.
128 const int N2 = N + 10;
132 (
char *)
calloc(N + 5,
sizeof(
char));
133 uint8_t *number = (uint8_t *)
calloc(
135 uint8_t *sum = (uint8_t *)
calloc(
136 N2,
sizeof(uint8_t));
139 FILE *fp = fopen(
"num.txt",
"rt");
142 perror(
"Unable to open file 'num.txt'.");
164 printf(
"first 10 digits: \t");
int count(int *arr, const int size)
Count func counts the number of prime numbers.
Definition: prime_seive.c:42
#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
◆ print_number()
int print_number |
( |
uint8_t * |
number, |
|
|
uint8_t |
N, |
|
|
int8_t |
num_digits_to_print |
|
) |
| |
Function to print a long number.
94 uint8_t start_pos = N - 1;
98 while (number[start_pos] == 0) start_pos--;
101 if (num_digits_to_print < 0)
105 else if (num_digits_to_print <= start_pos)
107 end_pos = start_pos - num_digits_to_print + 1;
111 fprintf(stderr,
"invalid number of digits argumet!\n");
115 for (
int i = start_pos; i >= end_pos; i--) putchar(number[i] + 0x30);