-1

im learning C and i am a bit confused with array handling.

I have a task that asks me to sum up 2 arrays of non negative integers.

example array 1 = {1,2,3} , array 2 = {4,5,6} -> 123+456 = 579

I searched a bit for a solution on how to convert those arrays of integers to an integer, but didnt really get helpful information.

I ended up with a code:

#include <stdio.h>


int sum(int A[],int B[], int n){
    int i,j,t,k;
    
    for(i=0;i<n;i++){
        t= t+A[i];
    }
    
    for(j=0;j<n;j++){
        k= k+B[j];
    }
    
    return t+k;
    
}


int main()
{
    int n = 3;
    int a[n] = {1,2,3};
    int b[n] = {4,5,6};
    
    printf("%d",sum(a,b,n));

    
    return 0;
} 

But my result is 1225283 which of course is wrong. I found a solution where people write something like "t= 10* t+A[i]" , i dont get where that "10* " comes from, but i tested it and then "t" gets "123" but if i try the same for "k" it doesnt work, returning "k" doesnt give me "456". I am a bit confused, whats the proper way of handling this problem? Thanks for any help.

Aru
  • 352
  • 1
  • 11
  • 3
    `t` Hi I'm an uninitialized variable. Set me to zero please. Like the friendly compiler said in the warning it gave. Voting to close as simple typo. – Lundin Mar 24 '21 at 11:52
  • You don't need to use a seperate variable in the `for` loop. You can reuse `i`. same for `k`. And as @Lundin said, initialize `t`. – Devolus Mar 24 '21 at 11:54
  • i didnt get any errors - Errors: 0 - Warnings: 0 , using DevC++ – Aru Mar 24 '21 at 11:56
  • Ok so in all fairness, gcc is crap for this purpose. You should compile with `-Wall -Wextra -Werror -pedantic-errors`. Might want to get rid of DevC++ since it isn't maintained since forever. Codeblocks is nice and free and uses a much newer version of gcc by default. – Lundin Mar 24 '21 at 11:58
  • 1
    Interesting, gcc does not even compile the code with the error message: `error: variable-sized object may not be initialized` and it is right about it. – mch Mar 24 '21 at 11:58
  • But `int a[n] = {1,2,3};` will not compile in any compiler unless this is some weird gcc extension I haven't heard of. – Lundin Mar 24 '21 at 11:59
  • thanks that helped, but what about the "10*" ? what does it do, why do i need it? does it work without it? – Aru Mar 24 '21 at 11:59
  • For the GCC error: [“Variable-sized object may not be initialized”](https://stackoverflow.com/q/3082914) – Quasímodo Mar 24 '21 at 12:00
  • Multiply by 10 is for creating a number like `123` rather than 1+2+3=6. Any number can be formed by val * base^n + val * base^n-1 ... In case of decimal numbers (base 10), that means 1*10^2 + 2*10^1 + 3*10^0 = 123. This applies to binary base 2, hex base 16 etc as well. – Lundin Mar 24 '21 at 12:00
  • as i said i got no errors, no warnings nothing, and why should it not compile? whats the better way to define the array of integers ? what instead of int a[n] = {1,2,3}; ? – Aru Mar 24 '21 at 12:01
  • Why not just initialize your arrays as `int a[] = {1,2,3};` and eliminate the VLA concern? – David C. Rankin Mar 24 '21 at 12:03
  • What is the expected result? `123+456` = `579` or `1+2+3+4+5+6` = `21`? – mch Mar 24 '21 at 12:05
  • im thankful for all this help, but people please read the question carefully...... – Aru Mar 24 '21 at 12:06
  • The reason it should not compile is that it's not valid C. Or to be more correct, the C standard does not require this to compile, so if this compiles, it's because of compiler extensions. And neither gcc nor clang supports it. – klutt Mar 24 '21 at 12:46
  • However, it IS valid C++ – klutt Mar 24 '21 at 15:05

2 Answers2

2

You're basically adding digits 1+2+3 instead of creating the number 123. Your code also has various other flaws, like uninitialized variables. Here is a working example:

int array2int(int A[], int n) {
    int ret = 0;

    for(int i=0, k=1; i<n; i++){
        ret = ret + k * A[i];
        k *= 10;
    }

    return ret;
}    

int sum(int A[],int B[], int n){
    return array2int(A, n) + array2int(B, n);    
}
klutt
  • 30,332
  • 17
  • 55
  • 95
1

First of all, in sum function, you haven't initialized neighter t nor k but you keep summing them and use later, so every time your code is executed, you chould get different result.

On the other hand, in something like "t= 10 t+A[i]", 10 comes from basic math, where a number could be resolved as a10^0 + b10^1 +c*10^2 + .... + m * 10^n. As a result, starting from least significant digit, everytime you try to add new digit (from least to most significant), you need your multipliciant to be 10 times greater.

int sum(int A[],int B[], int n){
    int i,j,t=0,k=0,ten=1;
    
    for(i=n-1;i>=0;i--){
        t += ten*A[i];
        ten *= 10;
    }

    ten = 1; /* initialize again*/
    
    for(j=n-1;j>=0;j--){
        k += ten*B[j];
        ten *= 10;
    }
    
    return t+k;
    
}

Something like that should work.

  • With that code i get "246" as output, which is wrong. – Aru Mar 24 '21 at 12:10
  • yeah maybe both of us should read code first before copy pasting. I did the same operation on array A twice, instead of doing for both A and B. Could you try again? – Deniz Polat Mar 24 '21 at 12:18
  • *"so every time your code is executed, you should get different result."* - Not necessarily. It's possible, but not required at all. – klutt Mar 24 '21 at 12:52
  • yeap, you right. i was aiming to write *could*, my bad – Deniz Polat Mar 24 '21 at 12:55