Thread: pls help me in this error...urgent

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    pls help me in this error...urgent

    L111 : invalid types for array subscript
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    typedef struct {
         int x;
    	 int y;
    	 }city;
    	 
    permute(city *a,int n,int t,int **d,int *b)
    { 
      int i,j,temp1,temp2;
      
      if(n==1)
       {
         for(j=0;j<t-1;j++)
    	   (*b) += d[j][j+1];
    	 (*b) += d[j][0];
    	 b++;
    	 }
    	 
      else
        {
         for(i=1;i<n;i++)
          {	
            temp1 = (a+i)->x;
    		temp2 = (a+i)->y;
            (a+i)->x = (a+(n-1))->x;
    		(a+i)->y = (a+(n-1))->y;
    	    (a+(n-1))->x = temp1;
    		(a+(n-1))->y = temp2;
    		
    	    permute(a,n-1,t,d,b);
    		
    	    temp1 = (a+i)->x;
    		temp2 = (a+i)->y;
            (a+i)->x = (a+(n-1))->x;
    		(a+i)->y = (a+(n-1))->y;
    	    (a+(n-1))->x = temp1;
    		(a+(n-1))->y = temp2;
          }  
       }
       
    }
    
    minimum(int *b, int n)
    { 
      int min;
      for(int i=n-2;i<=0;i++)
         if(*(b+i)<min)
    	   min = *(b+i);
      printf("Minimum cost for the trip is %d\n\n",min); 
    }
    
    int main()
    {
    int n;
    printf("How many cities you want to travel (include the starting position ,too, in your number)\n\n");
    scanf("%d",&n);
    city *a;
    a=(city *)malloc(n*sizeof(city));
    printf("\n\nAssume the x and y co-ordinate of the city from where you start(or end) the journey to be (0,0)\n\n");
    a->x=0;
    a->y=0;
    printf("Data stored for the city from where you start\n\n");
    printf("Now ,storing x and y co-ordinate of the other cities (random value)\n\n");
    printf("press enter to randomly generate the co-ordinates.....");
    srand((unsigned)time ( NULL ) );
    for(int i=1;i<n;i++)
    {
    (a+i)->x= 0 + rand()%1000;
    (a+i)->y= 0 + rand()%1000;
    }
    printf("\n\nData storing successful for other cities\n\n");
    printf("Now, sorting the cities with respect to their x co-ordinates\n\n");
    printf("press enter to sort the cities......");
    int temp1,temp2;
    for(int j=0;j<n-1;j++)
      for(int k=j+1;k<n;k++)
        {
    	  if((a+j)->x > (a+k)->x)
    	   {
    		 temp1 = (a+j)->x;
    		 temp2 = (a+j)->y;
    		 (a+j)->x = (a+k)->x;
    		 (a+j)->y = (a+k)->y;
    		 (a+k)->x = temp1;
    		 (a+k)->y = temp2;
    		}
    	}
    printf("\n\ncities with respect to their x co-ordinate is sorted\n\n");
    int d[n][n];
    printf("calculating the distance between the cities\n\n");
    printf("press enter to calculate and store.....");
    int m,l,g;
    for(l=0;l<n-1;l++)
      for(m=l;m<n;m++)
         d[l][m] = (int) sqrt(pow(((a+l)->x - (a+m)->x),2) + pow(((a+l)->y - (a+m)->y),2));
    for(l=1;l<n;l++)
      for(m=0;m<l;m++)
         d[l][m] = d[m][l];
    printf("\n\nDistances calculated and stored\n\n");
    int fac,min;
    fac = 1;
    for(g=1;g<=n-1;g++)
       fac *= g;
    fac +=1;
    int *b;
    b =(int *)malloc(fac*sizeof(int));
    permute[a,n,n,d,b];
    minimum[b,fac];  
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And on which line of all that code is the error occuring?

    Your compiler's error messages should contain a line number so you can go directly to the offending line....

    Also you should turn your compiler's error and warning levels up to maximum and whatever it prints out should be seen as a problem to be corrected.

    Yes... compiler errors are your friends...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > permute[a,n,n,d,b];
    > minimum[b,fac];
    If these are an attempt to call YOUR functions, perhaps you should look at how you call all the other functions, like printf.
    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.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You may find useful information about functions here - Lesson - 4 Functions

    Additionally:

    main() returns an int. You failed to do so. The basic C program is:
    Code:
    int main(void){
    
         return 0;
    }
    Read about main() here.

    Read about how to indent your code.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help urgent with error!!
    By Tiglet in forum C# Programming
    Replies: 5
    Last Post: 03-14-2010, 03:42 PM
  2. Replies: 1
    Last Post: 12-19-2008, 05:32 AM
  3. Replies: 2
    Last Post: 05-27-2007, 11:06 PM
  4. last line error, URGENT
    By deng17 in forum C++ Programming
    Replies: 7
    Last Post: 03-25-2007, 05:45 AM
  5. unknown error, urgent help needed
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2003, 04:09 AM