Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array Issue.

Array Issue.

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelp
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    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 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.	
    	}
    }
    
    L M S 3 Replies Last reply
    0
    • M Mike Certini

      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 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.	
      	}
      }
      
      M Offline
      M Offline
      Mike Certini
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • M Mike Certini

        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 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.	
        	}
        }
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.	
        }
        
        1 Reply Last reply
        0
        • M Mike Certini

          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 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.	
          	}
          }
          
          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Your while loop initializes the array roll with values between 1 and 10. Note that there is no guarantee every value between 1 and 10 will actually be stored in roll somewhere; for all you know it might be an array full of 1s or full of 10s! In your first for loop you increment loop num times, then access roll[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 in temp. In Debug this probably won't cause a problem, as likely roll[100] would just access fin[0] instead, and that would be initialized to 0. In Release mode however there is no guarantee that the array fin 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 in roll and store it in temp. Note that this value may be anything between 1 and 10! 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 in roll that correspond to the temp value, store it in fin, and increment (correctly) the index cyc. 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 of temp. 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups