-2

I encountered a problem the code for which is -

int* plusOne(int* A, int n1) 
{
    int i=1;
    while((A[n1-i]+1==10)&&(i<n1))
    {
        A[n1-i]=0;
        i++;
    }    
    A[n1-i]++;
    for(i=0;i<n1;i++)
    {
        printf("%d",A[i]);
    }
}

Only later I read that they want me to give output by using return statements. I searched for the solution and found Returning an array using C on Stack Overflow, but I'm not able to understand it — please help.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Dhruv Joshi
  • 33
  • 1
  • 7
  • I assume you are doing some coding challenges which ask to make a function. In short they are saying that they will handle printing the array, what you do is, make the desired function and return that array from that. In short you don't need that for loop. Just replace that complete loop with `return A;`. Also where ever you declared `A`, either make it `static int A[SIZE]` or allocate memory into it using `malloc()` – Mihir Luthra Sep 28 '19 at 16:30
  • @Mihir : `A[]` may be passed as an argument - the code is incomplete so we cannot tell. That would be a far better solution than a static or `malloc()` within this function, which would be begging for re-entrancy and memory leak issues respectively - it is ill-advised.. – Clifford Sep 28 '19 at 16:42
  • @Clifford, I told "I assume you are doing some coding challenges". In them generally they have that format. Like to complete an empty function. For intance, see [this](https://www.hackerrank.com/challenges/tutorial-intro/problem). Not really fond of these but have seen people do it. – Mihir Luthra Sep 28 '19 at 16:48
  • 1
    @Mihir — when I went to edit the question, I "found" the first line of the function hidden on the same line as the triple back-quote. I think that materially affects the relevance of your comments. – Jonathan Leffler Sep 28 '19 at 17:06
  • @Mihir : Originally the code block did not include the function head, but that was a mark-down composition issue fixed by Jonathan. It is clear now that `int* A` is passed by argument, making your suggestion irrelevant in any case. – Clifford Sep 28 '19 at 17:10
  • @Clifford true. Thanks to Jonathan, wouldn't have realised that otherwise. Now all he needs to do is simply `return A;` instead of that `for` loop(most probably, but can't gaurantee unless he provides more info). – Mihir Luthra Sep 28 '19 at 17:15
  • Was the signature of the function given to you? There is no 'print statement' in C; your code makes a call to the `printf()` function (but the function call is a statement). Can you use `putchar()` instead, or does that count as a 'print statement' too? What exactly is this function supposed to be doing? Why is it defined to return an `int *` yet there is no `return` statement. There are many unresolved problems that need to be resolved before an answer can be given. – Jonathan Leffler Sep 28 '19 at 17:15
  • 1
    @JonathanLeffler : 10 years on from [Jeff Attwood's rant on this subject](https://blog.codinghorror.com/treating-user-myopia/) and still not solved! ;-) – Clifford Sep 28 '19 at 17:19
  • 1
    @JonathanLeffler they mentioned " Do not print the output, instead return values " – Dhruv Joshi Sep 29 '19 at 02:42

1 Answers1

0

Let us analyse your code step by step:

  1. assigning 1 to integer variable 1
  2. loop is there it will be true for 1st time when last element is 9 only and also it should have at least 2 element in array.

    taking example of A={.....,9,9}

    loop will be true second time iff second last element is 9 only. After getting first non 9 number or reaching second element it will be finished.
    A={....b,a+1,0,0}
    a is first non 9 number or it might be first element.

  3. all element will be printed ..whenever there is 9 (in original array before loop) it will print 0.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Aditya
  • 1
  • 1
  • Note that the first line of the function was "hidden" on the same line as the triple back-quote used to format the code (I "found" it when I edited the question). I think that the function definition materially alters your answer. – Jonathan Leffler Sep 28 '19 at 17:11
  • You too need to read [Treating User Myopia](https://blog.codinghorror.com/treating-user-myopia/) from the early days of StackOverflow. @JonathanLeffler cannot fix everyone's ill-formatted posts! – Clifford Sep 28 '19 at 17:24
  • 1
    "analysing" the code is not the same as answering the question. There is something about the program specification that he does not understand, it is not an issue of understanding the code itself. – Clifford Sep 28 '19 at 17:26