Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [c]problem with arrays (https://thetfp.com/tfp/tilted-technology/111109-c-problem-arrays.html)

zero2 11-30-2006 11:02 PM

[c]problem with arrays
 
I'm having a problem storing a value from an array of type char into an array of type int.

Code:


/**
* Program Name: --
*
*/

#include <stdio.h>
#define MAX 10

/* Function Prototype */
void extractInfo(char[],int, int[]);

int main()
{
        char cInputOld[10] = { 'a', '1', 'r', 'o', '0', 'm', 'y', '1', 'i', 'b' };
        int iBinary[13];
        int iSize;
       
        iSize = MAX;
       
        extractInfo(cInputOld, iSize, iBinary);//Function call
       
        return 0;
}
/* Function */
void extractInfo(char cInputOld[], int iSize, int iBinary[])
{
 int i, j;
 
 printf("Given the array of characters:\n");
 for( i = 0; i < iSize; i++)
 {
 printf("cInputOld[%d] = %c", i, cInputOld[i]);
 printf("\n");
 }
 
 j = 0;
 
 printf("Array of binary digits:\n");
               
        for ( i = 0; i < iSize; i++)
                        {
                                if (cInputOld[i] == '0'|| cInputOld[i] == '1')//store char 0 or char 1
                                {
                                        for( j= 0; j < iSize; j++)
                                        {
                                        iBinary[j] = cInputOld[i];//assign cInputOld value to iBinary
                                        }
                                }
                        }
               
        for( j = 0; j < 13; j++)
        {
                printf("%3d", iBinary[j]);//print iBinary array
        }
      return;
 }

Output:

Code:

Given the array of characters:
cInputOld[0] = a
cInputOld[1] = 1
cInputOld[2] = r
cInputOld[3] = o
cInputOld[4] = 0
cInputOld[5] = m
cInputOld[6] = y
cInputOld[7] = 1
cInputOld[8] = i
cInputOld[9] = b
Array of binary digits:
 49 49 49 49 49 49 49 49 49 49  0  0  0

Instead Array of binary digits should have or at least the first 3 indices should be:

1 0 1

What am I doing wrong?

healer 11-30-2006 11:39 PM

As far as I know (and that's not alot), you cannot just assign a variable of one type to a variable of another type. Which is probably why
Quote:

Originally Posted by zero2
iBinary[j] = cInputOld[i];//assign cInputOld value to iBinary

doesn't work.

I'm just guessing here, but don't you need to parse the char variable to an int before you can assign the value to the iBinary array?

n0nsensical 12-01-2006 12:47 PM

Thats actually not a problem because char can be implicitly casted to int. They are both integer types, only the size is different and since int is bigger than char there's no type problem with the cast and the compiler does it automatically. There could be a signed/unsigned issue, but I think in most cases char and int are both signed by default, and at worst the compiler will issue a warning and still do the implicit cast.

Your problem is with the inner for loop in the function call. You don't need a loop there at all. Just initialize j to 0 before entering the (outer) loop, and when there's a successful test of cInputOld[i], assign to iBinary[j] and increment j, unless you're trying to do something other than what I'm thinking. Also, you should initialize the iBinary array or only print the values that were actually stored.

noodles 12-01-2006 02:53 PM

Quote:

Originally Posted by n0nsensical
Your problem is with the inner for loop in the function call. You don't need a loop there at all. Just initialize j to 0 before entering the (outer) loop, and when there's a successful test of cInputOld[i], assign to iBinary[j] and increment j, unless you're trying to do something other than what I'm thinking. Also, you should initialize the iBinary array or only print the values that were actually stored.

what he said.

what your code currently does is every time there's a '1' or '0', it loops through iBinary[] and replaces every value, up to the size of cInputOld[], with the integer value for the character. since '1' was the last one in cInputOld[], it wrote 9 (size of cInputOld[]) 49's into iBinary[]. if you want it to set itself to 1 or 0, the integer, make sure to subtract the integer 48 from the value of the character. that'll get your numbers in line. only your numbers.

zero2 12-01-2006 05:45 PM

Code:

/**
* Program Name: --
*
*/

#include <stdio.h>
#define MAX 10

void extractInfo(char[],int, int[]);
int main()
{
        char cInputOld[10] = { 'a', '1', 'r', 'o', '0', 'm', 'y', '1', 'i', 'b' };
        int iBinary[13] = { -1 } ;
        int iSize;
       
        iSize = MAX;
       
        extractInfo(cInputOld, iSize, iBinary);
       
        return 0;
}
void extractInfo(char cInputOld[], int iSize, int iBinary[])
{
 int i, j;
 
 printf("Given the array of characters:\n");
 for( i = 0; i < iSize; i++)
 {
 printf("cInputOld[%d] = %c", i, cInputOld[i]);
 printf("\n");
 }
 
 j = 0;
 
 printf("Array of binary digits:\n");
               
        for ( i = 0; i < iSize; i++)
                        {
                                if (cInputOld[i] == '0'|| cInputOld[i] == '1')
                                {
                                        iBinary[j] = cInputOld[i] - 48;
                                        j++;
                                       
                                }
                        }
                       
               
        for( j = 0; j < 13; j++)
        {
                printf("%3d", iBinary[j]);
        }
 return;
 }

After this recent update, now I want the output to look like the following:

1 0 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

I thought if I initialized the iBinary upfront:
iBinary[13] = { -1 }; it should assign the value of -1 to all indices.

btw thank you everyone for your help.

Rekna 12-01-2006 09:14 PM

What exactly are you trying to do?

Also use a switch statement instead of the if. It should be more efficient since it can use look up tables and then if you want to do more than just 0 and 1 for your digits you can change them all. Just do something like this:

Code:

switch(cInputOld[i])
{
  case '0': case '1':
        iBinary[j] = cInputOld[i] - 48;
        j++;
        break;
}


oberon 12-03-2006 11:07 AM

My guess is he wants to print out the binary form of each character. Normally, you could print that using a printf format, but the C standard doesn't require such a format conversion. Here's how to do it.

b.c:
Code:

#include <stdio.h>
void
print_string_in_binary(const char *str)
{
    int i;

    while (*str)
    {
        printf("%d-", *str);
        for (i = 6;i >= 0;i--)
        {
            printf("%d", *str >> i & 0x1);
        }
        printf(" ");
        str++;
    }
    printf("\n");
}
int
main(void)
{
    const char *s = "hello world\n";

    printf("s = %s\n", s);
    print_string_in_binary(s);
    return 0;
}

The key is to print out each bit of a character by shifting it to the least significant bit and bitwise ANDing the resulting integer with 1, which is the maximum binary value. That's what this does: "*str >> i & 0x1". Those of you not well versed with pointers should try to think of *str as str[i] with i being incremented whenever you increment str (note the "i" used here has no relation to the "i" in the print_string_in_binary function).

Result:
Code:

puck% gcc -Wall -o b b.c && ./b
s = hello world

104-1101000 101-1100101 108-1101100 108-1101100 111-1101111 32-0100000 119-1110111 111-1101111 114-1110010 108-1101100 100-1100100 10-0001010

A quick check indicates that this is the correct output.

zero2 12-12-2006 12:36 AM

Thanks for getting me started, I came up with a solution to my problem:

Code:


#include <stdio.h>

#define MAX 20
void printInfo(void);
int extractInfoVerB(char[], int, int[]);

int main ()
{
        int iSize;
        char cAryInputOld[20] = { '1', '0', '0', '1', '1', '0', '0',
                                                                                                        '0', '1', '0', '0', '0', '0', '2',
                                                                                                        'a', '0', 's', '1', '4', 'e' };
                                                                                                       
        int iBinaryAryOld[24] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                                                                                                                -1, -1, -1, -1, -1, -1, -1, -1 };
       
        iSize = MAX;
        printInfo();
        extractInfoVerB(cAryInputOld, iSize, iBinaryAryOld);
       
        return 0;
}

void printInfo(void)
{
        printf("Exercise #2\n");
        printf("zero2\n\n");
       
        return;
}

int extractInfoVerB(char cAryInputOld[], int iSize, int iBinaryAryOld[])
{
        int i, j, iTmp, iVal;
       
        j = 0;
       
        for (i = 0; i < iSize; i++)
                {
                        if (cAryInputOld[i] == '0' || cAryInputOld[i] == '1')
                                {
                                        iBinaryAryOld[j] = cAryInputOld[i] - 48;
                                        j++;
                                }
                }
        iTmp = 0;
    // # of occurences of 0
        for ( i = 0; i < iSize; i++)
                {
                        if (iBinaryAryOld[i] == 0)
                                {
                                        iTmp = iTmp + 1;
                                }
                }
               
        iBinaryAryOld[iSize + 0] = iTmp;
        iTmp = 0;
        // # of occurences of 1
        for ( i = 0; i < iSize; i++)
                {
                        if (iBinaryAryOld[i] == 1)
                                {
                                        iTmp = iTmp + 1;
                                }
                }
               
        iBinaryAryOld[iSize + 1] = iTmp;
        iTmp = 0;
        //# of occurences of pattern 11
        for ( i = 0; i < iSize; i++)
                {
                        if (iBinaryAryOld[i] == 1 && iBinaryAryOld[i+1] == 1)
                        {
                                iTmp = iTmp + 1;
                        }
                        else i = i + 2;
                }
        iBinaryAryOld[iSize + 2] = iTmp;
        iTmp = 0;
        //# of occurences of pattern 01
        for (i = 0; i < iSize; i++)
                {
                        if (iBinaryAryOld[i] == 0 && iBinaryAryOld[i+1] == 1)
                        {
                                iTmp = iTmp + 1;
                        }
                }
        iBinaryAryOld[iSize + 3] = iTmp;
        iTmp = 0;

        printf("Given the array of characters:\n  ");
        for (i = 0; i < iSize; i++)
                {
                        printf("%c", cAryInputOld[i]);
                }
       
       
        printf("\nThere is/are %d character (s) in the given array.\n\n", iSize);
       
        printf("Array of binary information: \n  [");
       
        for ( i = 0; i < iSize + 4; i++ )
                {
                        printf("%3d", iBinaryAryOld[i]);
                }
                printf("  ]");
               
                if( iBinaryAryOld[iSize] != 0 || iBinaryAryOld[iSize + 1] != 0 )
                        {
                                iVal = 1;
                                printf("\n\nThere is at least one character digit (0 or 1) stored in the array.");
                        }
                else
                        {
                                iVal = 0;
                        }
       
                        return iVal;
}



All times are GMT -8. The time now is 05:11 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73