Thread: Array Issue.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Array Issue.

    I put together a program that takes a list of numbers in an array and sorts them by copying out of the first array into a second array when the value in the first array matches the maximum array element in the first array. What I cannot figure out is why when I look at the array elements saved in the final array there is a skip in element #10 to #11. In other words the second array correctly saves nine 10's and then skips element #10 and then saves the 9's starting in element #11.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LIMIT 100
    
    int roll[100];
    int fin[100];
    int num = 0;
    
    
    int main(void)
    	{
    	int cyc = 0;
    	int top = 0;
    	int rotate = 0;
    	int seek = 0;
    	int loop = 0;
    	int temp = 0;
    		
    	while(num < LIMIT)
    		{
    		roll[num] = rand() % 10 + 1;
    		printf("%i",roll[num]);
    		printf("\n");
    		num++;
    		}
    	
    	for(cyc = 0; cyc < num; cyc++)
    		{
    		if(roll[loop] > roll[loop+1] && roll[loop] > temp)
    			{
    			temp = roll[loop];
    			}
    		loop++;
    		}
    	
    	for(cyc = 0; cyc < num; cyc++)
    		{
    		loop = 0;												//Zero loop for second number to be evaluated
    		while(loop < num)										//Go through entire list
    			{
    			if(roll[loop] == temp)								                        //Evaluate whether item matches maximum number
    				{																			
    				fin[cyc] = roll[loop];							                           //Copy item in the first array into the final array.
    				cyc++;											//Increment cycle by 1.
    				}
    			loop++;												//Increment loop to pass through entire list.
    			}
    		temp--;													//Decrement number to be evaluated.	
    		}
    	}

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    int main() should return a zero for normal operation, and an error code (int) for anything else (depending on the error).

    You should be receiving an error or warning for that.

    Your sort logic is using sequential for loops. Every sort algorithm I know has a *nested* pair of sort loops (or more), except Gnome sort.

    Aww, I see. You're using these for loops to do a merge sort. I haven't seen it done quite that way, before.
    Best way to solve it is to insert a print statement or assert for the bad array element, and step through it with the debugger. The instant it goes bad, you'll be right there to see what index number has gone awry.

    Your indentation is quite misleading - I STRONGLY recommend you change it to a more rational style. After awhile, your eye will begin to recognize problems with syntax and logic, if you use a good, consistent indentation style.

    What's true for your eye, is even more so for ours.
    Last edited by Adak; 02-16-2011 at 01:14 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Answer

    Adak,

    Thank you for your reply.

    Posted on different site:

    You are incrementing cyc unnecessarily in your for loop when you are copying.

    try this

    for(cyc = 0; cyc < num;)

    or a while loop

    cyc=0;
    while(cyc < num)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right here, the extra increment to cyc:

    Code:
    if(roll[loop] == temp)								                        //Evaluate whether item matches maximum number
    				{																			
    				fin[cyc] = roll[loop];							                           //Copy item in the first array into the final array.
    				cyc++;											//Increment cycle by 1.
    				}
    Yes, either increment it there, or let the loop do it, (depending on your logic), but not both.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Stop breaking my vertical scroll with your infernal code formatting. Thanks.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, like that's gonna work, msh. Can't even get this one to stop cross-posting.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For small number (say, less than 100) items, I'd avoid the more capable sorters, since they offer no advantage. They are slower, because they are more complex, and they are easier to goof up, also because they are more complex.

    For such small sorts, this simple substitution sort is faster than Quicksort or merge sort, and far easier to remember. It is always faster than bubblesort, yet easier to remember (imo). I suggest memorizing it, so you can use it any time, any place, as needed. This is how it looks for integers.

    Code:
    for(i = 0;i < SIZE-1;i++) {
      for(j = i + 1;j < SIZE; j++) {
        if(array[i] > array[j] {
          temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
      }
    }
    Insertion sort is even faster for small quantities, but a bit less intuitive. As mentioned before, qsort() is built into C, as well. It's not the most intuitive function to use, but it has real utility and good speed for sorting more items.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    Adak,

    Thank you for your reply. I am a rookie programmer. I will follow your direction by memorizing the algorithm you posted. After further review, I now see how the iteration in the code block advanced cyc and how the for statement advanced the cyc on the cycle after 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Char Array Issue
    By aussiemcgr in forum C Programming
    Replies: 5
    Last Post: 10-27-2010, 03:05 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM