Thread: Array issue

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Question Array issue

    So basically I need to make an array (hlp) which would include the m amount of highest numbers out of a scanned array (pole).
    My code should be correct but doesn't work (it ignores the first numbers in the original array)
    Please help me find the issue
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
      int i, m, n, x, j, l, o;
      int k, r, h, z;
      int pole[n];
      int hlp[m];
      printf("Input m\n");
      scanf("%d", &m);
      printf("Input n\n");
      scanf("%d", &n);
      
      while(m>n||m>200||m<1||n<1)
    {
           printf("Error\n");
           printf("Input m\n");
           scanf("%d", &m);
           printf("Input n\n");
           scanf("%d", &n);
    }
    for(r=0; r<m;r++)
    {
    	hlp[r]=0;
    }
    
    printf("Input array\n");
      for(i=0; i<n; i++)
      {
               scanf("%d", &o);
               pole[i]=o;
    }
    
    
      for(i=0; i<n; i++)
      {
      	j=0;
      	while(pole[i]<hlp[j] && j<m)
      	{
      		j=j+1;
    	  }
    	if(pole[i]>hlp[j])
    {
    		for(l=m; l>j; l--)
    		{	
    	     hlp[l]=hlp[l-1];
    	    }  
    }
    hlp[j]=pole[i]
    }
    
    
    for(i=0; i<m; i++)
    {
             printf("%d, ", hlp[i]);
    }
    	
    	return 0;
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
      int pole[n];
      int hlp[m];
      printf("Input m\n");
      scanf("%d", &m);
      printf("Input n\n");
      scanf("%d", &n);
    The first problem is that C doesn't predict the future, nor does it automatically resize arrays depending on the new values you assign to m and n.

    You also need to work on your (lack) of indentation.
    Indent style - Wikipedia, the free encyclopedia
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array issue?
    By MarkP8822 in forum C Programming
    Replies: 1
    Last Post: 09-24-2016, 07:54 PM
  2. *need help in array issue*
    By RyanC in forum C Programming
    Replies: 1
    Last Post: 11-26-2015, 03:59 PM
  3. Array Average Issue
    By carpeltunnel in forum C Programming
    Replies: 28
    Last Post: 09-04-2012, 11:17 PM
  4. Array Issue.
    By mcertini in forum C Programming
    Replies: 7
    Last Post: 02-16-2011, 10:56 AM
  5. array issue
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 08:37 AM

Tags for this Thread