Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

The task is: Write a program that loads a array a of n natural numbers and then forms a array b so that the elements of the array b are equal to the sum the digit 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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.

Thanks in advance!

>Solution :

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;
    while (s >= 10) {
        sum = sum + s % 10;
        s = s / 10;
    }
    return sum + s;
}

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading