Thread: Please help me with this wind simulator in c programming

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    8

    Please help me with this wind simulator in c programming

    Simulator, Part 1Create a function, DoSimulation( ), that performs the following.The simulator will generate wind speeds every 10 seconds for the duration of an hour. The wind speeds will beprinted to a file, “winds.txt”. Include a 0.5% possibility of encountering a small wind storm at each step. When the storm is occurring, thewind speed should increase with a random value between 20 and 30 mph. Also, the duration of the stormshould last a random length between 3 and 5 minutes. This is what I have so far, but it won't print anything on my file. please help!! thanks

    Code:
    /* This program simulates wind speeds for a flight simulator and it also analyzes the data produced*/#include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    
    void simulator(void);
    void analysis(void);
    
    
    int main()
    {
        int choice;
        do
        {
             printf("Menu\n");
             printf("1. Simulator\n");
             printf("2. Analysis\n");
             printf("3. Exit\n");
             scanf("%d", &choice);
    
    
             switch(choice)
             {
                 case 1: simulator();
                     break;
                 case 2: analysis();
                     break;
                 case 3: printf("Quitting program!\n");
                     break;
                 default: printf("Invalid choice!\n");
                     break;
             }
        } while (choice !=3);
    }
    void simulator(void)
    {
        FILE *project;
        project=fopen("winds.txt","w");
        if(project==NULL){
                printf("Error!");
                return 0;}
    
    
        double average,mingust,maxgust,windspeed,totalWindspeed,minwind,maxwind,range,div;
        maxgust=10;
        int i;
        int Maxseconds=3600;
    
    
        printf("What's the average wind speed?\n");
        scanf("%lf",&average);
        printf("What's the expected gust?\n");
        scanf("%lf",&mingust);
        //check parameters
        if (average<0)
            printf("not possible\n");
        else if (average>30)
            printf("not possible\n");
        else if (mingust<0)
            printf("not possible\n");
        else if (mingust>10)
            printf("not possible\n");
        else if (mingust>average)
            printf("not possible\n");
        else
        {
            //check if storm is occurring or not
            int stormoccurence;
            //Storm occurrence can take a random value between 0 and 999
            stormoccurence = rand()%1000;
            //Ensure value is <5
            if (stormoccurence>5)
            {
                printf("No storm occured\n");
            }
            else
            {
                printf("Storm started\n");
                srand(time(NULL));
                Maxseconds=3*60+(rand()%121);
    
    
            for(i=0; i<=Maxseconds; i+=10)
            {
    
    
            minwind=average-mingust;
            maxwind=average+mingust;
            range=maxwind-minwind;
            div=RAND_MAX/range;
            windspeed=minwind+(rand()/div);
            windspeed=windspeed+20+rand()%11;
            totalWindspeed+=windspeed;
    
    
    
    
        printf("simulation was successful\n");
        fprintf(project,"%lf %f",average, (float)totalWindspeed/Maxseconds);
        fclose(project);
        }}}
    return 0;

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    Actually, it does print to the file, only with very low probability (or 0 probability, in the case where the pseudo random generator was not seeded)...
    The reason is obvious when the code is properly indented:

    Code:
    void simulator(void) {
    
        FILE *project;
        project=fopen("winds.txt","w");
    
        if(project==NULL){
            printf("Error!");
            return 0;
        }
    
        double average,mingust,maxgust,windspeed,totalWindspeed,minwind,maxwind,range,div;
        maxgust=10;
        int i;
        int Maxseconds=3600;
    
        printf("What's the average wind speed?\n");
        scanf("%lf",&average);
        printf("What's the expected gust?\n");
        scanf("%lf",&mingust);
        //check parameters
        if (average<0)
            printf("not possible\n");
        else if (average>30)
            printf("not possible\n");
        else if (mingust<0)
            printf("not possible\n");
        else if (mingust>10)
            printf("not possible\n");
        else if (mingust>average)
            printf("not possible\n");
        else {
    
            //check if storm is occurring or not
            int stormoccurence;
            //Storm occurrence can take a random value between 0 and 999
            stormoccurence = rand()%1000;
            //Ensure value is <5
            if (stormoccurence>5) {
                    printf("No storm occured\n");
            }
            else {
                printf("Storm started\n");
                srand(time(NULL));
                Maxseconds=3*60+(rand()%121);
        
                for(i=0; i<=Maxseconds; i+=10) {
        
                    minwind=average-mingust;
                    maxwind=average+mingust;
                    range=maxwind-minwind;
                    div=RAND_MAX/range;
                    windspeed=minwind+(rand()/div);
                    windspeed=windspeed+20+rand()%11;
                    totalWindspeed+=windspeed;
        
                    printf("simulation was successful\n"); /***/
                    fprintf(project,"%lf %f",average, (float)totalWindspeed/Maxseconds); /***/
                    fclose(project);  /***/
                }
            }
        }
    
        return 0;
    }
    Control will only reach the lines marked in /***/ when rand() will generate a number that is less than or equal to 5, which allegedly should happen with probability 6/1000, though in your case with probability 0, since you called srand() only after calling rand().
    I assume you want to print to the file regardless of whether or not the weather is bad (pun intended ), and so your intention was to take those three lines out of that 'else' clause:

    Code:
    /* This program simulates wind speeds for a flight simulator and it also analyzes the data produced*/#include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
     
     
    void simulator(void);
    void analysis(void);
     
     
    int main()
    {
        int choice;
        do
        {
             printf("Menu\n");
             printf("1. Simulator\n");
             printf("2. Analysis\n");
             printf("3. Exit\n");
             scanf("%d", &choice);
     
     
             switch(choice)
             {
                 case 1: simulator();
                     break;
                 case 2: //analysis();
                     break;
                 case 3: printf("Quitting program!\n");
                     break;
                 default: printf("Invalid choice!\n");
                     break;
             }
        } while (choice !=3);
    }
    
    
    void simulator(void) {
    
        FILE *project;
        project=fopen("winds.txt","w");
    
        if(project==NULL){
            printf("Error!");
            return 0;
        }
    
        double average,mingust,maxgust,windspeed,totalWindspeed,minwind,maxwind,range,div;
        maxgust=10;
        int i;
        int Maxseconds=3600;
    
        printf("What's the average wind speed?\n");
        scanf("%lf",&average);
        printf("What's the expected gust?\n");
        scanf("%lf",&mingust);
        //check parameters
        if (average<0)
            printf("not possible\n");
        else if (average>30)
            printf("not possible\n");
        else if (mingust<0)
            printf("not possible\n");
        else if (mingust>10)
            printf("not possible\n");
        else if (mingust>average)
            printf("not possible\n");
        else {
    
            //check if storm is occurring or not
            int stormoccurence;
            //Storm occurrence can take a random value between 0 and 999
            stormoccurence = rand()%1000;
            //Ensure value is <5
            if (stormoccurence>5) {
                    printf("No storm occured\n");
            }
            else {
                printf("Storm started\n");
                srand(time(NULL));
                Maxseconds=3*60+(rand()%121);
        
                for(i=0; i<=Maxseconds; i+=10) {
        
                    minwind=average-mingust;
                    maxwind=average+mingust;
                    range=maxwind-minwind;
                    div=RAND_MAX/range;
                    windspeed=minwind+(rand()/div);
                    windspeed=windspeed+20+rand()%11;
                    totalWindspeed+=windspeed;
        
                }
            }
    
            printf("simulation was successful\n");
            fprintf(project,"%lf %f",average, (float)totalWindspeed/Maxseconds);
            fclose(project);
        }
    
        return 0;
    }
    This does print to file.
    Furthermore, you should compile with -Wall flag to enable warnings, your code has some warnings you would not want to ignore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-01-2010, 11:32 AM
  2. c programming, MC68HC11 micro controller board simulator
    By fortune2k in forum C Programming
    Replies: 10
    Last Post: 03-11-2009, 07:04 AM
  3. Flight Simulator Wind Speed!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 12:40 AM
  4. Unbelievable wind speeds
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-09-2004, 03:42 PM
  5. files wind up on the desktop
    By spray14 in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 11:58 PM