Thread: Do...While Loop problem

  1. #1
    Registered User Paul Adams's Avatar
    Join Date
    Feb 2012
    Posts
    9

    Question Do...While Loop problem

    Yes, this is a homework problem, but I have already submitted it for a grade, so please don't think I am trying to get you to complete it for me. So, now I am really just trying to learn what I should have done so that I will be able to fix this problem in the future, as I have LabView, Microcontrollers, and VeriLog coming up.

    I have a loop that gets the a set of data points. It seems to work fine if I use a for loop, however, I need the loop to continue until the user enters zero (0) for the time input. So, I changed from a for loop to a Do... while loop. I am thinking that the problem is in the loop since that is all that changed, but I am new to using the structures and pointers as well, so my mistake could be located else where.

    The commented out sections are a part of my trying to troubleshoot the error. I don't get any data from the Do... While Loop, which makes me think it might be a pointer or structure problem. Again, we just covered structures and pointers in class, so I am new to them.

    Thanks for any help that you can offer.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    double f(double x);
    double monteCarlo(double a, double b, double yMin, double yMax, int trials);
    double expectedDoubles(void);
    double randDouble(double min, double max);
    
    
    struct Data {
        float time;
        float accel;
    };
    
    
    void printItem(struct Data pi);
    const int MAX = 100;
    struct Data points[MAX];
    
    
    void main(int argc, char* argv[])
    {
        int count;
        int i = 0;
        double v;
        int steps = 10000;
        int a, b, yMin, yMax;
    
    
        //printf("How many items? ");
        //scanf("%d", &count);
    
    
        ////Enter in the data, Time = 0 means quit
        printf("Enter in all data, time = 0 to quit.\n");
        count = 0;
        do {
            printf("Time? ");
            scanf("%f", &points[i].time);
            printf("Accel? ");
            scanf("%f", &points[i].accel);    
            ++count;
        } while (points[i].time != 0);
    
    
        //for (i = 0; i < count; ++i) {
        //    printf("Time? ");
        //    scanf("%f", &points[i].time);
        //    printf("Accel? ");
        //    scanf("%f", &points[i].accel);    
        //}
    
    
        //Print out the data points
        /*printf("\n\nPrinting Data Points\n");
        for (i = 0; i < count; ++i) {
            printItem(points[i]);*/
        //}
    
    
        //Find a,b,yMin,yMax ==> Starting data point is assumed to be 0,0
        a = 0;
        b = points[0].time;
        yMin = 0;
        yMax = points[0].accel;
        for (i = 1; i < count; ++i) {
            if(points[i].time > b){
            b = points[i].time;
            }
            if(points[i].accel > yMax){
            yMax = points[i].accel;
            }
            }
    
    
        //Print resulting Velocity
        //Data check
        /*printf("%d, %d, %d, %d\n",a,b,yMin,yMax);*/
        v = monteCarlo(a, b, yMin, yMax, steps);
        printf("Velocity = %.3f [m/s]\n", v);
        getchar();
        getchar();
    }
    
    
    void printItem(struct Data dp) {
        printf("Time: %.2f\n", dp.time);
        printf("Accel: %.2f\n", dp.accel);
        printf("\n\n");
    }
    double f(double x) {  //function to be integrated
        double v;
        //Vf = V0 + a * t
        v = 0 + x;  //v0 starts at 0 according to the problem & at is replaced by x here
        return v;
    }
    double monteCarlo(double a, double b, double yMin, double yMax, int trials) {
        double boundryArea = (b - a) * (yMax - yMin);
        int trial;
        double randX, randY;
        int hits = 0;
    
    
        srand(time(0));
        for (trial = 0; trial < trials; ++trial) {
            randX = randDouble(a, b);
            randY = randDouble(yMin, yMax);
            if (f(randX) >= 0 && randX >= 0 && randY < f(randX)) {
                ++hits;
            } else if (f(randX) < 0 && randY < 0 && randY > f(randX)) {
                ++hits;
            }
        }
        return (hits / (double) trials) * boundryArea;
    }
    
    
    double randDouble(double min, double max) {
        double r = rand() / (double) (RAND_MAX + 1);
        return r * (max - min) + min;
    }
    
    
    double expectedDoubles(void) {
        const int TRIALS = 10000; 
        int trial;
        int die1;
        int die2;
        int rolls;
        int totalRolls = 0;
        
        for (trial = 0; trial < TRIALS; ++trial) {
            rolls = 0;
            do {
                die1 = rand() % 6 + 1;
                die2 = rand() % 6 + 1;
                ++rolls;
            } while (die1 != die2);
            totalRolls += rolls;
        }
        return (double)totalRolls / TRIALS;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It looks like you mean this:
    Code:
        count = 0;
        while (1) {  // or for(;;)
            printf("Time? ");
            scanf("%f", &points[count].time);
            if ((points[count].time == 0)
                break;
            printf("Accel? ");
            scanf("%f", &points[count].accel);    
            ++count;
        }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User Paul Adams's Avatar
    Join Date
    Feb 2012
    Posts
    9
    Ok... now I feel really dense. I see that I forgot to change my parameter from i to count.

    I am not familiar with your way of doing the while loop, but I like it. I guess the break kicks me out of the loop because of the condition met in the If statement.

    Thank you so much. I feel better knowing that I was almost there and my pointers and structure wasn't the issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with loop
    By StartX in forum C Programming
    Replies: 12
    Last Post: 01-04-2012, 01:27 AM
  2. Problem with a for() loop.
    By Flotonic in forum C Programming
    Replies: 38
    Last Post: 06-10-2011, 07:34 AM
  3. Problem with while( ) loop
    By 007007 in forum C Programming
    Replies: 5
    Last Post: 06-06-2011, 10:53 AM
  4. loop problem
    By Noobie in forum C++ Programming
    Replies: 18
    Last Post: 01-29-2003, 02:56 PM

Tags for this Thread