Thread: Array function help!

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Array function help!

    Edit:

    I figured out my problem, it was that I am stupid. -_-

    Please delete this thread.
    Last edited by SirSig; 10-23-2011 at 04:56 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Read the number as a string directly - much easier.
    Devoted my life to programming...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by GReaper View Post
    Read the number as a string directly - much easier.
    It's probably homework, I'd venture that he is required to do it this way.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by quzah View Post
    It's probably homework, I'd venture that he is required to do it this way.
    Oh, my bad. Then, you know what to do:
    Code:
    ++a[digit];
    EDIT: ... Don't do that! A public conversation isn't deleted, and neither should you erase your post. We want others to learn from it!
    Last edited by GReaper; 10-23-2011 at 05:15 PM.
    Devoted my life to programming...

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by SirSig View Post
    Edit:

    I figured out my problem, it was that I am stupid. -_-

    Please delete this thread.
    Please don't delete your posts; it deprives everyone else of the chance to learn from it.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by SirSig View Post
    Edit:

    I figured out my problem, it was that I am stupid. -_-

    Please delete this thread.
    Ahhh, sorry I did not know the proper etiquette. I realized my problem as soon as I posted it so I did not want to take up any body's time.

    My code was:

    Code:
    /*-------------
    Include Section
    --------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    #include <time.h>
    #define SIZE 10
    
    /*------------------Main Functions------------------
    Purpose: Prompts user to input integer
    Returns: Signal to the OS
    --------------------------------------------------*/
    
    void sep_digits(long long int num);
    void print_digits(int a[], int size);
    
    int main(void) {
    
    long long int input;
    
    
    printf("Enter a positive integer or 0 (zero) to end: ");
    scanf("%lld", &input);
    
    while (input != 0) {
       if (input < 0) {
          printf("\nWrong input\n");
          }
       else {
          sep_digits(input);
       }
       printf("\nEnter a positive integer or 0 (zero) to end: ");
       scanf("%lld", &input);
       }
    printf("\n*** Program Terminated ***\n");
    return(EXIT_SUCCESS);
    }
    
    /*------------------Seperate Digits Functions------------------
    Purpose: Seperates and counts the numbers in the user's input.
    Returns: Seperated and counted value.
    --------------------------------------------------*/
    
    void sep_digits(long long int num) {
    
    
    int a[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int digit, i;
    
    while (num > 0) { // num has the value of user input
       digit = num % 10;
    
          a[digit] = a[digit] + 1;
    
       num = num / 10;
       }
    print_digits(a, SIZE);
    }
    
    /*------------------Print Digits Functions------------------
    Purpose: Prints output.
    Returns: Prints output of the sep_digit function to the user in a formatted orientation.
    --------------------------------------------------*/
    
    void print_digits(int a[], int size) {
    
    int i;
    
    printf("\n\n");
    printf("Digits:         0  1  2  3  4  5  6  7  8  9\n");
    printf("Occurrences:  ");
    for (i = 0; i < 10; i++) {
       printf("  %d",a[i]);
       }
    printf("\n");
    }
    except for at this while loop:

    Code:
    while (num > 0) { // num has the value of user input
       digit = num % 10;
    
          a[digit] = a[digit] + 1;
    
       num = num / 10;
       }
    I had improperly put the a[digit] increment in a way so that it did not actually increment a[digit] this causing no change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  2. Replies: 3
    Last Post: 12-10-2010, 08:51 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 4
    Last Post: 11-07-2001, 02:46 PM