Home > OS >  Array b are equal to the sum the digit of the array element a
Array b are equal to the sum the digit of the array element a

Time:12-25

I need help solving this problem, if anyone had a similar problem it would help me.

The task is: Write a program that loads an array a of n natural numbers and then forms an array b so that the elements of the array b are equal to the sum the digits of the array element a.

Example:
n=5
a[0]=1
a[1]=2
a[2]=3
a[3]=32
a[4]=54
Array b is:
b[0]=1
b[1]=2
b[2]=3
b[3]=5
b[4]=9

I did like this:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 100

void form(int [], int [], int);
int sum_digit(int);
void print (int [], int);

int main() {
    int n;
    printf("Enter n:");
    scanf("%d", &n);
    while (n < 1 || n > MAX) {
        int n;
        printf("Enter n:");
        scanf("%d", &n);
    }
    int a[MAX], b[MAX];
    printf("Enter element of array a:\n");
    for (int i = 0; i < n; i  ) {
        printf("%d. element:",i   1);
        scanf("%d", &a[i]);
    }
    form(a, b, n);
    print(b, n);
    return 0;
}

void form(int a[MAX], int b[MAX], int n) {
    for (int i = 0; i < n; i  ) {
        while (a[i] > 0) {
            int pom = a[i] % 10;
            b[i] = sum_digit(pom);
            a[i] = a[i] / 10;
        }
    }
}

int sum_digit(int s) {
    int sum = 0;
    sum = sum   s;
    return sum;
}

void print(int b[MAX], int n) {
    for (int i = 0; i < n; i  ) {
        printf("%d ", b[i]);
    }
}

The problem arises for multi-digit array elements. Each time the sum_digit function is called the sum is set to zero. So for example for element 32, I can't get 5 because I can't add 2 and 3. If anyone has an idea how to solve this I would be grateful.

CodePudding user response:

There are multiple problems in your code:

  • you do not test the return value of scanf() so any invalid or missing input will cause undefined behavior because the destination variables will not be updated.

  • There is a new variable n defined in the while loop, so the original variable in the outer scope is never modified by scanf().

  • form and sum_digit are both incorrect: you should compute the sum in sum_digit() with a loop and store all sums in a loop in form.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 100

void form(int [], int [], int);
int sum_digits(int);
void print (int [], int);

int main() {
    int n;
    
    for (;;) {
        printf("Enter n:");
        if (scanf("%d", &n) != 1)
            return 1;
        if (n >= 1 && n <= MAX)
            break;
        printf("Incorrect value: %d\n", n);
    }
    int a[MAX], b[MAX];
    printf("Enter elements of array a:\n");
    for (int i = 0; i < n; i  ) {
        printf("%d. element:", i   1);
        if (scanf("%d", &a[i]) != 1)
            return 1;
    }
    form(a, b, n);
    print(b, n);
    return 0;
}

void form(int a[MAX], int b[MAX], int n) {
    for (int i = 0; i < n; i  ) {
        b[i] = sum_digits(a[i]);
    }
}

int sum_digits(int s) {
    int sum = 0;

    /* this loop handles positive and negative numbers alike */
    while (s != 0) {
        sum = sum   s % 10;
        s = s / 10;
    }
    /* return the positive sum of digits */
    return abs(sum);
}

void print(int b[MAX], int n) {
    for (int i = 0; i < n; i  ) {
        printf("%d ", b[i]);
    }
    printf("\n");
}

CodePudding user response:

For starters pay attention to that as the type of elements of the array a is int then it means that the user can enter negative numbers in the array. But the sum of digits should be a non-negative value.

And the array a should not be changed. The task is just to form the array b.

Now here you are.

void form( const int a[], int b[], int n ) 
{
    for ( int i = 0; i < n; i   )
    {
        b[i] = sum_digit( a[i] );
    }
}

and

int sum_digit( int num )
{
    enum { Base = 10 };

    int sum = 0;

    do
    {
        int digit = num % Base;
        if ( digit < 0 ) digit = -digit;
        sum  = digit;
    } while ( num /= Base );

    return sum;
} 

Here is a demonstration program.

#include <stdio.h>

int sum_digit( int );

void form( const int a[], int b[], int n )
{
    for (int i = 0; i < n; i  )
    {
        b[i] = sum_digit( a[i] );
    }
}

int sum_digit( int num )
{
    enum { Base = 10 };

    int sum = 0;

    do
    {
        int digit = num % Base;
        if (digit < 0) digit = -digit;
        sum  = digit;
    } while (num /= Base);

    return sum;
}

void print( const int a[], int n )
{
    for (int i = 0; i < n; i  )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

int main( void )
{
    enum { MAX = 5 };
    int a[MAX] = { 1, -22, 333, -4444, 55555 };
    int b[MAX];

    print( a, MAX );

    form( a, b, MAX );

    print( b, MAX );
}

The program output is

1 -22 333 -4444 55555
1 4 9 16 25

CodePudding user response:

sum_digit() should calculate the sum of all the digits, not just one digit. So you need to move the loop from form() to sum_digit().

void form(int a[MAX],int b[MAX],int n)
{
    for(int i=0;i<n;i  )
    {
        b[i]=sum_digit(a[i]);
    }
}

int sum_digit(int s)
{
    int sum=0;
    while (s > 0) {
        sum  = s % 10;
        s /= 10;
    }
    return sum;
}

Since the assignment says that the array contains natural numbers, you could also change all your int declarations to unsigned.

  • Related