Tilted Forum Project Discussion Community  

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


 
 
LinkBack Thread Tools
Old 12-12-2006, 01:03 AM   #1 (permalink)
Junkie
 
zero2's Avatar
 
[c]Storing values into an array

I have a problem, and I'm not sure how to begin.

The problem I have is to write a function that asks a user to enter an integer, then output the number of times a digit appears.

So my ouput looks like this:

Enter an integer: 1034029
The sign : +
The counts of digits:
0 2
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
9 1

Since this problem looks similar to the last problem I had, and posted in another thread, I guess what I was thinking of doing is storing the user inputed integer into an array, after that's done everything else should be just like the problem in the other thread.

What would be the best way of storing the values into an array. Should I create a loop to store the values, and once a return value '\0' is entered it stops, or should I use gets? Or is there a better way of doing this?
zero2 is offline  
Old 12-14-2006, 07:32 PM   #2 (permalink)
Junkie
 
One way is to just input it as a c_style string

Code:
char line[1000];
cin >> line;

otherwise input it as an integer and use divide and mod to extract the digits. Both are easy to do.
Rekna is offline  
Old 12-14-2006, 08:21 PM   #3 (permalink)
Junkie
 
zero2's Avatar
 
I came up with something, but there's probably a way to simplify it even more. Here's my code:

Code:
#include <stdio.h>

void displayIntegerDigit(char[]);

int main ( void ) 
{
	char cAry1[81];
	
	printf( "\nEnter an integer: " );
	gets(cAry1);
	
  displayIntegerDigit(cAry1);
	
	
	return 0;
}
	
	
void displayIntegerDigit(char cTest[])
{
	int iA;
	int i;
	
	printf("\nThe sign: +\n");
	printf("The counts of digits:\n");
	
	iA = 0;
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '0' )
				{
					iA++;
				}
		}
	printf("\t\t0 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '1' )
				{
					iA++;
				}
		}
	printf("\t\t1 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '2' )
				{
					iA++;
				}
		}
	printf("\t\t2 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '3' )
				{
					iA++;
				}
		}
	printf("\t\t3 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '4' )
				{
					iA++;
				}
		}
	printf("\t\t4 and %d\n", iA);

	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '5' )
				{
					iA++;
				}
		}
	printf("\t\t5 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '6' )
				{
					iA++;
				}
		}
	printf("\t\t6 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '7' )
				{
					iA++;
				}
		}
	printf("\t\t7 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '8' )
				{
					iA++;
				}
		}
	printf("\t\t8 and %d\n", iA);
	
	iA = 0;
	
	for ( i = 0; i < 81; i++ )
		{
			if (* ( cTest + i ) == '9' )
				{
					iA++;
				}
		}
	printf("\t\t9 and %d\n", iA);
	
  return;
}
zero2 is offline  
Old 12-14-2006, 11:14 PM   #4 (permalink)
Junkie
 
Your method is very bad. You are looping through the same string multiple times.

Instead use 1 loop over the string and use an array of counters to count the digits. You should be able to reduce your loop body to 1 line of code (if you assume valid input).
Rekna is offline  
Old 01-12-2007, 08:48 AM   #5 (permalink)
Upright
 
Sorry I'm late, but here's one way to do it. I hope the comments help:

Code:
/* count.c
 *
 * Count the occurance of each numeral in a string.
 */

#include <stdio.h>

int main( void )
{
   /* When we count up the frequency of each number, we'll store it in
    * this array.  The array is initialized to zero.
    */
   int total[10] = { 0 };

   /* We'll use a character pointer to move through our string later. */
   char *pointer;

   /* Get the number from the user.  We'll limit the number length to 51,
    * which allows 50 digits and room for the terminating null.
    */
   char number[51];

   puts( "Enter an integer:" );

   /* Using fgets() instead of gets() helps avoid buffer overflows. */
   fgets( number, 51, stdin );

   /* Loop through the number a single time, counting the frequency
    * of each digit, 0-9.
    *
    * The 'for' loop is initialized by pointing the character pointer to the
    * beginning of 'number' (its address).  We continue the loop as long
    * as the pointer references something other than a zero (the null at
    * the end of the string is a zero, not to be confused with the
    * character '0' the user might provide).  Finally, we increment the
    * pointer across the string on each iteration.
    */

   for ( pointer = number; *pointer; pointer++ )
   {
      /* I'm not sure if you've learned about switch...case in class yet,
       * so I hope this is okay.
       */
      switch( *pointer )
      {
      case '0': total[0]++; break;
      case '1': total[1]++; break;
      case '2': total[2]++; break;
      case '3': total[3]++; break;
      case '4': total[4]++; break;
      case '5': total[5]++; break;
      case '6': total[6]++; break;
      case '7': total[7]++; break;
      case '8': total[8]++; break;
      case '9': total[9]++; break;
      }
   }

   /* Display the final count.  I split it across two printf()'s
    * purely for aethetics.  One would certainly do the trick.
    */
   printf( "0: %d\n1: %d\n2: %d\n3: %d\n4: %d\n",
      total[0], total[1], total[2], total[3], total[4] );

   printf( "5: %d\n6: %d\n7: %d\n8: %d\n9: %d\n",
      total[5], total[6], total[7], total[8], total[9] );

   return 0;
}
Sample output:
Code:
Enter an integer: 8675309 - Jenny don't lose my number!
0: 1
1: 0
2: 0
3: 1
4: 0
5: 1
6: 1
7: 1
8: 1
9: 1
The switch...case works to ignore non-numbers without allowing it to break the code.
Tyrell is offline  
 

Tags
array, cstoring, values


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:08 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 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