Thread: Help Me Pls...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    Unhappy Help Me Pls...

    Write a program that generates a binary file consisting of aircraft_position_t structures containing four components:an
    aircraft ID(a random five char like LF122 ,KR 256,LJ562. first two must be a char between A to Z, the last three must be a number)
    and random get a distance x,y,z for the aircraft, x and y should be between -100 to +100 and value of z should be between -3.0 to +3.0. Created ten structures to represent ten nerby aircraff.

    Then write a second program that reads the binary file create by your first program and calculate the distance by

    sprt.root(x^2+y^2+z^2)

    for each of these aircraft. Write to a text file the(x,y,z)positions of the nearest two aircraft. for each aircraft less that 55km from another aircraft. write an alarm message to the screen(not the file)


    OR

    Teach me how to get use rand() funtion to get a ramdom char from A to Z
    AND how to use rand() funtion to get a number between -10 to 10


    PLS HELP ME SLOVE THIS PROBLEM...WRITE BY C PROGRAMMING

  2. #2
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    can some one help me correct this program;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct DateType 
    {
           int Day;
           int x;
           int y;
           int z;
           
    };
    
    int ReadDate(struct DateType x)
    {
        int i;srand((unsigned int)time(NULL));
        for(i =0; i< 40;i++)
        {
        
        x.x = GetRandom(-100,100);
        printf("haha=&#37;d\n",x.x);
    }
    }    
    
    int GetRandom(int min,int max)
    {
    	/* Returns a value between 0 and Max-1 */
    	return (rand()%min-rand()%max);
    	
    }
    int main(void)
    {
    	
        struct DateType   x;
         ReadDate(x);
         getch();
         return 0;
    	
    }
    i use the GetRandom funtion to get the random number between -100 to 100;
    but when the program get two random number is the same one, then it will gv me the result of 0, and it will not gv any result of 100 and -100.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you should reconsider your math there.

    Something like this:
    Code:
       int x;
       x = rand()%(max-min)+min;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    thx guy you help ma alot, but how can i used rand() in double value between 9.0 to -9.0...help me check this code

    Code:
    double ReadDate(struct DateType x)
    {
        //int arrayX[num],arrayY[num],arrayZ[num],
        int i;
       srand((double)time(NULL));
       
       for(i=0;i<num;i++){
        x.y[i] = GetRandom(-100.0,100.0);
        x.x[i] = GetRandom(-100.0,100.0);
        x.z[i] = GetRandom(-50.0,50.0);
    }
        for(i=0;i<num;i++){
        printf("haha=%lf  #%d\n",x.x[i],i+1);
        printf("haha=%lf  #%d\n",x.x[num-i],i+1);
        printf("haha=%lf  #%d\n",x.z[i],i+1);
    }
    }
    
    double GetRandom(double min,double max)
    {
    	double x;
                    x= rand()/(max-min)+min;/* Returns a value between 0 and Max-1 */
    	return x;
    	
    }
    when i compiler the compiler will say conflicting types for 'GetRandom', and previous implicit declaration of 'GetRandom' was here....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, first of all, if you want to get a 0..1.0 value from random, you should divide by RAND_MAX. Then multiply by whatever range (max - min) you want, and add your min value. [Note that max - min when min is negative works fine, it becomes max + abs(min).

    Second, you need to declare your function before you use it. Either put the whole function before the first call to the function, or put a forward declaration (aka prototype) somewhere before the first call. Otherwise, the compiler will "guess" the types of your function, and usually get it wrong, and when it finds the actual function, tell you that it mismatches - because it wasn't what the compiler guessed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    sry, i no really sure what u say...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
        int a;
        int b;
         srand((unsigned int)time(NULL));
        a = rand()&#37;100;
        b = rand()/100;
        printf("%d.%d",b,a);
        getch();
        return 0;
    }
    izzi only got this plan to get a double value that behind the decimal point is no only 0, like 1.23
    23.45, 65.67 but not 123.000, 234.000,545.000.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so you need to make the number a double first (and use "double" variables of course).

    Something like this
    Code:
    double GetRandom(double min, double max)
    {
        return (rand() / (double)RAND_MAX) * (max - min) + min;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    hi guy i try it ald but... i still cant...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
        int a;
        int b;
        double c;
         srand((unsigned int)time(NULL));
        a = rand()&#37;100;
        b = rand()/100;
        c = GetRandom(100.0,-100.0);
        printf("%f",c);
        getch();
        return 0;
    }    
    
    double GetRandom(double min, double max)
    {
        return (rand() / (double)RAND_MAX) * (max - min) + min;
    }
    when i compiler the compiler will say
    error conflicting types for 'GetRandom'
    error previous implicit declaration of 'GetRandom' was here....

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    sry guy i forgot to deca the function....lolx...thx alot

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >izzi only got this plan to get a double value that behind the decimal point is no only 0, like 1.23
    23.45, 65.67 but not 123.000, 234.000,545.000.

    Then get rand a [0, 100] and rand b [0, 10)

    Also please stop writing in pigeon English.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    yes i already complete...but how do i print my answer in to a binary file by "wb" commen?

    this is the code;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define num 10
    
    double GetRandom(double min, double max)
    {
        return (rand() / (double)RAND_MAX) * (max - min) + min;
    }
    
    int GetChar(int iMin, int iMax)
    {return rand() &#37; (iMax - iMin + 1) + iMin;
    } 
    
    struct DateType 
    {
           int aircraft_ID[num];
           double x[num];
           double y[num];
           double z[num];
           
    };
    
    int ReadDate(struct DateType x)
    {
        
        int i;
       srand((unsigned int)time(NULL));
       
       for(i=0;i<num;i++){
        x.y[i] = GetRandom(-100,100);
        x.x[i] = GetRandom(-100,100);
        x.z[i] = GetRandom(-50,50);
    }
    
        for(i=0;i<num;i++)
        {
        printf("%c%c%c%c%c, x = %f, y = %f,z = %f\n",GetChar(65,90),GetChar(65,90),GetChar(48,57),GetChar(48,57),GetChar(48,57),x.x[i],x.y[i],x.z[i]);
        }
    }
    
    int main(void)
    {
        struct DateType   x;
        ReadDate(x);
        getch();
        return 0;
        
    }
    Last edited by tatamilim; 11-03-2007 at 08:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM