Thread: merge arrays homework struggle

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    13

    merge arrays homework struggle

    The good news is that I have the program working.

    The problem is that I am getting a "assignment from incompatible pointer type" warning when I compile. For example, in line 26.

    The pointers are declared as doubles, the arrays pointed to are doubles, the function prototypes are defined with double type arrays.

    I'm sure it's something stupid that I just don't understand.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int array_input ( double user[] );
    
    void array_sort ( double user[], int limit );
    
    int array_combined ( double A[], double B[], double C[], int a_loop, int b_loop );
    
    int main()
    {
        int limit = 1000;                               // limit of array size
        double A[limit], B[limit], C[ 2 * limit];       // 1,2, and 3 arrays
        int junk = 0, loop, a_loop, b_loop, c_loop;     // loop and array variables
        double *puser, *puser_B, *puser_C;              // pointer declaration
        char yes;                                       // user controlled loop
    
        // user controlled loop
        while (junk == 0 )
        {
    
        printf("Enter the numbers in the first array.\n");
        fflush(stdin);
    
    // function call for user input of first array
        puser = &A;
        a_loop = array_input( puser );
    
        printf("Enter the numbers in the second array.\n");
        fflush(stdin);
    
    // function call for user input of second array
        puser = &B;
        b_loop = array_input( puser );
    
    
        // function call to sort first array
        printf("\nThe first set of numbers sorted in descending order are:\n");
        puser = &A;
        array_sort( puser, a_loop );
    
    
    // function call to sort second array
        printf("\nThe second set of numbers sorted in descending order are:\n");
        puser = &B;
        array_sort( puser, b_loop );
    
    
        // function call to combine arrays
        puser = &A;
        puser_B = &B;
        puser_C = &C;
        c_loop = array_combined ( puser, puser_B, puser_C, a_loop, b_loop );
    
    
        // function call to sort combined array
        array_sort( puser_C, c_loop );
    
        printf("\n");
    
    
         printf("\nThe combined loop is:\n");
         for ( loop=0; loop < c_loop; loop++)
            printf("%.1f\n",C[loop]);
    
    
        printf("Would you like to run the program again? y for yes\n");
        fflush(stdin);
        scanf("%c", &yes);
    
        if (yes != 'y')
        exit(0);
        }
        return 0;
    }
    
    
    // input array
    int array_input ( double user[] )
    {
        int loop = 0, x, finished;       // looping variable
        char yes;                        // user controlled variable
    
    while ( finished == 0) {
    
        while (scanf("%lf",&user[loop]))
        {
            loop++;
        }
    
        printf("Do you want to stop entering numbers? y for yes.\n");
        fflush(stdin);
        scanf("%c", &yes);
    
        if ( yes == 'y' ) {
            finished = 1;
        }
    
        if ( yes != 'y')
            printf("Enter the next number >");
    }
    
        printf("The numbers you entered were:\n");
        for ( x=0; x < loop; x++)
            printf("%.1f\n",user[x]);
    
    
        return (loop);
    }
    
    
    void array_sort ( double user[], int limit )
    {
        int loop,x;             // loop variables
        double temp;            // temp variable
    
        // set limit of array size
            loop = limit;
        // shorten array after largest moved to end
          for ( x = 1 ; x < loop  ; x++ )
            {
            // loop to move highest number to end of array
            for (limit = 0; limit < loop - x ; limit++)
                {
                   if ( user[limit] < user[limit+1] )
                   {
                       temp = user[limit+1];
                       user[limit+1] = user[limit];
                       user[limit] = temp;
                   }
                }
            }
    
        // print sorted array
        for ( x=0; x < loop; x++)
            printf("%.1f\n",user[x]);
    
    }
    
    // function to combine arrays
    int array_combined ( double A[], double B[], double C[], int a_loop, int b_loop )
    {
       int x=0, y=0, z=0,w;   // loop variables
    
    // combine arrays to point where size of arrays are equal
     while(x < a_loop && y < b_loop )
     {
      if(A[x] <= B[y])
       {
       C[z]=A[x];
       x++;
       z++;
       }
      else
       {
       C[z]=B[y];
       z++;
       y++;
       }
     }
    
    // if A is larger, add in remaining elements of A
    
     while( x < a_loop )
      {
      C[z]=A[x];
      x++;
      z++;
      }
    
    // if B is larger, add in remaining elements of B
    
     while( y < b_loop )
      {
      C[z]=B[y];
      z++;
      y++;
      }
    
      // eliminate duplicates
      for( x = 0; x < z; x++ )
    {
       for( y = x + 1; y < z; )
       {
          if( C[y] == C[x])
          {
             for( w = y; w < z ; w++ )
                 C[w] = C[w+1];
              z--;
          }
          else
             y++;
       }
    }
    
      return(z);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The good news is that I have the program working.

    The problem is that I am getting a "assignment from incompatible pointer type" warning when I compile. For example, in line 26.
    First if that second line is true then the first line is necessarily false.

    There are several problems with your code:

    First do you realize that you are trying to use VLA (Variable Length Arrays)? This feature is only guaranteed to be available when compiling with a C99 compatible compiler. Since you seem to be using the Microsoft compiler I recommend you stop using this feature, since this compiler is not C99 compliant. I'd recommend you #define the constant outside of main() to avoid this issue.

    Second fflush() is only defined in the standard to work with output streams, using it on input streams is undefined. You can eliminate the need for most of your fflush() calls by changing your scanf() calls to fix the problem. Put a space before the percent symbol to skip leading whitespace.

    Code:
    scanf(" %c", &someCharacterVariable);
    Third since A[] is an array the name of the array without any brackets (A) is a pointer to the first element, so no need for the ampersand.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by jimblumberg View Post
    Third since A[] is an array the name of the array without any brackets (A) is a pointer to the first element, so no need for the ampersand.
    or you could use &A[0].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge two arrays together
    By Fedryan in forum C Programming
    Replies: 2
    Last Post: 11-04-2013, 01:11 PM
  2. sub-arrays for merge Sort
    By abood1190 in forum C Programming
    Replies: 3
    Last Post: 05-15-2013, 05:35 PM
  3. Can you merge arrays?
    By SeriousTyro in forum C Programming
    Replies: 14
    Last Post: 09-21-2009, 10:38 AM
  4. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 04:47 PM
  5. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM