Finding lowest value

This is a discussion on Finding lowest value within the C Programming forums, part of the General Programming Boards category; hello guys and gals. I am taking a data file created by the program in another function which looks like ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    31

    Finding lowest value

    hello guys and gals.

    I am taking a data file created by the program in another function which looks like this:
    1 38 27
    7 24 23
    38 12 15
    23 14 9
    27 18 8
    11 9 29
    3 24 40
    34 44 2
    34 45 27
    46 3 25
    19 45 47
    26 25 37
    41 26 28
    2 16 44
    3 49 31
    26 25 42
    33 14 8
    1 5 11
    19 47 36
    4 4 7
    21 25 5
    34 19 14
    29 14 1
    46 45 21
    42 26 2
    26 23 35
    5 47 46
    33 3 12
    21 38 9
    35 38 16
    45 41 44
    38 7 32
    13 1 8
    3 34 34
    37 43 19
    17 31 19
    31 37 25
    38 36 8
    49 49 29
    18 44 42
    13 12 29
    49 16 47
    36 18 28
    37 26 8
    32 29 49
    4 42 21
    31 21 7
    44 42 28
    14 14 13
    22 21 24
    These are all x y and z components of aircraft which the program creates in an early function (yes I did get it to work). Then I used a another function which looks like
    Code:
     /*this is the function that will print to the distout.out file   */
        void FunctionDistout(FILE *Fplanes, FILE *Fdistout)
       {
          int aXaxis[50][3];
          int i = 0;
          int j = 0;
       	
       	
          for(i=0; i<50; i++)
          {
             for(j=0; j<3; j++)
             {
                fscanf(Fplanes, "%d, %d %d", &aXaxis[i][0], &aXaxis[i][1], &aXaxis[i][2]);
             }
          }
       		
          for(j=0; j<50; j++)
          {
             if(sqrt(aXaxis[j][0]*aXaxis[j][0]+aXaxis[j][1]*aXaxis[j][1]+aXaxis[j][2]*aXaxis[j][2]) < 55)
             {
                fprintf(Fdistout, "WARNING!!! Aircraft at %d %d %d is %3.2f KM away \n",
                   	aXaxis[j][0], aXaxis[j][1], aXaxis[j][2],
                   	sqrt(aXaxis[j][0]*aXaxis[j][0]+aXaxis[j][1]*aXaxis[j][1]+aXaxis[j][2]*aXaxis[j][2]));
             }
          }
       		
       		
       }
    this function calculates all the aircrafts distances and writes them to a file. This works fine. But I created another function which will print to the screan the closet aircraft. How do I do this? Here are two of the many attempts at it: What have i done wrong?:

    {
    int aXaxis[50][3];
    int i = 0;
    int j = 0;
    int l;
    float u = 90;


    for(i=0; i<50; i++)
    {
    for(j=0; j<3; j++)
    {
    fscanf(Fplanes, "%d, %d %d", &aXaxis[i][0], &aXaxis[i][1], &aXaxis[i][2]);
    }
    }

    if(u > sqrt(aXaxis[j][0]*aXaxis[j][0]+aXaxis[j][1]*aXaxis[j][1]+aXaxis[j][2]*aXaxis[j][2]))
    {
    for(l=0; l<50; l++)
    {
    u = (sqrt(aXaxis[l][0]*aXaxis[l][0]+aXaxis[l][1]*aXaxis[l][1]+aXaxis[l][2]*aXaxis[l][2]));
    }

    }

    printf("Nearest aircraft at %d %d %d: %f KM away.",
    aXaxis[l][0], aXaxis[l][1], aXaxis[l][2], u);

    }









    void FunctionPrintSound(FILE *Fplanes)
    {
    int aXaxis[50][3];
    int p = 0;
    int q = 0;
    int j = 0;
    float u = 90;

    for(p=0; p<50; p++)
    {
    fscanf(Fplanes, "%d, %d %d", &aXaxis[p][0], &aXaxis[p][1], &aXaxis[p][2]);
    for(q=0; q<3; q++)
    {
    fscanf(Fplanes, "%d, %d %d", &aXaxis[q][0], &aXaxis[q][1], &aXaxis[q][2]);
    }
    }
    for(j=0; j<50; j++)
    {
    if(u > sqrt(aXaxis[j][0]*aXaxis[j][0]+aXaxis[j][1]*aXaxis[j][1]+aXaxis[j][2]*aXaxis[j][2]))
    {
    u = (sqrt(aXaxis[j][0]*aXaxis[j][0]+aXaxis[j][1]*aXaxis[j][1]+aXaxis[j][2]*aXaxis[j][2]));
    {
    printf("Nearest aircraft at %d %d %d: %3.2f KM away.",
    aXaxis[j][0], aXaxis[j][1], aXaxis[j][2], u);
    }

    }

    }
    }
    Last edited by Salem; 11-26-2005 at 01:58 AM. Reason: Experimental change of QUOTE to CODE tags - now you fix the rest of the post yourself.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    btw Both of these functions work, but do not return the correct results

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sloopy
    btw Both of these functions work, but do not return the correct results
    They probably hurt themselves trying to read unindented code. On an aside, I don't consider 'do not return the correct results' to be code that 'works'.

    [edit]
    Eh...alright, but this is the last time I read your code until you learn how to indent.
    Code:
     for(i=0; i<50; i++)
    {
    for(j=0; j<3; j++)
    {
    fscanf(Fplanes, "%d, %d %d", &aXaxis[i][0], &aXaxis[i][1], &aXaxis[i][2]);
    }
    }
    Why do you have a loop that reads 3 lines, each of which read 3 items, inside a loop that executes 50 times. Do you have 150 lines in your file? Oh, also, stop using & there. You don't need to with arrays of basic types.
    [/edit]

    Quzah.
    Last edited by quzah; 11-25-2005 at 08:44 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Oh, also, stop using & there. You don't need to with arrays of basic types.
    The arrays make no difference, it's still %d in the conversion, so it still needs to be a pointer to an int.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    they were indented

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Indenting only works with code tags, not quote tags.
    I only fixed one set of tags, in the (vain) hope that you would click 'edit' and fix the others yourself.
    Oh well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. did i do this right?
    By Joldil in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2007, 03:43 AM
  3. Finding the lowest number of a certain input
    By theorbb in forum C Programming
    Replies: 26
    Last Post: 04-17-2006, 03:36 AM
  4. Finding lowest value
    By titleist_03 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2004, 08:11 PM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21