Thread: help with array

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    12

    help with array

    Greetings fellow programmers,

    I am having difficulty in making a function that checks an integer array for the number '3' and whichever number contains it, moves it to a new array and makes it in descending order.

    I am stuck with the moving part and here is what I have done so far:

    Code:
    void check(int x[], char y[], int z[]){
        
        int i, j = 0;
        char c = 3;
        for (i = 0; i < MAX; i++)
            y[i]=x[i];
        for (i = 0; i < MAX; i++)
            while (y[i] != NULL)
            {
                if (y[i] == c)
                {
                    z[j] = y[i];
                    j++;
                }
            }
        for (i = 0; i < j; i++)
            printf("array2[%d]=%d\n", i, z[i]);
    }
    I have a feeling that I am far away from making it works

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Huh...

    If I think I understand what you're saying, you want to search an array for the number 3. If it is found, the index of the array will be stored. The stored indexes will be sorted.

    I think that's what you're saying. In which case...
    Code:
    const int data[4] = { 3, 0, 3, 1 };
    
    size_t indexes[4] =  { 0 };
    size_t num_indexes;
    
    for (size_t i = 0; i < data; ++i) {
      if (data[i] == 3) {
        indexes[num_indexes] = i;
        ++num_indexes;
      }
    }
    
    // sort indexes

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    Thanks for the prompt reply.

    I need any number that contain '3' to be stored i.e. 13, 2223, 39 etc. not only 3.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,739
    Quote Originally Posted by ghost00359
    I need any number that contain '3' to be stored i.e. 13, 2223, 39 etc. not only 3.
    I suggest that you start by writing a function:
    Code:
    int has_digit(int number, int digit);
    has_digit will return 1 if number has the given digit, otherwise it will return 0. For example has_digit(2223, 3) == 1, but has_digit(4445) == 0.

    Once you have implemented this function and tested that it works correctly, you will find it much easier to accomplish the task of moving the relevant numbers to a new array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    Wow thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM