Home > Blockchain >  Sum of array digits using pointer arithmetic
Sum of array digits using pointer arithmetic

Time:02-05

I need to write sum of digits for all array elements using pointer arithmetic.

Program should print: 0 1 2 5 8 16 14

#include <stdio.h>
void sumDigit(int arr[], int n) {
  int *p = arr, m, sum;
  while (p < arr   n) {
      sum=0;
      m = (*p) % 10;
      sum  = m;
      (*p) /= 10;
    p  ;
     printf("%d ", sum);
  }
}
int main() {
  int arr[] = {0, 1, 2, 14, 35, 97, 68};
  int n = sizeof(arr) / sizeof(*arr);
  sumDigit(arr, n);
  return 0;
}

This code prints only last digits (0 1 2 4 5 7 8).

How to implement pointer arithmetic for this task?

CodePudding user response:

Probably gone on long enough in general-chat. You're completely misunderstanding the purpose of the assignment. The purpose is to accomplish the task without using subscripting. E.g. you can't do this:

for (int i=0; i<n;   i)
{
    // do something with arr[i]
}

Instead, they want you to do something like this:

int *p = arr;
while (p != arr n)
{
    // do something with *p, then....

      p;
}

That's it. There are at least a dozen ways to do this for the task at hand; I can think of five right off the top of my head. One way is shown below:

#include <stdio.h>

void sumDigit(const int arr[], size_t n)
{
    const int *stop = arr   n;
    for (; arr != stop;   arr)
    {
        int sum = 0;
        int value = *arr;
        while (value != 0)
        {
            sum  = value % 10;
            value /= 10;
        }
        printf("%d ", sum);
    }
    fputc('\n', stdout);
}

int main()
{
    int arr[] = {0, 1, 2, 14, 35, 97, 68};
    size_t n = sizeof arr / sizeof *arr;
    sumDigit(arr, n);
    return 0;
}

Output

0 1 2 5 8 16 14 

Here's another way:

void sumDigit(const int arr[], size_t n)
{
    const int *p = arr;
    while (p != arr n)
    {
        int sum = 0;
        int value = *p;
        while (value != 0)
        {
            sum  = value % 10;
            value /= 10;
        }
        printf("%d ", sum);
          p;
    }
    fputc('\n', stdout);
}

And another...

void sumDigit(const int arr[], size_t n)
{
    for (const int *p = arr; p != arr n;   p)
    {
        int sum = 0;
        int value = *p;
        while (value != 0)
        {
            sum  = value % 10;
            value /= 10;
        }
        printf("%d ", sum);
    }
    fputc('\n', stdout);
}

Here's one that, as I described in general-comment, uses a purpose-driven function to compute the sum-of-digits of a provided int value, thereby making the actual sumDigit much easier to understand:

int sum_of_digits(int value)
{
    int sum = 0;
    while (value != 0)
    {
        sum  = value % 10;
        value /= 10;
    }
    return sum;
}

void sumDigit(const int arr[], size_t n)
{
    for (const int *p = arr; p != arr n;   p)
        printf("%d ", sum_of_digits(*p));
    fputc('\n', stdout);
}

All of these produce the same output, because they all ultimately do the same thing.

  •  Tags:  
  • Related