Thread: help with basic program

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    help with basic program

    Once again, I am asking everybody to help me out with my simulation. And once again, I will fill everybody in if someone hasn't looked over my previous posts. I am making a simulation on an ecosystem, with 3 animals. Each animal has a ?/? chance of being created, and a ?/? of being destroyed. Here is the code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
        float numerator;
        float denominator;
        int interval;
        int created = 0;
        int total = 0;
        int element;
        int end;
        int duration;
        int sample;
        FILE *fp;
        
        srand (time(0));  
        
        printf("Welcome to Eco-Sim!\n");
        printf("Version 1.00\n");
        printf("By Jack Olszewski\n");
        printf("\n");
        
        printf ("Time (1 = 1 Year):");
        scanf ("%d",&end);    
        printf("\n\n");
        printf("Plant:\n");
        printf("\n");
        printf ("Enter the Numerator for Creation: ");
        scanf ("%f", &numerator);
        
        printf ("Enter the Denominator for Creation: ");
        scanf ("%f", &denominator);
        
        printf ("\nProbability Of Creation Is: %f\n", (numerator/denominator)/2);
    
        
        interval = (int)((numerator/denominator)/2*RAND_MAX);
        
        if ((fp = fopen ("plantcreation.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
              fprintf(fp,"Plant Creation Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Created\n\n");
                 created++;   
              }
                 
              else
                 fprintf (fp, "Not Created\n\n");
              
              total++;        
        }            
        printf("\n");
        printf ("Enter the Numerator for Destruction: ");
        scanf ("%f", &numerator);
        
        printf ("Enter the Denominator for Destruction: ");
        scanf ("%f", &denominator);
        
        printf ("\nProbability Of Destruction Is: %f\n\n", (numerator/denominator)/2);
     
        
        interval = (int)((numerator/denominator)/2*RAND_MAX);
        
        if ((fp = fopen ("plantdestruction.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
              fprintf (fp,"Plant Destruction Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Destroyed\n\n");
                 created++;   
              }
                 
              else
                 fprintf (fp, "Not Destroyed\n\n");
              
              total++;        
        }               
        printf("Herbivore:");
        printf("\n\n");
        printf ("Enter the Numerator for Creation: ");
        scanf ("%f", &numerator);
        
        printf ("Enter the Denominator for Creation: ");
        scanf ("%f", &denominator);
        
        printf ("\nProbability Of Creation Is: %f\n\n", (numerator/denominator)/2);
    
      
        interval = (int)((numerator/denominator)/2*RAND_MAX);
        
        if ((fp = fopen ("herbivorecreation.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
               fprintf (fp,"Herbivore Creation Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Created\n\n");
                 created++;   
              }
                 
              else
                 fprintf (fp, "Not Created\n\n");
              
              total++;        
        }            
        printf ("Enter the Numerator for Destruction: ");
        scanf ("%f", &numerator);
        
        printf ("Enter the Denominator for Destruction: ");
        scanf ("%f", &denominator);
        
        printf ("\nProbability Of Destruction Is: %f\n\n", (numerator/denominator)/2);
    
        
        interval = (int)((numerator/denominator)/2*RAND_MAX);
        
        if ((fp = fopen ("herbivoredestruction.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
              fprintf (fp,"Herbivore Destruction Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Destroyed\n\n");
                 created++;   
              }
                 
              else
                 fprintf (fp, "Not Destroyed\n\n");
              
              total++;        
        }
        printf("Carnivore:");
        printf("\n\n");
        printf("Enter the Numerator for Creation:");
        scanf("%f", &numerator);
        
        printf("Enter the Denominator for Creation:");
        scanf("%f", &denominator);
        
        printf("\nProbability Of Creation Is: %f\n\n", (numerator/denominator)/2);
    
    
        interval = (int)((numerator/denominator)/2*RAND_MAX);
        
        if ((fp = fopen ("carnivorecreation.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
               fprintf (fp,"Carnivore Creation Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Created\n\n");
                 created++;   
              }
                 
              else
                 fprintf (fp, "Not Created\n\n");
              
              total++;        
        }            
        printf ("Enter the Numerator for Destruction: ");
        scanf ("%f", &numerator);
        
        printf ("Enter the Denominator for Destruction: ");
        scanf ("%f", &denominator);
        
        printf ("\nProbability Of Destruction Is: %f\n\n", (numerator/denominator)/2);
    
        
        interval = (int)((numerator/denominator)/2         *RAND_MAX);
        
        if ((fp = fopen ("carnivoredestruction.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
    
        duration = end + time (0);
       while ( time (0) < duration )
        {
              sample = rand ();
              
              fprintf (fp,"Carnivore Destruction Log");
              fprintf (fp, "Day: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Destroyed\n\n");
                 created++;   
                 }
                 
              else
                 fprintf (fp, "Not Destroyed\n\n");
              
              total++;        
        }
    
    return 0;
    
    }
    Here is the big problem. At the minute, the values are being written to a log file. The project I'm doing involves taking the values, and converting them into graphs, and then to analyze them, etc. Imagine taking 29,000+ values, and in someway trying to count, or use Find to count the number of Created or Destroyed animals. How do I do it, so that it does write the values to the log file, but it also displays the number of created and destroyed animals at the end of the program?

    Thanks in advance,

    JOlszewski

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    How do you plan do draw the graphs for such huge number of values?

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    jeez man whats wrong with you??? you have everyone doing your
    work for you, cant you figure out something yourself???

    you have running variables, created and total. for each trial,
    total - created = destroyed. write the values of each into an
    array or a struct which can store the values from the other
    trials as well. all you have to do is set created and total back
    to 0 before you change the creature.

    also, good netiquette would be to thank people like myself, and
    Dave_Sinkula who have made productive contributions making
    up the bulk of your program. the only reason i am replying to
    this is to get you to stop posting threads for a problem that
    was solved for the most part by me last week,

    http://cboard.cprogramming.com/showthread.php?t=75065

    and also improved by Dave soon after, here

    http://cboard.cprogramming.com/showthread.php?t=75201

    and to show you up. quit wasting peoples time and attempt
    a little work for yourself
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    lol...dude, i was going to include you in the credits anyways....
    1. i've mentioned i'm a noobie to programming..after learning c for 2 weeks, try writing a program like this. maybe for you it's easy, but for me isn't.
    2. if you don't want to help, dont; i'm not forcing you.
    Last edited by JOlszewski; 02-01-2006 at 04:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM