Thread: Array of pointers program help please!

  1. #16
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include <crtdbg.h>  // do not remove
    
    #define FLUSH while (getchar() != '\n')
    #define MEM_ERROR printf("Not enough memory!\n")
    
    // Prototype Declarations
    double getNum(void);
    double insertionSort(double *ptr, int counter);
    
    int main (void)
    {
    
    //Local Definitions
    double *ptr;
    double **pptr;
    int counter;
    int i;
    
    double number;
    double* p_walker;
    
    
           //  Statements
           printf("\t\t Homework 3 - Pointer Applications:\n"
                      "\t\t    Insertion Sort and Pointers\n\n" );
           if(!(pptr = malloc(1000 * sizeof (*pptr)))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
    
           if(!(ptr = malloc(1000 * sizeof ( *ptr )))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
    
           printf("\nPlease enter numbers and enter -1 when you are done: ");
           number = getNum();
           p_walker = ptr;
           while(number!= -1){
    
                   p_walker = &number;               pptr[counter] = p_walker;
                   printf("entered1: %lg \n", number);
                   printf("entered: %lg \n", *p_walker);
                   number = getNum();
                   p_walker++;
                   counter++;
    
           }
                   printf("endpointer = %ld startPointer = %ld Counter = %d\n", p_walker, ptr, counter);
    
    
    
    
           for (i = 0; i < counter; i++) {
                   //printf("%lg\t",*(ptr+i));
                   printf("%lg\t", *(*pptr + i));
           }
           ptr = realloc(ptr, counter * sizeof(double));
           if( ptr == NULL )
              MEM_ERROR;
           else
              ptr = ptr;
    
    
    
           insertionSort(ptr, counter);
    
           //  printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");  // do not remove
           printf("\n\t\tEnd of Program\n"
                      "\n\t\tHave a great day!\n");
    
           return 0;
    
    }
    
    /************************************************
     Prompt the user to enter a valid integer number
     Pre: nothing
     Post: returns a valid integer
    */
    double getNum(void)
    {
     double num;
           while (scanf("%lg", &num) != 1)
           {
                         // scanf returns 1 if number read corrrectly
               FLUSH;    // to "consume" the invalid input
              printf("-*- Invalid number. Please re-enter: ");
           } // while
    
    
       return num;
    }
    
    double insertionSort(double *ptr, int counter)
    {
    int i, j;
    double temp;
    
           for(i = 1; i < counter; i++)
           {
                   temp = ptr[i];
                   j = i - 1;
                           while(temp < ptr[j] && j >= 0)
                           {
                                   ptr[j + 1] = ptr[j];
                                   j = j - 1;
                           }
                   ptr[j + 1] = temp;
           }
    
           printf("\n The ascending order of the list is: \n");
           for (i = 0; i < counter; i++)
           {
                   printf("\n%d", ptr[i]);
           }
    }
    -Hope this version turns out better. It tends to get messed up when I email it to myself through Unix.

    Crap, no luck. Sorry.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by hotshotennis View Post
    P.S. Sorry if my code is making your eyes bleed.
    True. The colour is the worst. Kurt

  3. #18
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by ZuK View Post
    True. The colour is the worst. Kurt
    Its from Unix. Its a little hard to get it to make a nice color. Ill try again right now though so hopefully someone can help me out.

  4. #19
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include <crtdbg.h>  // do not remove
    
    #define FLUSH while (getchar() != '\n')
    #define MEM_ERROR printf("Not enough memory!\n")
    
    // Prototype Declarations
    double getNum(void);
    double insertionSort(double *ptr, int counter);
    
    int main (void)
    {
    //Local Definitions
    double *ptr;
    double **pptr;
    int counter;
    int i;
    double number;
    double* p_walker;
    
    
           //  Statements
           printf("\t\t Homework 3 - Pointer Applications:\n"
                      "\t\t    Insertion Sort and Pointers\n\n" );
           if(!(pptr = malloc(1000 * sizeof (*pptr)))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
    
           if(!(ptr = malloc(1000 * sizeof ( *ptr )))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
    
           printf("\nPlease enter numbers and enter -1 when you are done: ");
           number = getNum();
           p_walker = ptr;
           while(number!= -1){
                   p_walker = &number;
                   pptr[counter] = p_walker;
                   printf("entered1: %lg \n", number);
                   printf("entered: %lg \n", *p_walker);
                   number = getNum();
                   p_walker++;
                   counter++;
    
           }
                   printf("endpointer = %ld startPointer = %ld Counter = %d\n", p_walker, ptr, counter);
    
    
    
           for (i = 0; i < counter; i++) {
                   //printf("%lg\t",*(ptr+i));
                   printf("%lg\t", *(*pptr + i));
           }
           ptr = realloc(ptr, counter * sizeof(double));
           if( ptr == NULL )
              MEM_ERROR;
           else
              ptr = ptr;
    
           insertionSort(ptr, counter);
    
           //  printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");  // do not remove
           printf("\n\t\tEnd of Program\n"
                      "\n\t\tHave a great day!\n");
    
           return 0;
    
    }
    
    /************************************************
     Prompt the user to enter a valid integer number
     Pre: nothing
     Post: returns a valid integer
    */
    double getNum(void)
    {
     double num;
           while (scanf("%lg", &num) != 1)
           {
                         // scanf returns 1 if number read corrrectly
               FLUSH;    // to "consume" the invalid input
              printf("-*- Invalid number. Please re-enter: ");
           } // while
    
    
       return num;
    }
    
    double insertionSort(double *ptr, int counter)
    {
    int i, j;
    double temp;
    
           for(i = 1; i < counter; i++)
           {
                   temp = ptr[i];
                   j = i - 1;
                           while(temp < ptr[j] && j >= 0)
                           {
                                   ptr[j + 1] = ptr[j];
                                   j = j - 1;
                           }
                   ptr[j + 1] = temp;
           }
    
           printf("\n The ascending order of the list is: \n");
           for (i = 0; i < counter; i++)
           {
                   printf("\n%d", ptr[i]);
           }
    
    
    
    }
    

    I give up. It keeps giving me this gray color no matter how I paste my code.
    Last edited by hotshotennis; 05-12-2012 at 05:39 PM.

  5. #20
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Your latest code gives the following edited warnings in Pelles C v6.00.4 at warning level 1:
    Code:
    C:\...\main.c(49): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double *'.
    C:\...\main.c(49): warning #2234: Argument 3 to 'printf' does not match the format string; expected 'int' but found 'double *'.
    C:\...\main.c(112): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double'.
    C:\...\main.c(92): warning #2096: Missing return value.
    So you have problems with the format strings on lines 49 and 112. The function insertionSort(...) (defined starting on line 92) says it returns a double value but does not.

    In message #1 of the thread, you quoted part but not all of the error or warning message. Most compilers include the line number on which it thinks the error occurred as part of the error or warning message. This is an important piece of information. However, sometimes the actual issue is on a line prior to the one mentioned in the message.

    You may want to turn up the warning options or level on whichever compiler you are using.

  6. #21
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by pheininger View Post
    Your latest code gives the following edited warnings in Pelles C v6.00.4 at warning level 1:
    Code:
    C:\...\main.c(49): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double *'.
    C:\...\main.c(49): warning #2234: Argument 3 to 'printf' does not match the format string; expected 'int' but found 'double *'.
    C:\...\main.c(112): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double'.
    C:\...\main.c(92): warning #2096: Missing return value.
    So you have problems with the format strings on lines 49 and 112. The function insertionSort(...) (defined starting on line 92) says it returns a double value but does not.

    In message #1 of the thread, you quoted part but not all of the error or warning message. Most compilers include the line number on which it thinks the error occurred as part of the error or warning message. This is an important piece of information. However, sometimes the actual issue is on a line prior to the one mentioned in the message.

    You may want to turn up the warning options or level on whichever compiler you are using.
    Thank you for your help. All of these issues have already been resolved except for the return value of insertionSort. I will look into it and fix it. Right now I have some other problems that I am working on with this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 5
    Last Post: 11-19-2002, 09:36 PM