0
#include <stdio.h>
#include <cs50.h>
#include <math.h>

int procedure (int array[] , int length);

int input_length ( long input );

int main (void)
{
    long card = get_long("Number : ");
    int size = input_length(card);
    int digit[size] ;

    for ( int i=0 ; i < size ; i++)
    {
        digit[i] = card % 10 ;
        card/=10 ;
    }

        int sum = procedure(digit , size) ;
    }
int procedure (int array[] , int length)
{
    int totalsum = 0 ;
    for (int i = 1 ; i < length  ; i+=2 )
    {
        array[i] *=2 ;

        array[i] = (array[i] / 10) + (array[i] % 10 ) ;
    }
    for ( int i=0 ; i < length  ; i++)
    {
        totalsum += array[i] ;
    }
    return totalsum ;
}

I have seen the code run in debug50 . It works well until it reaches procedure function . I want to get output sum by giving input digit[] and size . sum gives correct value but after that the value of elements of digit[] also changes as in the procedure function .

PS - I tried to make the code as short as possible . Please help . I am new so I dont know how to ak a perfect question. -THANKS

EDIT -

IF I do this it works all good . But I still want to know why it happened .

int main (void)
{
    long card = get_long("Number : ");
    int size = input_length(card);
    int temp[size] ;
    int digit[size]; 

    for ( int i=0 ; i < size ; i++)
    {
        temp[i] = card % 10 ;
        digit[i] = card % 10 ;
        card/=10 ;
    }
}
0
0

GOT THE ANSWER -

When you pass array by an function it is not the copy of array that is passing through the function indeed it is the original array . So the value of array also changes .

Thanks for not answering.

4
  • "Thanks for not answering" is not a good way to make friends here, especially since there is only a handful of us who tale the time to provide answers. I read this question and remember typing up an answer before moving on to answer another question yesterday, but must've forgotten to submit/post my answer. Either way, we all volunteer our time and knowledge to help answer questions here and tend to be less active on the weekends; you shouldn't expect an immediate answer. Finding the answer on your own, especially to a common issue like this, is a good skill to have. :) Jun 20 at 11:08
  • I am Really sorry about that.
    – MWD
    Jul 5 at 16:28
  • Should I edit it ?
    – MWD
    Jul 5 at 16:50
  • I wouldn't worry about it too much. I know I won't be holding it against you, and I don't see others doing so either. Just remember to mark your answer as accepted (once you can), so that the system doesn't try to keep finding a new answer for it. :) Jul 6 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .