Printing int arrays like a string?

Discussion in 'Mac Programming' started by larswik, Dec 25, 2010.

  1. larswik, Dec 25, 2010
    Last edited: Dec 25, 2010

    larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    So a char array can be simply printed using the %s. Is their also an EASY was to print a int array for must I create some kind of loop to do it? I am doing a test writing a random number generating guessing game, and I want to populate an array with ints.

    I was testing my program as I went along to check to see if the array was populated correctly. I was having to do it 1 at a time since my array was an int and not a char.

    -Lars

    EDIT: I found another way of doing this that works. Thanks
     
  2. lee1210, Dec 25, 2010
    Last edited: Dec 25, 2010

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    There's no good terminator for an int array for the library to depend on, so no dice. Not sure what method you came up with, but a function that accepts the base of the array and its length as arguments and prints in a loop might do it. Then you don't need loops muddying up other code, you can just invoke the function.

    -Lee
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    Can you post more about the solution you found?

    1. It might help someone else
    2. Talking about it helps you grasp the concepts better
    3. It may stimulate further ideas and discussion

    B
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    Sure... The goal is to work with array's to better understand them. First I wanted to populate the char array with '-' just dashes. Then replace one of the chars with the random number that was an int, bad idea, there is a big difference between 5 and '5'. I then replaced one of the Chars with 'Y' instead of trying to do it with an int with this code: arrayHolder[randNum] = 'Y'; I have tests built in that give away the answer to see if it worked correctly.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define total 10
    
    int main (int argc, const char * argv[]) {
    
    	int randNum, i, playerGuess;
    	char arrayHolder[total];
    	
    	srand(clock());
    	randNum = rand() %total; //Gen a num 0 - 9
    	
    	printf("the random number is %d\n", randNum); //Test to see the num
    	
    	for (i=0; i<= total; i++)
    	{
    		arrayHolder[i] = '-';
    		arrayHolder[randNum] = 'Y';
    	}
    	
    	printf("The array looks like this: %s\n", arrayHolder);// test to look at the array
    	
    	printf("Enter a number from 0 to 9: ");
    	scanf("%d", &playerGuess);
    	
    	if (arrayHolder[playerGuess] == 'Y')
    		printf("You guessed it!");
    	else
    		printf("Wrong! it was: %d", randNum);
    	
        return 0;
    }
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    A tip (that may not be immediately applicable):
    You can subtract '0' from '0'-'9' to get the integer value for that digit.

    -Lee
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    I think it's already on larswik's mind. ;)

    Converting from char to int in this way is a good way to learn about converting between data types and learning something about the ASCII character set. The same general technique can be useful for letters or converting uppercase to lowercase and back.

    B
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    All right - Let me understand this since I did see / read about this a little. If I want to start with int value of lets say 2, it is stored as the ASCII value of 50 the same way that 'a' is stored as 97. ( my terminology might be a little off).

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
    
    	char letter;
    	
    	letter = 'a';
    	
    	printf("the letter number is %d", letter);
        return 0;
    }
    So if I understand you correctly, if I have char value of 9, it's stored as the ASCII of 57 and if I subtract '0' which is ASCII value 48 from ASCII 57 I get 9.

    So this code would convert my char to an int. Is this called type casting or data type conversion, or do they mean the same thing?

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
    
    	char let1, let2;
    	int result;
    	
    	let1 = '7';
    	let2 = '0';
    	
    	result = let1 - let2;
    	
    	printf("The number value is %d", result);
    	
        return 0;
    }
    Thanks again for you help guys!

    -Lars
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Calling this "conversion" might be the most accurate here. You have a digit stored as a character using an encoding (ASCII). You want to get the integer value this character represents. So you have to "map" the encoded digit to the integer value it represents. As a convenience we use '0' to do this mapping instead of remembering it's ASCII value.

    Your code looks sound to me. There is a typecast involved, but not where you think. Typecasting is taking a value of one type and getting the best match for this value as another type. This can be done explicitly or implicitly. In this case when you subtract one char from another, the result is a char. You want to store the result as an int, so the char (which is a signed integer) must be cast up to an int (which is likely a wider signed integer). This is pretty cheap because these values are stored as 2's complement (don't worry about this just yet) so the highest bit is used to "extend" the value, filling all the bits needed to make up for the difference between the width of a char and an int. A typecast can be done between all primitives, with varying degrees of difficulty/accuracy. When casting down precision is lost so bad things can happen.

    -Lee
     
  9. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #9
    Another example of this type of conversion, you could add 32 to any uppercase letter, and get the resulting lower case. ie. Adding 32 to 'A' and it would result in the lowercase 'a'
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Or adding 'a'-'A'.

    -Lee
     
  11. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #11

    i<= total should be i<total
    and you should also set
    arrayHolder[total]=0;
    with
    char arrayHolder[total+1];
    to terminate the %s
    arrayHolder[randNum] = 'Y'; can also be done after the loop
     
  12. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006

Share This Page