Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 11-30-2006, 11:02 PM   #1 (permalink)
Junkie
 
zero2's Avatar
 
[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?
zero2 is offline  
Old 11-30-2006, 11:39 PM   #2 (permalink)
Found my way back
 
healer's Avatar
 
Location: South Africa
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?
__________________
Quote:
Originally Posted by The_Jazz
Ok - can I edit my posts to read "what healer said"?
healer is offline  
Old 12-01-2006, 12:47 PM   #3 (permalink)
Junkie
 
Location: San Francisco
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.

Last edited by n0nsensical; 12-01-2006 at 12:53 PM..
n0nsensical is offline  
Old 12-01-2006, 02:53 PM   #4 (permalink)
Psycho
 
noodles's Avatar
 
Location: sc
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.
__________________
This is what is hardest: to close the open hand because one loves.
Nietzsche
noodles is offline  
Old 12-01-2006, 05:45 PM   #5 (permalink)
Junkie
 
zero2's Avatar
 
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.
zero2 is offline  
Old 12-01-2006, 09:14 PM   #6 (permalink)
Junkie
 
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;
}
Rekna is offline  
Old 12-03-2006, 11:07 AM   #7 (permalink)
Quadrature Amplitude Modulator
 
oberon's Avatar
 
Location: Denver
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.
__________________
"There are finer fish in the sea than have ever been caught." -- Irish proverb

Last edited by oberon; 12-03-2006 at 11:28 AM.. Reason: tweak wording a bit
oberon is offline  
Old 12-12-2006, 12:36 AM   #8 (permalink)
Junkie
 
zero2's Avatar
 
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;
}
zero2 is offline  
 

Tags
arrays, cproblem


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:40 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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