Apologies for posting a badly formed question prior to this attempt.
I'm trying to get a Gauss Seidel method to work in C, to check how much quicker it is than higher level interpreted languages (i.e python), but I'm having some issues with the results that I'm obtaining.
My input matrix is
Symmetric Positive-Definitive
& Diagonally dominant
so I believe it should converge.
The problem attempts to solve "Ax=b" ,
(Where 'A' = 'a[ ][ ]' ,'b' = 'b[ ]', and 'x'= 'x[ ]')
The final array 'check [ ]' is obtained via a dot product between 'a' and 'x' to see if it returns something close to 'b'.
The below code is fully executable.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int i=0,j=0;
int num=0;
double h = 1.0/(3+1);
double h2 = pow(h,2);
double w=1.5, sum=0.0;
long double x[9],y[9], check[9];
long double tol = pow(10, -10);
long double a[9][9] = {{-4, 1, 0, 1, 0, 0, 0, 0, 0} ,
{1, -4, 1, 0, 1, 0, 0, 0, 0} ,
{0, 1, -4, 0, 0, 1, 0, 0, 0} ,
{1, 0, 0, -4, 1, 0, 1, 0, 0} ,
{0, 1, 0, 1, -4, 1, 0, 1, 0} ,
{0, 0, 1, 0, 1, -4, 0, 0, 1} ,
{0, 0, 0, 1, 0, 0, -4, 1, 0} ,
{0, 0, 0, 0, 1, 0, 1, -4, 1} ,
{0, 0, 0, 0, 0, 1, 0, 1, -4}};
long double b[9] = {0.000000,
0.000000,
0.000000,
0.000000,
0.125000,
0.000000,
0.000000,
0.000000,
0.000000 };
for(i=0;i<9;i++){ // initialise the arrays to 0
x[i]=0;
y[i]=2*tol;
}
for(i=0;i<9;i++){ // prints 'a' matrix, to check if its right
for(j=0;j<9;j++){
printf("a[%d][%d] = %LF ",i,j,a[i][j]);
}
printf("\n" );
}
printf("\n\n");
for(i=0;i<9;i++){ // prints b matrix
printf("b[%d] = %LF \n",i,b[i]);
}
do{ // the Gauss seidel Solver
for(i =0;i<9;i++){
for(j=0; j<9; j++){
if(i!=j){
sum += a[i][j]*x[j];
}
x[i] = (w/a[i][i])* (b[i] - sum + a[i][i]*x[i]) + (1-w)*x[i];
}
}
num=num+1;
}while (fabs(y[i]-x[i])>tol);
printf("\n\n\n");
for(i=0;i<9;i++){ // Prints the solution X
printf("x[%d] = %LF \n",i,x[i]);
}
printf("\n\n");
for(i=0;i<9;i++){ // Ititialises matrix
check[i]=0;
}
for (i = 0; i < 9; i++){ // performs a dot product of
// 'a' and 'x', to see if we get
// 'b' as the result
for(j = 0; j< 9; j++){
check[i]+= a[i][j] * x[j];
}
check[i] = check[i]/h2; // the 'b' matrix was multiplied by h2,
// hence to get it back, a division is necessary
printf("check[%d] = %LF\n",i,check[i]);
}
printf("num=%d\n",num );
return 0;
}
The output i.e 'x' that I get is:
x[0] = -0.000000
x[1] = -0.000000
x[2] = -0.000000
x[3] = -0.000000
x[4] = -0.421875
x[5] = -0.791016
x[6] = -1.423828
x[7] = -3.816650
x[8] = -11.702087
and the output for 'Check' I get is:
check[0] = 0.000000
check[1] = -4.500000
check[2] = -5.625000
check[3] = -14.625000
check[4] = -10.968750
check[5] = -42.328125
check[6] = 17.156250
check[7] = 18.421875
check[8] = 212.343750
Ideally, if everything works, check[4] should output 2 (the reason for which is given in a comment in the code when outputting 'check'), and every other element of check should be 0.
Any suggestions?
sum should be reinitialized to 0 inside the for-loop before starting the next row and the equation is incorrect. The equation you are using from the python implementation assumes that a[i][i]*x[i] was added to make the full dot-product, they used numpy to get the dot product instead of looping so they had no opportunity to do i != j. Also I'm not sure the equation in that implementation is the Gauss Seidel method, it looks more like Successive Over Relaxation because of the w and (1 - w). Anyway, here is my modified solution. I check for convergence using the error, |Ax - b| < tol for all entries. The for-loop is split into two as a small optimization. a[i][i] * x[i] is added to sum to get the current value for (Ax)i in the error check.
int converged;
do {
converged = 1;
for (i = 0; i < 9; i++) {
sum = 0;
for (j = 0; j < i; j++) {
sum += a[i][j] * x[j];
}
for (j = i + 1; j < 9; j++) {
sum += a[i][j] * x[j];
}
x[i] += w * ((b[i] - sum) / a[i][i] - x[i]);
if (fabs(sum + a[i][i] * x[i] - b[i]) > tol) {
converged = 0;
}
}
} while (!converged);
which gives the output:
x[0] = -0.007812
x[1] = -0.015625
x[2] = -0.007812
x[3] = -0.015625
x[4] = -0.046875
x[5] = -0.015625
x[6] = -0.007812
x[7] = -0.015625
x[8] = -0.007813
check[0] = 0.000000
check[1] = -0.000000
check[2] = -0.000000
check[3] = -0.000000
check[4] = 2.000000
check[5] = 0.000000
check[6] = -0.000000
check[7] = 0.000000
check[8] = 0.000000
num=31
For the benefit of those following along at home. I suggest reading with the wikipedia article on Gauss-Seigel. I will attempt to explain what the algorithm is doing, and provide C code that implements the algorithm.
The Python example in the wikipedia page uses this simple example for matrix A and B
| 10 -1 2 0 | | 6 |
A = | -1 11 -1 3 | B = | 25 |
| 2 -1 10 -1 | | -11 |
| 0 3 -1 8 | | 8 |
Those matrices represent the following system of equations
10*x1 - x2 + 2*x3 = 6
-x1 + 11*x2 - x3 + 3*x4 = 25
2*x1 - x2 + 10*x3 - x4 = -11
3*x2 - x3 + 8*x4 = 15
The solution that we're trying to find with Gauss-Seigel is
x1=1 x2=2 x3= -1 x4=1
So how does the algorithm work? Well first take a wild a guess at the answer, e.g.
x1=0 x2=0 x3=0 x4=0
Then plug those guesses into the equations and try to improve the guesses. Specifically, plug the values for x2,x3,x4 into the first equation, and then compute a new value for x1.
10*x1 - 0 + 0 = 6 ==> x1 = 6/10 = 0.6
Then plug the new value of x1, and the old values of x3,x4 into the second equation to get an improved guess for x2
-0.6 + 11*x2 - 0 + 0 = 25 ==> 11*x2 = 25.6 ==> x2 = 2.327273
And for x3 and x4
2*0.6 - 2.327273 + 10*x3 - 0 = -11 ==> 10*x3 = -9.872727 ==> x3 = -0.987273
3*2.327273 + 0.987273 + 8*x4 = 15 ==> 8*x4 = 7.030908 ==> x4 = 0.878864
So after one iteration of Gauss-Seigel, the improved guess at the answer is
x1=0.6 x2=2.327273 x3= -0.987273 x4=0.878864
The algorithm continues until either the solution converges or the maximum number of iterations is exceeded.
Here's what the code looks like in C. The counter k limits the number of iterations (just in case the solution doesn't converge). The Gauss-Seidel method is applied by evaluating each of the equations while skipping X[i]. Then the new value for X[i] is computed. The code displays the new values of X[], and the checks if the answer is good enough by evaluating each equation and verifying that the sum is within epsilon of B[i].
#include <stdio.h>
#include <math.h>
#define SIZE 4
double A[SIZE][SIZE] = {
{ 10, -1, 2, 0 },
{ -1, 11, -1, 3 },
{ 2, -1, 10, -1 },
{ 0, 3, -1, 8 }
};
double B[SIZE] = { 6, 25, -11, 15 };
double X[SIZE] = { 0, 0, 0, 0 };
int main( void )
{
int i, j, k, done;
double sum;
done = 0;
for ( k = 0; k < 100 && !done; k++ )
{
// perform the next iteration of Gauss-Seidel
for ( i = 0; i < SIZE; i++ )
{
sum = 0;
for ( j = 0; j < SIZE; j++ )
if ( j != i )
sum += A[i][j] * X[j];
X[i] = (B[i] - sum) / A[i][i];
}
// print the k'th iteration of X[]
printf( "%2d --", k );
for ( i = 0; i < SIZE; i++ )
printf( " %lf", X[i] );
printf( "\n" );
// check for convergence
done = 1;
for ( i = 0; i < SIZE; i++ )
{
sum = 0;
for ( j = 0; j < SIZE; j++ )
sum += A[i][j] * X[j];
if ( fabs( B[i] - sum ) > 1e-6 )
{
done = 0;
break;
}
}
}
}
Related
Given 2 arrays of integers a[] and b[] with the same size of n (1 <= n <= 100) numbered from 1 to n.
(0 <= a[i], b[i] <= 6)
You can swap any a[i] with b[i].
What is the minimum number of swaps needed so that the difference of the sums of array a[] and b[] is minimum ?
Then print out:
The number of swaps
The swapped indexes
The difference of sums of both arrays
Example
n = 6
a[] = { 1, 1, 4, 4, 0, 6 }
b[] = { 6, 3, 1, 1, 6, 1 }
Result
- 2 (The number of swaps)
- 5, 6 (The swapped indexes)
- 0 (The difference of sums of the arrays)
Explanation
If you swap a[5] with b[5] and a[6] with b[6] which requires 2 swaps, arrays a[] and b[] will become:
a[] = {1, 1, 4, 4, 6, 1}
b[] = {6, 3, 1, 1, 0, 6}
Sum of a[] is 1 + 1 + 4 + 4 + 6 + 1 = 17
Sum of b[] is 6 + 3 + 1 + 1 + 0 + 6 = 17
So the difference of the two sums is 0.
Here's an iterative method that saves the differences so far and updates the smallest list of indexes needed to swap to achieve them.
JavaScript code:
function update(obj, d, arr){
if (!obj[d] || obj[d].length > arr.length)
obj[d] = arr;
}
function f(A, B){
let diffs = {0: []};
for (let i=0; i<A.length; i++){
const newDiffs = {};
for (d in diffs){
// Swap
let d1 = Number(d) + B[i] - A[i];
if (diffs.hasOwnProperty(d1) && diffs[d1].length < diffs[d].length + 1)
update(newDiffs, d1, diffs[d1]);
else
update(newDiffs, d1, diffs[d].concat(i+1));
d1 = Number(d) + A[i] - B[i];
if (diffs.hasOwnProperty(d1) && diffs[d1].length < diffs[d].length)
update(newDiffs, d1, diffs[d1]);
else
update(newDiffs, d1, diffs[d]);
}
diffs = newDiffs;
}
console.log(JSON.stringify(diffs) + '\n\n');
let best = Infinity;
let idxs;
for (let d in diffs){
const _d = Math.abs(Number(d));
if (_d < best){
best = _d;
idxs = diffs[d];
}
}
return [best, idxs];
};
var A = [1, 1, 4, 4, 0, 6];
var B = [6, 3, 1, 1, 6, 1];
console.log(JSON.stringify(f(A, B)));
Here's a C++ implementation of mine based on Javascript answer of גלעד ברקן.
Short Explanation:
We maintain a mapping of all differences and their minimum swaps seen so far and try to extend all of the differences seen so far based on new values to get new mapping of such kind. We have 2 choices at each step when considering ith items in A and B, either consider the items as it is or swap the ith items.
Code:
#include <iostream>
#include <climits>
#include <unordered_map>
#include <vector>
using namespace std; // Pardon me for this sin
void update_keeping_existing_minimum(unordered_map<int, vector<int> >& mp, int key, vector<int>& value){
if(mp.find(key) == mp.end() || mp[key].size() > value.size())mp[key] = value;
}
// Prints minimum swaps, indexes of swaps and minimum difference of sums
// Runtime is O(2^size_of_input) = 2^1 + 2^2 .. + 2^n = 2*2^n
// This is a bruteforce implementation.
// We try all possible cases, by expanding our array 1 index at time.
// For each previous difference,
// we use new index value and expand our possible difference outcomes.
// In worst case we may get 2 unique differences never seen before for every index.
void get_minimum_swaps(vector<int>& a, vector<int>& b){
int n = a.size();
unordered_map<int, vector<int> > prv_differences_mp;
prv_differences_mp[0] = {}; // initial state
for(int i = 0 ; i < n ; i++){
unordered_map<int, vector<int> > new_differences_mp;
for (auto& it: prv_differences_mp) {
// possibility 1, we swap and expand previous difference
int d = it.first;
int d1 = d + b[i] - a[i];
if(prv_differences_mp.find(d1) != prv_differences_mp.end() && prv_differences_mp[d1].size() < (prv_differences_mp[d].size() + 1)){
update_keeping_existing_minimum(new_differences_mp, d1, prv_differences_mp[d1]);
} else {
// only place we are modifying the prv map, lets make a copy so that changes don't affect other calculations
vector<int> temp = prv_differences_mp[d];
temp.push_back(i+1);
update_keeping_existing_minimum(new_differences_mp, d1, temp);
}
// possibility 2, we don't swap and expand previous difference
int d2 = d + a[i] - b[i];
if(prv_differences_mp.find(d2) != prv_differences_mp.end() && prv_differences_mp[d2].size() < prv_differences_mp[d].size()){
update_keeping_existing_minimum(new_differences_mp, d2, prv_differences_mp[d2]);
} else {
update_keeping_existing_minimum(new_differences_mp, d2, prv_differences_mp[d]);
}
}
cout<<i<<":index\n";
for(auto& it: prv_differences_mp){
cout<<it.first<<": [ ";
for(auto& item: it.second)cout<<item<<" ";
cout<<"] ; ";
}
cout<<"\n";
prv_differences_mp = new_differences_mp;
}
int best = INT_MAX;
vector<int> min_swap_ans;
for(auto& it: prv_differences_mp){
int _d = it.first >= 0 ? it.first: -it.first;
if(_d < best){
best = _d;
min_swap_ans = it.second;
}
}
cout<<"Number of swaps: "<<min_swap_ans.size()<<"\n";
cout<<"Swapped indexes:\n";
for(auto idx: min_swap_ans)cout<<idx<<" ";
cout<<"\nDifference: "<<best<<"\n";
}
int main(){
vector<int> A{ 1, 1, 4, 4, 0, 6 };
vector<int> B{ 6, 3, 1, 1, 6, 1 };
get_minimum_swaps(A, B);
return 0;
}
I am trying to create a Gaussian filter kernel in C to do some image processing. I am using a 2d float array on the heap, but when I call free() on the rows I keep getting an free(): invalid pointer error. I have printed out memory locations and values of the filter to standard output and everything seems to be what I expect
//kernel->kernel = float **
//kernel->row_len = kernel->col_len = 5
float total_weight = 0.0;
//build the holding col
kernel->kernel = malloc(sizeof(float *) * kernel->col_len);
//get mem for each row and set the values
for (int j = 0; j < kernel->col_len; j++)
{
kernel->kernel[j] = malloc(sizeof(float) * kernel->row_len);
for (int i = 0; i < kernel->row_len; i++)
{
kernel->kernel[j][i] = ken_ComputeGuassianVal(i, j, sigma, size);
total_weight += kernel->kernel[j][i];
}
//print debugging info
printf("Create - %p\n", (kernel->kernel + j));
for (int i = 0; i < kernel->row_len; i++)
{
printf("%d, %d - %f \n", i, j, kernel->kernel[j][i]);
printf("%p\n", (*(kernel->kernel + j) + i));
}
printf("\n");
}
//Normalise the kernel otherwise brightness will be added to the image
for (int j = 0; j < kernel->col_len; j++)
{
for (int i = 0; i < kernel->row_len; i++)
{
kernel->kernel[j][i] /= total_weight;
}
}
for (int j = 0; j < kernel->col_len; j++)
{
printf("Attempting to free memory at location %p\n", (kernel->kernel + j));
free(kernel->kernel + j);
printf("\n");
}
free(kernel->kernel);
Here is the output I am getting to standard output
Create - 0x55aa2a80d4e0
0, 0 - 0.000000
0x55aa2a7ed8d0
1, 0 - 0.000000
0x55aa2a7ed8d4
2, 0 - 0.000001
0x55aa2a7ed8d8
3, 0 - 0.000000
0x55aa2a7ed8dc
4, 0 - 0.000000
0x55aa2a7ed8e0
Create - 0x55aa2a80d4e8
0, 1 - 0.000000
0x55aa2a84c6a0
1, 1 - 0.001083
0x55aa2a84c6a4
2, 1 - 0.034551
0x55aa2a84c6a8
3, 1 - 0.001083
0x55aa2a84c6ac
4, 1 - 0.000000
0x55aa2a84c6b0
Create - 0x55aa2a80d4f0
0, 2 - 0.000001
0x55aa2a7f96a0
1, 2 - 0.034551
0x55aa2a7f96a4
2, 2 - 1.102181
0x55aa2a7f96a8
3, 2 - 0.034551
0x55aa2a7f96ac
4, 2 - 0.000001
0x55aa2a7f96b0
Create - 0x55aa2a80d4f8
0, 3 - 0.000000
0x55aa2a80d510
1, 3 - 0.001083
0x55aa2a80d514
2, 3 - 0.034551
0x55aa2a80d518
3, 3 - 0.001083
0x55aa2a80d51c
4, 3 - 0.000000
0x55aa2a80d520
Create - 0x55aa2a80d500
0, 4 - 0.000000
0x55aa2a7eddf0
1, 4 - 0.000000
0x55aa2a7eddf4
2, 4 - 0.000001
0x55aa2a7eddf8
3, 4 - 0.000000
0x55aa2a7eddfc
4, 4 - 0.000000
0x55aa2a7ede00
Destroy - 0x55aa2a7ed8d0
Attempting to free memory at location 0x55aa2a80d4e0
Destroy - 0x55aa2a84c6a0
Attempting to free memory at location 0x55aa2a80d4e8
free(): invalid pointer
[1] 13936 abort ./dipcw
I have tried both array notation kernel->kernel[j] and (kernel->kernel + j). I am using elementary linux 5.0 and gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
Edit: Changed the stop condition variable in the normalization loop to use the same variables as the other loops. Added free statement for the double pointer at the end
The rule is: there should be a corresponding free() for every malloc()
struct matrix {
unsigned nrow;
unsigned ncol;
float ** ptrs;
} data;
/* allocate */
unsigned row, col;
data.ptrs = malloc (data.nrow * sizeof *data.ptrs); // <<< [A]
for (row=0; row < data.nrow; row++) { // <<< [B]
data.ptrs[row] = malloc (data.ncol * sizeof *data.ptrs[0]); // <<< [C]
}
/* there should be a corresponding free for every malloc
, but in the "inside-out" order :
*/
unsigned row, col;
for (row=0; row < data.nrow; row++) { // <<< [B]
free( data.ptrs[row] ); // <<< [C]
}
free(data.ptrs); // <<< [A]
Note: for simplicity, I swapped rows/columns , and I used matrix.field instead of pointer->field.
Is it possible in C to have a fast for/while loop that loops through the odd numbers and 2? Without using arrays.
So I'd like it to loop through {1, 2, 3, 5, 7, 9, ..}
Of course. Here is a pretty straight forward way.
for(int i=1; i<N; i++) {
if(i>3) i++;
// Code
}
A bit more hackish variant:
for(int i=1; i<N; i+=1+(i>2)) {
// Code
}
But I think in this case that the most readable variant would be something like:
// Code for 1 and 2
// Then code for 3,5,7 ...
for(int i=3; i<N; i+=2) {
// Code
}
Another option
for(int i=1;;++i) // you didn't specify a limit
{
switch(i)
{
default:
if(!(i&1))continue;
case 1:
case 2:
DoSomething(i):
}
}
Another alternative which does use an array but only a small one that is a constant size of two elements no matter how many numbers in the sequence would be:
{
int i;
int iray[] = {1, 2};
int n = 15;
for (i = 1; i < n; i += iray[i > 2]) {
printf (" i = %d \n", i);
// code
}
}
which produces:
i = 1
i = 2
i = 3
i = 5
i = 7
i = 9
i = 11
i = 13
Extending this alternative to other sequences
And this alternative can be extended to other sequences where there is a change of a similar nature. For instance if the desired sequence was
1, 2, 3, 5, 8, 11, ..
Which involves several changes in the sequence. Beginning at 1 an increment of 1 is used followed by a first increment change beginning at 3 where an increment of 2 is used followed by a second change in the sequence beginning at 5 where an increment of 3 is used, you can make the following modification.
{
int i;
int iray[] = {1, 2, 3}; // increment changes
int n = 15;
// calculate the increment based on the current value of i
for (i = 1; i < n; i += iray[(i > 2) + (i > 3)]) {
printf (" i = %d \n", i);
// code
}
return 0;
}
which would produce:
i = 1
i = 2
i = 3
i = 5
i = 8
i = 11
i = 14
#include <stdio.h>
int main()
{
for(unsigned x = 0; x < 10; x++)
printf("%u%s element - %u\n",x + 1, !x ? "st" : x == 1 ? "nd" : x == 2 ? "rd" : "th", !x + x * 2 - (x >= 2));
return 0;
}
no jumps calculating in the !x + x * 2 - (x >= 2) so no pipeline flushes.
This C program will take the value stored in the variable a and print them one by one.
#include <stdio.h>
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0)
return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d, ", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf("\n");
return 0;
}
Output:
2, 0, 4, 8,
When the function foo executes:
1) For the first time: n = 2048, k = 8, j = 204, sum = 8
2) For the second time: n = 204, k = 4, j = 20, sum = 12
3) For the third time: n = 20, k = 0, j = 2, sum = 12
4) For the fourth time: n = 2, k = 2, j = 0, sum = 14
If I replace the line (present in the foo function):
printf ("%d, ", k);
with this:
printf ("%d | %d, ", k, sum);
Output:
2 | 14, 0 | 12, 4 | 12, 8 | 8,
Can someone please explain how this program works:
1) How it's printing value stored in a?
2) And in this order: 2, 0, 4, 8, ?
3) Why is the value of sum is changing when we're printing values of k?
4) What would happen when n become 0?
You are calling the function foo on a.
That is the order since you are printing AFTER you are processing the rest of the number. Try moving the call to printf before the call to foo in foo and see if you get anything different.
sum is changing because you are doing sum = sum + k and passing it to all the future calls.
When n eventually becomes 0 due to repeated divisions, the last call to foo starts returning and following them all the previous calls start returning after printing the digit they had extracted using n % 10
This is part of my assignment. I have to make a calculator using arrays.
this is the addition part of it.
It works fine but the problem is, it drops the first digit. array size is for example 10 . it doesn't show the first digit whenever addition has remainder.
for example i want to add 5 9 7 5 3 1 0 0 0 0 with 5 0 8 6 4 2 0 0 0 0. the true answer is 11061730000 but this program shows 1061730000 (drops first 1).
how do i fix this behavior??
code:
#include <stdio.h>
#define SIZE_MAX 10
#define SIZE_USE SIZE_MAX-1
int main()
{
int i;
int inum_firstPTR[SIZE_MAX] = {5, 9, 7, 5, 3, 1, 0, 0, 0, 0};
int inum_secondPTR[SIZE_MAX] = {5, 0, 8, 6, 4, 2, 0, 0, 0, 0};
int add_resultPTR[SIZE_MAX] = {0}; //initializing result array
for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
}
puts("");
for (i = 0; i < SIZE_MAX; i++)
{
printf("%d", add_resultPTR[i]);
}
puts("");
return 0;
}
for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else //carry
{
if ( i > 0) // normal carry to next element
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
else // element [0], no more elements to carry to
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
}
}
addition of 5+5+carry1 is 11 but add_resultPTR[0] can only store 1 and there is no position before [0] to store carry1 therefore it only prints from 1.
moreover, 11061730000 are 11 digits how do you suppose to fit into an array of SIZE_MAX 10.
define SIZE_MAX as 11 and then to prevent from overflow store addition of inum_firstPTR[SIZE_MAX] and inum_firstPTR[SIZE_MAX] from add_resultPTR[0] to add_resultPTR[10] and print it in reverse i.e. if add_resultPTR[10] is 1 then from [10] to [0] otherwise from [9] to [0].