Hi, I have an array of integers and I am trying to figure out how many elements in the array have exactly two "2"s. I'm not sure how to do this and was hoping someone here can point me in the right direction. Thanks in advance.
This is a discussion on Newbie question within the C Programming forums, part of the General Programming Boards category; Hi, I have an array of integers and I am trying to figure out how many elements in the array ...
Hi, I have an array of integers and I am trying to figure out how many elements in the array have exactly two "2"s. I'm not sure how to do this and was hoping someone here can point me in the right direction. Thanks in advance.
int ar[2];
ar[0] = 2' ar[1] =2; (ar[2] = '\0')
Is this what you were trying to understand....
Sorry, what I meant was if the array has numbers 12342, 111, 1341122, and 12345, then which array elements have exactly two "2" digits. Answer is 12342 and 1341122 but I don't know how to approach this.
only ar[2] has two "2" digits
ar[0] = 12342;
ar[1] = 111;
ar[2] = 1341122;
ar[3] = 12345;
Is that clearer?
Sorry I don't think you understood my question. I'm just trying to make a program that reads integer elements from an array and that prints out the elements with two '2' digits inside of it.
sounds like you need to convert the integers to strings and then count the 2s in it.
try sprintf to convert the integer into a string, and then use a loop to count the 2s.
Last edited by robwhit; 12-05-2007 at 01:38 AM. Reason: MacGyver
If you're trying to convert an integer into a string, I believe sprintf() is the correct one. sscanf() would be better suited to read an int from a string.
You can extract the digits, starting at 1's, then 10's, then 100's place and so on, from the given integer and keep track of number of 2 encountered.