Luka Mandić
Luka Mandić

Reputation: 11

Multiply char array of integers in C

Can some one help me multiply this example with for loops in C ? well,

I have char array as example

 x[]={ '0','3','0','8','9','6','4','3','8','4','0','0','7' };

* 765432,765432

and I need mulitply elements as:

ZZZ=(0*7)+(3*6)+(0*5)+(8*4)+(9*3)+(6*2)+(4*7)+(3*6)+(8*5)+(4*4)+(0*3)+(0*2)=
 0 +18 + 0 +32 +27 +12 +28 +18 +40 +16 +0 +0 = 191

What do you think:

I tried :

int mnozi = 0;

for (i = 0; i < 12; i++) {
    //mnozi = (int)p[i]-48; 
    for (int j = 7; j > 1; j--) 
        mnozi = ((int)p[i] - 48) *j;
        printf("\n%d", mnozi);

        ZZZ = ZZZ + mnozi;
    }

and there are some strange outputs!

Upvotes: 0

Views: 1876

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84599

There are a number of ways to multiply the array by your descending 7-2 pattern repeated twice. One is a simple use of a pointer to walk down x through two iterations of the pattern. It can be coded a number of ways, another would be:

#include <stdio.h>

int main (void) {

    char x[]={ '0','3','0','8','9','6','4','3','8','4','0','0','7' }, *p = x;
    int i = 7, r = 0, z = 0;

    for (; *p && p < x + sizeof x; p++) {
        printf (" %d * %d = %d\n", *p - '0', i, (*p - '0') * i);
        z += (*p - '0') * i--;
        if (i < 2) { i = 7; if (++r > 1) break; }
    }

    printf ("\n z : %d\n\n", z);

    return 0;
}

Example Use/Output

$ ./bin/multcharray

 0 * 7 = 0
 3 * 6 = 18
 0 * 5 = 0
 8 * 4 = 32
 9 * 3 = 27
 6 * 2 = 12
 4 * 7 = 28
 3 * 6 = 18
 8 * 5 = 40
 4 * 4 = 16
 0 * 3 = 0
 0 * 2 = 0

 z : 191

A slight variation limiting the iterations by limiting the characters in x the multiplication is applied to could be:

    char x[]={ '0','3','0','8','9','6','4','3','8','4','0','0','7' }, *p = x;
    int i = 7, z = 0;

    for (; *p && p < x + sizeof x - 1; p++) {
        z += (*p - '0') * i;
        printf (" %d * %d = %d\n", *p - '0', i, (*p - '0') * i);
        if (--i < 2) i = 7;
    }

    printf ("\n z : %d\n\n", z);

The variations are endless. Look over all the answers and let us know if yo have any questions.

Upvotes: 4

MikeCAT
MikeCAT

Reputation: 75062

Using nested loop seems wrong. Try this:

#include <stdio.h>

int main(void) {
    char x[]={ '0','3','0','8','9','6','4','3','8','4','0','0','7' };
    char *p = x;
    int i, ZZZ;

    int mnozi = 0;

    for (i = 0; i < 12; i++) {
        //mnozi = (int)p[i]-48; 
        int j = 7 - i % 6;
        mnozi = ((int)p[i] - '0') *j;
        printf("\n%d", mnozi);
        printf(" : i = %d, p[i] = '%c', i %% 6 = %d, j = %d", i, p[i], i % 6, j); /* to making what is happening clearer */

        ZZZ = ZZZ + mnozi;
    }

    printf("\n%d\n", ZZZ);
    return 0;
}

Output:

0 : i = 0, p[i] = '0', i % 6 = 0, j = 7
18 : i = 1, p[i] = '3', i % 6 = 1, j = 6
0 : i = 2, p[i] = '0', i % 6 = 2, j = 5
32 : i = 3, p[i] = '8', i % 6 = 3, j = 4
27 : i = 4, p[i] = '9', i % 6 = 4, j = 3
12 : i = 5, p[i] = '6', i % 6 = 5, j = 2
28 : i = 6, p[i] = '4', i % 6 = 0, j = 7
18 : i = 7, p[i] = '3', i % 6 = 1, j = 6
40 : i = 8, p[i] = '8', i % 6 = 2, j = 5
16 : i = 9, p[i] = '4', i % 6 = 3, j = 4
0 : i = 10, p[i] = '0', i % 6 = 4, j = 3
0 : i = 11, p[i] = '0', i % 6 = 5, j = 2
191

Upvotes: 2

Related Questions