Thread: help needed with my programe

  1. #1
    supernormal2
    Guest

    help needed with my programe

    i wrote a programe that suppose to read 3 coloms and 20 row into an array and split them into 3 different arrays one array for taxi status(1=available 2=inuse,3=oncall) 2nd one for x-coordenat of taxi and the 3rd one for y-coordenat of the taxi the programe should determin the state of the taxi from taxistate array and if that is available taxi the programe should calculate the distance between the taxi location and (Xc,Yc) which is the location of the customer needing a taxi. the programe should write all the distance between the the cstomer and the available taxis into the array d_distance and write all the distance between the in use taxis and the customer into array called d_inuse and do the same form in call taxi

    ---------------
    the programe give no error when compiled but when i tried to print to the screen the array that contain the distance between customer and available taxis or even one element from that array my programe clashes and i got small window saying Access Violation and gives me choise of save details and close ????? i have never seen that windows before


    ------------------

    here is my code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define NROWS 20
    #define NCOLS 3 
    #define STATECOL 0
    #define XCOL 1
    #define YCOL 2
    
    main()
    {
    FILE *inputdata;
    int i,j,xc,yc,tc;
    int taxi_list[NROWS][NCOLS];  
    int taxistates[20],xtaxi[20],ytaxi[20];
    int d_available[20],d_inuse[20],d_oncall[20];
    
    
    // customer info.
    
    printf(" please note that u need to press enter after entering any inputs \n Enter your location as X and Y coordenate:\n");
    printf("Enter your X-coordenate \n X=");
    scanf("%d",&xc);
    printf("Enter your Y-coordenate \n Y=");
    scanf("%d",&yc);
    printf("after how many mins you need the taxi ");
    scanf("%d",&tc);   
    
    //open input file to read
    inputdata=fopen("taxis_info.txt","r");
    if (inputdata==NULL){
    printf("error canot open taxis_info \n");
    exit(1);
    }   
    //read all info from input file to one array
      for (i=0;i<NROWS;i++){
            for(j=0;j<NCOLS;j++){
              fscanf(inputdata,"%d",&taxi_list[i][j]);
              }
            }
            
              
     //  creat 3 different arrays from the previous 20x3 array 
     //for taxistates,x-coord, y-coord of the taxis 
     
    for ( i = 0 ; i < NROWS ; i++ ) {
      taxistates[i] = taxi_list[i][STATECOL];
      xtaxi[i] = taxi_list[i][XCOL];
      ytaxi[i] = taxi_list[i][YCOL];
     } 
    //craet 3 arrays to hold distance from customer to available,inuse and oncall taxis
    //initialise the three arrays with zeros
    for(i=0;i<NROWS;i++){
         d_available[i]=0;
         d_inuse[i]=0 ;
         d_oncall[i]=0  ;
        
         }      
         
     //check taxistates f (ie available,inuse,oncall or not available taxis)
     //calculate distance between customer and  each available,inuse and oncall taxi 
     //place each set of distances in an array 
     
    for (i=0;i<NROWS;i++){
      if (taxistates[i]==1)
            d_available[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2);
            
      else if (taxistates[i]==2)
            d_inuse[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2) ;
      
      else if   (taxistates[i]==3)
            d_oncall[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2);
            }  
            
    //testing element in the array 
    
    printf("%d",d_available[5]);
       
            
              }
    -----------------
    here is my input file
    1 0 1
    1 0 2
    1 1 0
    1 2 0
    1 1 1
    1 1 2
    1 2 1
    2 2 2
    2 1 3
    2 3 1
    2 3 0
    2 3 2
    2 2 3
    3 4 1
    3 4 2
    3 4 5
    0 3 3
    0 3 3
    0 3 3
    could any1 plz help me as the diedline for the assignment in 2 days

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int d_available[20],d_inuse[20],d_oncall[20];

    You may need to declare these as floats or doubles:
    double d_available[20],d_inuse[20],d_oncall[20];
    Code:
    if (taxistates[i]==1)
       d_available[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2);
    
    else if (taxistates[i]==2)
       d_inuse[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2) ;
    
    else if (taxistates[i]==3)
       d_oncall[i]=sqrt((xc-xtaxi[i])^2+(yc-ytaxi[i])^2);
    This should be:
    Code:
    if (taxistates[i]==1)
       d_available[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));
    
    else if (taxistates[i]==2)
       d_inuse[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2)) ;
    
    else if (taxistates[i]==3)
       d_oncall[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));

  3. #3
    supernormal2
    Guest
    thanx for ur help swoopy

    my programme is working now but gives rong answers and some time negative answers which is impossiple because of squared number added to squared number must be positive ?????

    any idea what is wrong in my code

    thanks

  4. #4
    supernormal2
    Guest
    my code looks like this now

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define NROWS 20
    #define NCOLS 3 
    #define STATECOL 0
    #define XCOL 1
    #define YCOL 2
    
    main()
    {
    FILE *inputdata;
    int i,j,xc,yc,tc;
    int taxi_list[NROWS][NCOLS];  
    int taxistates[NROWS],xtaxi[NROWS],ytaxi[NROWS];
    double d_available[NROWS],d_inuse[NROWS],d_oncall[NROWS];
    
    
    // customer info.
    
    printf(" please note that u need to press enter after entering any inputs \n Enter your location as X and Y coordenate:\n");
    printf("Enter your X-coordenate \n X=");
    scanf("%d",&xc);
    printf("Enter your Y-coordenate \n Y=");
    scanf("%d",&yc);
    printf("after how many mins you need the taxi ");
    scanf("%d",&tc);   
    
    //open input file to read
    inputdata=fopen("taxis_info.txt","r");
    if (inputdata==NULL){
    printf("error canot open taxis_info \n");
    exit(1);
    }   
    //read all info from input file to one array
      for (i=0;i<NROWS;i++){
            for(j=0;j<NCOLS;j++){
              fscanf(inputdata,"%d",&taxi_list[i][j]);
              }
            }
            
              
     //  creat 3 different arrays from the previous 20x3 array 
     //for taxistates,x-coord, y-coord of the taxis 
     
    for ( i = 0 ; i < NROWS ; i++ ) {
      taxistates[i] = taxi_list[i][STATECOL];
      xtaxi[i] = taxi_list[i][XCOL];
      ytaxi[i] = taxi_list[i][YCOL];
     } 
    //craet 3 arrays to hold distance from customer to available,inuse and oncall taxis
    //initialise the three arrays with zeros
    for(i=0;i<NROWS;i++){
         d_available[i]=0;
         d_inuse[i]=0 ;
         d_oncall[i]=0  ;
        
         }      
         
     //check taxistates f (ie available,inuse,oncall or not available taxis)
     //calculate distance between customer and  each available,inuse and oncall taxi 
     //place each set of distances in an array 
     
    for (i=0;i<NROWS;i++){
    if (taxistates[i]==1)
       d_available[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));
    
    else if (taxistates[i]==2)
       d_inuse[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2)) ;
    
    else if (taxistates[i]==3)
       d_oncall[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));
     
            }  
            
    //testing distance calcultaion
     
    printf("%d",d_available[5]);
    printf("\n");
        
            
              }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Make sure when you print doubles, you use %f or %lf. For example:
    Code:
    for (i=0;i<NROWS;i++){
    if (taxistates[i]==1)
       printf("available:%f\n",d_available[i]);
    
    else if (taxistates[i]==2)
       printf("inuse: %f\n",d_inuse[i]);
    
    else if (taxistates[i]==3)
       printf("oncall: %f\n",d_oncall[i]);
     
            }
    I ran your code with the above prints, and for the cases I tried, it printed positive numbers. Post back if this doesn't help, and what values you inputted for xc, yc, and tc.

    Also, I noticed the data file you posted only has 19 rows. Perhaps just a typo.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One other possibility is you may have to cast to double before calling the pow() function:
    Code:
       d_available[i]=sqrt(pow( (double) xc-xtaxi[i],2)+pow( (double) yc-ytaxi[i],2));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. function argument not needed
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 05:34 PM
  4. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  5. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM