Programming help PLEASE!

Discussion in 'Computer Science & Culture' started by Adam, Apr 18, 2002.

Thread Status:
Not open for further replies.
  1. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Alrighty then. I haven't done much programming, and I have this little sucker to do for a class. I don't know exactly what I'm doing, as you will see.

    ----------------

    #include <stdio.h>

    #define sqroot(x) (x / x);

    float variance_function(float, float);

    int main(int argc, char *argv[])
    {

    /* Declare and initialise some variables */
    float sum = 0.0, mean = 0.0, standev = 0.0, variance = 0.0;
    int num_of_entries = 0;
    char do_or_not = 'n';

    /* Initial user prompt */
    printf("\nWelcome to number-thingamajig!");
    printf("\nTo use the programme, enter Y, else enter N: ");
    scanf("%c", &do_or_not);

    /* Run programme in a whopping great big if/else */
    if(do_or_not == 'N' || 'n')
    {
    printf("\nExiting programme...\n\n");
    break;
    }
    else
    {

    /* Gather input */
    while(scanf() != "\n")
    {
    sum += scanf("%f");
    num_of_entries++;
    }

    /* Work out the mean, standard deviation, and variance here */
    mean = ( sum / num_of_entries );
    variance = variance_function(sum, num_of_entries);
    standev = sqroot(variance);

    /* Display output */
    printf("\nRESULTS");
    printf("\nNumber of entries: %d", num_of_entries);
    printf("\nSum of entries: %f", sum);
    printf("\nMean: %f", mean);
    printf("\nStandard Deviation: %f", standev);
    printf("\nVariance: %f", variance);

    printf("\n\nThank you, come again!");

    }

    }

    float variance_function(float sum, float num_of_entries)
    {
    return ((sum * sum)/num_of_entries) - (num_of_entries * num_of_entries);
    }

    ----------------

    See, it's supposed to take in a bunch of floats and work out the sum, mean, standard deviation, and variance, then printf() them all out. The thing is, it also needs the argc and argv. What are the argc and argv in there for if I'm to have the programme accepting entries within a loop? And how do I make it all work?

    Thanks.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Porfiry Nomad Registered Senior Member

    Messages:
    4,127
    argc and argv are merely there for passing command-line arguments into the program. You never fill them in -- the OS/shell will add them in (argc being the # of arguments, argv being an array of the arguments themselves). I think you can just ignore them.

    Ooh, watch out here. You really want:

    Your version of the if would always evaluate to true since 'n' is always true (anything non-zero is true). Also, I think your nesting of the while() inside the if() is wrong. I'm pretty sure you want the if() to be inside the while(). You want to evaluate the condition on each character that's inputted.

    There are also a bunch of other errors, but I'm sure you'll come across them while debugging

    Please Register or Log in to view the hidden image!

     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Groovy, thanks for the tips.

    Please Register or Log in to view the hidden image!

     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    That's how I write programmes at the moment, by the way. I throw in all the ideas I want and then start pushing bits around until it works.
     
  8. Ash711 Registered Member

    Messages:
    24
    hi adam, just be careful as your sqroot script is false (it will always return 1 (x/x = 1, for all x !=0)
    you should better use:
    #includes "math.h" then call sqrt(x) -> float......

    Same caution for your variance, the formula is false
    variance is
    variance(X) = sum(( Xi - mean)^2)/numbOfObservations
    (sum of square mean deviation)


    Oh and for argc and argv... why not allow the user to type directly the numbers from the console ?
    something like
    myprog 15 15 20 15 17
    will return
    mean=16.4
    variance=4.84
    mean deviation=2.2
    hopes it'll helps.....
     
  9. sjmarsha Registered Senior Member

    Messages:
    363
    Ouch! Using scanf() inside a while loop! My god man that has the potential to be one of the best ways around the white space problem I have ever seen... One question tho...Why didn't you just use getch() and then use atoi()?

    Also isn't there a function called isnumber() (or isnum() or something like that). Would it not be better to enter your loop on that rather than a non enter? i.e what happens if a space is typed in by mistake or a letter for that matter.

    Just wondering thats all..
     
  10. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Thanks very much for the suggestions. I have not used most of the functions you all mentioned yet, so I'll give them a try tomorrow.
     
  11. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    This is what I have so far. Not working at all.

    Please Register or Log in to view the hidden image!



    -----------------------------------------------------

    #include <stdio.h>
    #include <math.h>

    double variancefunction(double, int);

    int main(int argc, char *argv[])
    {
    /*******************************************/
    /* DECLARE AND INITIALISE VARIABLES */

    int num_of_entries = 0;
    double input = 0.0, sum_of_entries = 0.0, variance = 0.0, mean = 0.0, standev = 0.0;

    /*******************************************/
    /* INPUT */

    while(input != '\0')
    {
    printf("\nEnter a number: ");
    scanf("%f", &input);
    sum_of_entries += input;
    num_of_entries++;
    }

    /*******************************************/
    /* WORKING STUFF OUT */

    mean = sum_of_entries / num_of_entries;
    variance = variancefunction(sum_of_entries, num_of_entries);
    standev = sqrt(variance);

    /*******************************************/
    /* DISPLAY OUTPUT */

    printf("\n\nRESULTS");
    printf("\nNumber of entries: %d", num_of_entries);
    printf("\nSum of entries: %f", sum_of_entries);
    printf("\nMean of entries: %f", mean);
    printf("\nStandard deviation: %f", standev);
    printf("\nVariance: %f", variance);
    printf("\n\nThank you, come again!");

    /*******************************************/

    return 0;
    }
    /* END OF MAIN */
    /*******************************************/


    /*******************************************/
    /* FUNCTION TO WORK OUR VARIANCE */

    double variancefunction(double sum_of_entries, int num_of_entries)
    {
    return ((sum_of_entries * sum_of_entries)/num_of_entries) - ((sum_of_entries / num_of_entries) * (sum_of_entries / num_of_entries));
    }
     
  12. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    DOH! I see it. Funny how it looks completely different on a webpage than in my compiler.
     
  13. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    No, I don't see. I hate programming, at least for the rest of the afternoon.

    Please Register or Log in to view the hidden image!

     
  14. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Okay, I've simplified it some. Now it is getting stuck in the while loop. When you hit Q or q to exit the loop, it continually printf()s the loop's question.

    PS: I'll add more bits and pieces later when I've got the basic thing working.

    PPS: I'm not getting any errors, and the only warning are those for not using argc and argv.

    ------------------------------------------

    #include <stdio.h>
    #include <math.h>

    double variancefunction(double, int);

    int main(int argc, char *argv[])
    {
    int num_of_entries = 0;
    double input = 0.0, sum_of_entries = 0.0, variance = 0.0, mean = 0.0, standev = 0.0;

    printf("\n\nInput numbers and hit ENTER after each. Hit 'Q' to quit.");

    while((input != 'Q') && (input != 'q'))
    {
    printf("\nEnter a number: ");
    scanf("%f", &input);
    sum_of_entries += input;
    num_of_entries++;
    }

    mean = sum_of_entries / num_of_entries;
    variance = variancefunction(sum_of_entries, num_of_entries);
    standev = sqrt(variance);

    printf("\n\nRESULTS");
    printf("\nNumber of entries: %d", num_of_entries);
    printf("\nSum of entries: %f", sum_of_entries);
    printf("\nMean of entries: %f", mean);
    printf("\nStandard deviation: %f", standev);
    printf("\nVariance: %f", variance);
    printf("\n\nThank you, come again!");

    return 0;
    }

    double variancefunction(double sum_of_entries, int num_of_entries)
    {
    return ((sum_of_entries * sum_of_entries)/num_of_entries) - ((sum_of_entries / num_of_entries) * (sum_of_entries / num_of_entries));
    }
     
  15. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    #include <stdio.h>
    #include <math.h>

    double variancefunction(double, int);

    int main(int argc, char *argv[])
    {
    int num_of_entries = 0;
    double input = 0.0, sum_of_entries = 0.0, variance = 0.0, mean = 0.0, standev = 0.0;

    printf("\n\nInput numbers and hit ENTER after each. Hit 'Q' to quit.");

    while(input != 0)
    {
    printf("\nEnter a number: ");
    scanf("%f", &input);
    if((input == 'Q') || (input == 'q'))
    break;
    sum_of_entries += input;
    num_of_entries++;
    }

    mean = sum_of_entries / num_of_entries;
    variance = variancefunction(sum_of_entries, num_of_entries);
    standev = sqrt(variance);

    printf("\n\nRESULTS");
    printf("\nNumber of entries: %d", num_of_entries);
    printf("\nSum of entries: %lf", sum_of_entries);
    printf("\nMean of entries: %lf", mean);
    printf("\nStandard deviation: %lf", standev);
    printf("\nVariance: %lf", variance);
    printf("\n\nThank you, come again!");

    return 0;
    }

    double variancefunction(double sum_of_entries, int num_of_entries)
    {
    return ((sum_of_entries * sum_of_entries)/num_of_entries) - ((sum_of_entries / num_of_entries) * (sum_of_entries / num_of_entries));
    }
     
  16. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Ah well, I was too tired to think clearly. I re-wrote the thing entirely, half the size and twice as neat. Got bloody good marks. It ended up about the neatest code in the class.

    If you're wondering about how this programming class works, well, teh first week's assignment was a for loop. This thing was second week. Third week's assignment, I'm told, is a couple of pages long using things we haven't used before. They really to push on head quite fast.
     
  17. Rick Valued Senior Member

    Messages:
    3,336
    You buy out these books:
    1.)Ray Duncan's Dos programming.
    2.)assembly language primer by Robert Lafore.
    3).C++ by Robert lafore.

    Ray duncan's is bible fro programming,in case you"re an aitheist if you dont have this book you"re missing something.(and that is big).

    bye!
     
  18. sjmarsha Registered Senior Member

    Messages:
    363
    You've declared your input as a double, I'm not sure but if you try to put a char in it you could get problems...

    I would have gone for the isdigit() funtion (sorry i said isnum before...) , i.e done this

    printf("\nEnter a number: ");
    scanf("%f", &input);

    while (isdigit(input))
    {
    printf("\nEnter a number: ");
    scanf("%f", &input);
    sum_of_entries += input;
    num_of_entries++;
    }

    if ((input == 'Q') || (input == 'q'))
    {
    /* Your code*/
    Return 0;
    }
     
Thread Status:
Not open for further replies.

Share This Page