-
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.
}
}
-
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.
-
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)
-
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.
-
Stop breaking my vertical scroll with your infernal code formatting. Thanks.
-
Yeah, like that's gonna work, msh. Can't even get this one to stop cross-posting.
-
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.
-
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.