![]() |
![]() |
#1 (permalink) |
Junkie
|
[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? |
![]() |
![]() |
#3 (permalink) |
Junkie
|
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; } |
![]() |
![]() |
#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; } 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 |
![]() |
Tags |
array, cstoring, values |
|
|