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 01:21 PM.

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360