for loop with sort issues

This is a discussion on for loop with sort issues within the C Programming forums, part of the General Programming Boards category; i can get teh data but an having issues with my sort any ideas please Code: /************************************************************ FILE NAME: Lab3 ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question for loop with sort issues

    i can get teh data but an having issues with my sort any ideas please
    Code:
     /************************************************************
    FILE NAME:    Lab3
    DESCRIPTION:  Calculate loan amortization
    DATE:         Oct 8, 2006 
    AUTHOR:       Cheryl B Jensen
    ASSIGNMENT:   Lab 2
    FUNCTIONS:    main, Linked_List
    /***************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    
    typedef struct listNode				/* struct definition for linked list */
    {
    	int data;
    	struct listNode *priorPtr;
    	struct listNode *nextPtr;
    }ListNode;
    
    struct listNode *start = NULL;		/* initialize first node */
    struct listNode *last = NULL;		/* initialize last node */
    
    /*int Linked_List (int, *ptr);  /*prototype for function */
    
    
    /*******************************************************************
    
    FUNCTION HEADER: Main
    FUNCTION:     Main
    DESCRIPTION:  Ask for user input from keyboard up to 50 integers,
    				store the data in an array, sort the array, create a linked 
    				list, and display the data from the linked list
    INPUT:
      PARAMETERS:  input up to 50 integers (Max) - use Count to determine 
      				actual size of array
      KEYBOARD:    Read user's input listed above
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Display linked list of the sorted array in ascending order
      PARAMETERS:   1 to Count (size of array)
      DISPLAY:      display contents of array within the linked list 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     Linked_List
    *******************************************************************/
    
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    
    
    
    #define Max 50
    int main (void)
    {
    
    		/* max size of array input; array will end when it reaches
    						max or when user quits by hitting enter (null entry) */
    int ArrayInput[Max]; /* declared, defined, & initialized array */
    int Count = 0;		  /* actual size of array, defined by user or Max */
    int InputNum;		/* int entered by user */	
    int Index = 0;		/* location of current array cell in first for statement*/
    int Index2 = 0;		/* location of current array cell in 2nd for statement */
    int hold = 0;		/* temporary holding location for swap */
    int Pass = 0;		/* loop counter */
    char More;		/* to continue or end loop by user input of null entry */
    struct listNode *start = NULL;  /* initialize start pointer structure */
    struct listNode *last = NULL;	/* initialize last pointer structure */
    struct listNode *data = NULL;	/* initialize data pointer structure */ 
    struct listNode *tempnode = NULL;	/* initialize temporary pointer */
    
    
    
    	printf ("Welcome to your Sort program.\n");
    	printf ("This program will sort up to 50 integers in ascending order.\n");
    		
    	do {
    			printf("\nEnter an integer:  ", stdout);
    			scanf ("\n%d", &ArrayInput[Index]); 
    				while (ArrayInput[Index] <= 0) 
    				{
    					printf ("Invalid entry. Try again:\n\n");
    					scanf ("\n%d", &ArrayInput[Index]);
    				} /* end of if statement */
    			
    									
    				printf ("Do another? Y or N?\n");
    				scanf ("\n%c", &More); 
    			} /* end of the body of do..while statement */
    			
    			while (((More == 'Y') || (More == 'y')) && (Index < Max));	
    		
            Count = Index;
    		printf ("%d\t",  ArrayInput[Index]);
    		
    		for (Pass = 1; Pass < Count; Pass++) {
                 printf("in doloop");
    			for (Index2 = 1; Index2 , Count - 1; Index2++) {
    				if (ArrayInput[Index2] > ArrayInput[Index2 + 1]) {
    					hold = ArrayInput[Index2];
    					ArrayInput[Index2] = ArrayInput[Index2 +1];
    					ArrayInput[Index2 +1] = hold;
    					printf ("%d\t",  ArrayInput[Index2]);
    				} /* end of if */
    			} /* end of inner for */
    			data = (struct ListNode *) malloc(sizeof(ListNode));
    			if (!data)
    				return -1;
    				data->data= ArrayInput[Index];
    				Linked_List (data, &start, &last);
    		} /* end of outer for */	
    	
    		tempnode = start;
    		printf ("Your data displayed in ascending order:\n\n");
    		while (tempnode) {
    			printf ("&d\n",tempnode->data);
    			tempnode = tempnode->nextPtr;
    		} /* end of while statement */	
    		
    
         	system ("PAUSE");
        	return 0;
        }  /* end of main function */
    
    
    
    /*****************************************************************************
    FUNCTION HEADER: Linked_List
    FUNCTION:     Linked_Array
    DESCRIPTION:  Create a linked list from the sorted array
    INPUT:
      PARAMETERS:  sorted array
      KEYBOARD:    no keyboard input
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Return linked list
      PARAMETERS:   size of actual sorted array
      DISPLAY:      no display yet 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     no functions; called by main
    *******************************************************************/
    int Linked_List ( struct listNode 	*newnode,
    									struct listNode **start, 
    									struct listNode **last
    									)									
    	
    {
    	struct listNode *oldptr, *ptr;
    	if (*last == NULL)
          {
    			newnode->nextPtr = NULL;
    			newnode->priorPtr = NULL;
    			*last = newnode;
    			*start = newnode;
    			return;
    	}
    	ptr = *start;
    	oldptr = NULL;
    	while (ptr)
    	{
    			if(ptr->data < newnode->data) {
    				oldptr = ptr;
    				ptr = ptr->nextPtr;
    			}
    			else
    			{
    				if (ptr->priorPtr) {
    					ptr->priorPtr->nextPtr = newnode;
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = ptr->priorPtr;
    					ptr->priorPtr = newnode;
    					return;
    				}
    							
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = NULL;
    					ptr->priorPtr = newnode;
    					*start = newnode;
    					return;
    					
    			} /* end of else statement */
    			
    	} /* end of while statement */
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    
    	} /* end of function */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Clean it up first?
    main.c:8:1: warning: "/*" within comment
    main.c:24:33: warning: "/*" within comment
    main.c: In function `main':
    main.c:80: warning: too many arguments for format
    main.c:101: warning: left-hand operand of comma expression has no effect
    main.c:111: warning: assignment from incompatible pointer type
    main.c:115: warning: implicit declaration of function `Linked_List'
    main.c:122: warning: too many arguments for format
    main.c:62: warning: unused variable `InputNum'
    main.c: In function `Linked_List':
    main.c:161: warning: `return' with no value, in function returning non-void
    main.c:180: warning: `return' with no value, in function returning non-void
    main.c:187: warning: `return' with no value, in function returning non-void
    main.c:197: warning: control reaches end of non-void function
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Hmmm, in cases of trying to develop a "long" program, being a non-expert, i would generally subdivide tasks into different small programs to see if they would run, or to determine errors. By using that scheme, it saves me the trouble of having to find and debug all the combined errors all at once.

    for example:

    see if after inputting the integers, your array actually holds all the correct numbers.
    see if your linked list operations work after each operation.
    i would generally test each loop too if i were you.

    note: this advice is for non-experts like me only, as experts wouldnt have many errors in their program.
    It is not who I am inside but what I do that defines me.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    I've given up helping this person because the code formatting is so resolutely bad that it just isn't worth the trouble trying to help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    thanks for all of you help. Im still very new at this and am trying to do it small and debug as I go. I want to mtell everyone thanks. You have helped me out aa lot And I know that I still have a very far way to go. Thanks for all of your input and help.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Presumably
    Code:
    printf("\nEnter an integer:  ", stdout);
    Is supposed to be
    Code:
    printf("\nEnter an integer:  ");

    Code:
        do {
            printf("\nEnter an integer:  ", stdout);
            scanf ("\n%d", &ArrayInput[Index]);
            while (ArrayInput[Index] <= 0)
            {
                printf ("Invalid entry. Try again:\n\n");
                scanf ("\n%d", &ArrayInput[Index]);
             } /* end of if statement */
    
            printf ("Do another? Y or N?\n");
            scanf ("\n%c", &More);
            }  while (((More == 'Y') || (More == 'y')) && (Index < Max));
    Apart from being a truly dire interface, you aren't changing the value of "Index", so you are just overwriting the first element in the array each time.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    I changed to code but all that i can go though it till i say no and then it shows a bunch of number and then closes right away
    Code:
      /************************************************************
    FILE NAME:    Lab3
    DESCRIPTION:  Calculate loan amortization
    DATE:         Oct 8, 2006 
    AUTHOR:       Cheryl B Jensen
    ASSIGNMENT:   Lab 2
    FUNCTIONS:    main, Linked_List
    /***************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    
    typedef struct listNode				/* struct definition for linked list */
    {
    	int data;
    	struct listNode *priorPtr;
    	struct listNode *nextPtr;
    }ListNode;
    
    struct listNode *start = NULL;		/* initialize first node */
    struct listNode *last = NULL;		/* initialize last node */
    
    /*int Linked_List (int, *ptr);  /*prototype for function */
    
    
    /*******************************************************************
    
    FUNCTION HEADER: Main
    FUNCTION:     Main
    DESCRIPTION:  Ask for user input from keyboard up to 50 integers,
    				store the data in an array, sort the array, create a linked 
    				list, and display the data from the linked list
    INPUT:
      PARAMETERS:  input up to 50 integers (Max) - use Count to determine 
      				actual size of array
      KEYBOARD:    Read user's input listed above
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Display linked list of the sorted array in ascending order
      PARAMETERS:   1 to Count (size of array)
      DISPLAY:      display contents of array within the linked list 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     Linked_List
    *******************************************************************/
    
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    
    
    
    #define Max 50
    int main (void)
    {
    
    		/* max size of array input; array will end when it reaches
    						max or when user quits by hitting enter (null entry) */
    int ArrayInput[Max]; /* declared, defined, & initialized array */
    int Count = 0;		  /* actual size of array, defined by user or Max */
    int InputNum;		/* int entered by user */	
    int Index = 0;		/* location of current array cell in first for statement*/
    int Index2 = 0;		/* location of current array cell in 2nd for statement */
    int hold = 0;		/* temporary holding location for swap */
    int Pass = 0;		/* loop counter */
    char More;		/* to continue or end loop by user input of null entry */
    struct listNode *start = NULL;  /* initialize start pointer structure */
    struct listNode *last = NULL;	/* initialize last pointer structure */
    struct listNode *data = NULL;	/* initialize data pointer structure */ 
    struct listNode *tempnode = NULL;	/* initialize temporary pointer */
    
    
    
    	printf ("Welcome to your Sort program.\n");
    	printf ("This program will sort up to 50 integers in ascending order.\n");
    		
     do {
            printf("\nEnter an integer:  ");
            scanf ("\n%d", &ArrayInput[Index]);
            while (ArrayInput[Index] <= 0)
            {
                printf ("Invalid entry. Try again:\n\n");
                scanf ("\n%d", &ArrayInput[Index]);
             } /* end of if statement */
    
            printf ("Do another? Y or N?\n");
            scanf ("\n%c", &More);
            }  while (((More == 'Y') || (More == 'y')) && (Index++ < Max));	
    		
            Count = Index;
    		
    		for (Pass = 1; Pass < Count; Pass++) {
                 printf("in doloop");
    			for (Index2 = 1; Index2 , Count - 1; Index2++) {
    				if (ArrayInput[Index2] > ArrayInput[Index2 + 1]) {
    					hold = ArrayInput[Index2];
    					ArrayInput[Index2] = ArrayInput[Index2 +1];
    					ArrayInput[Index2 +1] = hold;
    					printf ("%d\t",  ArrayInput[Index2]);
    				} /* end of if */
    			} /* end of inner for */
    			data = (struct ListNode *) malloc(sizeof(ListNode));
    			if (!data)
    				return -1;
    				data->data= ArrayInput[Index];
    				Linked_List (data, &start, &last);
    		} /* end of outer for */	
    	
    		tempnode = start;
    		printf ("Your data displayed in ascending order:\n\n");
    		while (tempnode) {
    			printf ("&d\n",tempnode->data);
    			tempnode = tempnode->nextPtr;
    		} /* end of while statement */	
    		
    
         	
         	getchar ();
         	getchar ();
        	return 0;
        }  /* end of main function */
    
    
    
    /*****************************************************************************
    FUNCTION HEADER: Linked_List
    FUNCTION:     Linked_Array
    DESCRIPTION:  Create a linked list from the sorted array
    INPUT:
      PARAMETERS:  sorted array
      KEYBOARD:    no keyboard input
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Return linked list
      PARAMETERS:   size of actual sorted array
      DISPLAY:      no display yet 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     no functions; called by main
    *******************************************************************/
    int Linked_List ( struct listNode 	*newnode,
    									struct listNode **start, 
    									struct listNode **last
    									)									
    	
    {
    	struct listNode *oldptr, *ptr;
    	if (*last == NULL)
          {
    			newnode->nextPtr = NULL;
    			newnode->priorPtr = NULL;
    			*last = newnode;
    			*start = newnode;
    			return;
    	}
    	ptr = *start;
    	oldptr = NULL;
    	while (ptr)
    	{
    			if(ptr->data < newnode->data) {
    				oldptr = ptr;
    				ptr = ptr->nextPtr;
    			}
    			else
    			{
    				if (ptr->priorPtr) {
    					ptr->priorPtr->nextPtr = newnode;
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = ptr->priorPtr;
    					ptr->priorPtr = newnode;
    					return;
    				}
    							
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = NULL;
    					ptr->priorPtr = newnode;
    					*start = newnode;
    					return;
    					
    			} /* end of else statement */
    			
    	} /* end of while statement */
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    
    	} /* end of function */

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    for (Index2 = 1; Index2 , Count - 1; Index2++)
    What exactly is that supposed to do? Does initialization/condition/increment not ring a bell?

    In Linked_List (which isn't declared) you are using
    Code:
    return;
    When the function is supposed to return an int.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i should that the number that is in the array and sort it ArrayInput[Index]. so i need to take out my returns in the Linked list

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    In English?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i can enter in the data. But the do while statment is not working. I'm still not able to put the data into the array. Any ideas. what I do is enter in the data. And the Index stays the same ever changes.
    Code:
     /************************************************************
    FILE NAME:    Lab3
    DESCRIPTION:  Calculate loan amortization
    DATE:         Oct 8, 2006 
    AUTHOR:       Cheryl B Jensen
    ASSIGNMENT:   Lab 2
    FUNCTIONS:    main, Linked_List
    /***************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    
    typedef struct listNode				/* struct definition for linked list */
    {
    	int data;
    	struct listNode *priorPtr;
    	struct listNode *nextPtr;
    }ListNode;
    
    struct listNode *start = NULL;		/* initialize first node */
    struct listNode *last = NULL;		/* initialize last node */
    
    /*int Linked_List (int, *ptr);  /*prototype for function */
    
    
    /*******************************************************************
    
    FUNCTION HEADER: Main
    FUNCTION:     Main
    DESCRIPTION:  Ask for user input from keyboard up to 50 integers,
    				store the data in an array, sort the array, create a linked 
    				list, and display the data from the linked list
    INPUT:
      PARAMETERS:  input up to 50 integers (Max) - use Count to determine 
      				actual size of array
      KEYBOARD:    Read user's input listed above
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Display linked list of the sorted array in ascending order
      PARAMETERS:   1 to Count (size of array)
      DISPLAY:      display contents of array within the linked list 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     Linked_List
    *******************************************************************/
    
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    
    
    #define Max 50	
    int main (void)
    {
    		/* max size of array input; array will end when it reaches
    						max or when user quits by hitting enter (null entry) */
    int ArrayInput[Max]; /* declared, defined, & initialized array */
    int Count = 0;		  /* actual size of array, defined by user or Max */
    int InputNum;			/* int entered by user */	
    int Index = 0;		/* location of current array cell in first for statement*/
    int Index2 = 0;		/* location of current array cell in 2nd for statement */
    int hold = 0;		/* temporary holding location for swap */
    int Pass = 0;		/* loop counter */
    char More;		/* to continue or end loop by user input of null entry */
    struct listNode *start = NULL;  /* initialize start pointer structure */
    struct listNode *last = NULL;	/* initialize last pointer structure */
    struct listNode *data = NULL;	/* initialize data pointer structure */ 
    struct listNode *tempnode = NULL;	/* initialize temporary pointer */
    
    
    
    	printf ("Welcome to your Sort program.\n");
    	printf ("This program will sort up to 50 integers in ascending order.\n");
    	printf ("Ready to start program? Y or N\n");
    	scanf ("\n%c", &More);	
    		while (((More == 'Y') || (More == 'y')) && (Index < Max))	
    			{
    			printf ("\nEnter an integer:  ");
    			scanf ("\n%d", &ArrayInput[Index]); 
    			
    				if (ArrayInput[Index] <= 0)
    				{
    					printf ("\nInvalid entry. Try again:\n\n");
    					scanf ("\n%d", &ArrayInput[Index]);
    				} /* end of if statement */
    				else
    				{
    				Index++;
    				printf ("\nDo another? Y or N?\n");
    				scanf ("\n%c", &More); 	
    				} /* end of else statement */
    			} /* end of while statement */
    			
    		printf ("%d", ArrayInput[Index]);		
    		Count = Index;			
    		
    		for (Pass = 1; Pass < Count; Pass++) {
    			for (Index = 0; Index, Count - 1; Index++) {
    				if (ArrayInput[Index] > ArrayInput[Index + 1]) {
    					hold = ArrayInput[Index];
    					ArrayInput[Index] = ArrayInput[Index +1];
    					ArrayInput[Index +1] = hold;
    				} /* end of if */
    			} /* end of inner for */
    			data = (struct ListNode *) malloc(sizeof(ListNode));
    			if (!data)
    				return -1;
    				data->data= ArrayInput[Index];
    				Linked_List (data, &start, &last);
    		} /* end of outer for */
    		
    		/*	Index++;
    			printf ("\nDo another? Y or N?\n");
    			scanf ("\n%c", &More); 	
    			} /* end of while statement */
    			
    		tempnode = start;
    		printf ("Your data displayed in ascending order:  \n\n");
    		while (tempnode) {
    			printf ("\n%d\n",tempnode->data);
    			tempnode = tempnode->nextPtr;
    		} /* end of while statement */	
    		
    
         	system ("PAUSE");
         	getchar ();
         	getchar ();
        	return 0;
        }  /* end of main function */
    
    
    
    /*****************************************************************************
    FUNCTION HEADER: Linked_List
    FUNCTION:     Linked_Array
    DESCRIPTION:  Create a linked list from the sorted array
    INPUT:
      PARAMETERS:  sorted array
      KEYBOARD:    no keyboard input
      FILE:        No input file for this program
    
    OUTPUT:
      RETURN VALUE: Return linked list
      PARAMETERS:   size of actual sorted array
      DISPLAY:      no display yet 
      FILE:         No DATA WRITTEN TO FILE
      CALLS TO:     no functions; called by main
    *******************************************************************/
    int Linked_List ( struct listNode 	*newnode,
    									struct listNode **start, 
    									struct listNode **last
    									)									
    	
    {
    	struct listNode *oldptr, *ptr;
    	if (*last == NULL)
          {
    			newnode->nextPtr = NULL;
    			newnode->priorPtr = NULL;
    			*last = newnode;
    			*start = newnode;
    			return;
    	}
    	ptr = *start;
    	oldptr = NULL;
    	while (ptr)
    	{
    			if(ptr->data < newnode->data) {
    				oldptr = ptr;
    				ptr = ptr->nextPtr;
    			}
    			else
    			{
    				if (ptr->priorPtr) {
    					ptr->priorPtr->nextPtr = newnode;
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = ptr->priorPtr;
    					ptr->priorPtr = newnode;
    					return;
    				}
    							
    					newnode->nextPtr = ptr;
    					newnode->priorPtr = NULL;
    					ptr->priorPtr = newnode;
    					*start = newnode;
    					return;
    					
    			} /* end of else statement */
    			
    	} /* end of while statement */
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    
    	} /* end of function */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21