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.
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 100int 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. } }
-
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.
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 100int 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. } }
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)
-
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.
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 100int 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. } }
In the second for-loop, you increase
cyc
in the if-statement. It's no longer necesary to also increase it in the loop:for(cyc = 0; cyc < num;){ 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. }
-
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.
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 100int 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. } }
Your while loop initializes the array
roll
with values between1
and10
. Note that there is no guarantee every value between1
and10
will actually be stored inroll
somewhere; for all you know it might be an array full of 1s or full of10
s! In your firstfor
loop you incrementloop
num
times, then accessroll[loop+1]
; on the last loop you will read past the end of the array, invalidating the comparison, so if the biggest value in your array happens to be stored in the last element, it may or may not be stored intemp
. In Debug this probably won't cause a problem, as likelyroll[100]
would just accessfin[0]
instead, and that would be initialized to 0. In Release mode however there is no guarantee that the arrayfin
will be initialized to 0 - in my experience it most likely will not. The intention of the first for loop appears to be to find the largest value inroll
and store it intemp
. Note that this value may be anything between1
and10
! The error you noticed, however, is due to what the others already pointed out above: you shouldn't increment cyc in the outer loop! What happens is this: the inner loop finds all items inroll
that correspond to thetemp
value, store it infin
, and increment (correctly) the indexcyc
. Once you're at the end of the loop, temp gets decremented, then cyc gets incremented again, causing the gap you noticed, then you go on searching for occurences of your now decreased value oftemp
. The reason for this confusion is that the outer loop is really all about temp, not cyc! The clearest way to write your last loop would be like this:cyc = 0;
for (int compare = temp; compare > 0; compare--) {
for (loop = 0; loop < LIMIT; loop++) {
if (roll[loop] == compare) {
fin[cyc] = compare;
cyc++;
}
}
}Note that I replaced num with LIMIT. This doesn't change anything in your code, but it is easier to read, as a reader unfamiliar with your code doesn't have to interpret all of it just to find out what value num would hold. Also it's less likely to accidentally mess up if you later change your code and possibly change the value of num. Now, while the above is clearer, it is also potentially less efficient; you used the c