11-30-2006, 11:02 PM | #1 (permalink) |
Junkie
|
[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; } 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 1 0 1 What am I doing wrong? |
11-30-2006, 11:39 PM | #2 (permalink) | ||
Found my way back
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:
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:
|
||
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.. |
12-01-2006, 02:53 PM | #4 (permalink) | |
Psycho
Location: sc
|
Quote:
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 |
|
12-01-2006, 05:45 PM | #5 (permalink) |
Junkie
|
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; } 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. |
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; } |
12-03-2006, 11:07 AM | #7 (permalink) |
Quadrature Amplitude Modulator
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; } 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
__________________
"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 |
12-12-2006, 12:36 AM | #8 (permalink) |
Junkie
|
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; } |
Tags |
arrays, cproblem |
|
|