Thread: homework assignment; need help, what am i missing(!)?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    homework assignment; need help, what am i missing(!)?

    the lesson is about using arrays

    1) Write a program that will take N positive numbers
    from the user and print the average of the numbers.
    The input can be terminated by entering a negative
    number. You can assume N<1000

    the code i have, thus far (not working):

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argv, char** argc){
        int n, i;
        float sum = 0.0, average;
        char num[1000];
        for (i = 0; i < 1000; ++i)
        {
            printf("%d. Enter number: ", i + 1);
            scanf("%f", &n, &num[i]);
            if (n < 0)
                {
                    break;
                }
            
        }
        sum += num[i];
        average = sum / strlen(num);
        printf("Average = %.2f", average);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    of course it's due today so "URGENT!"

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should explain how it does not work.

    Let's start by looking at the warnings generated by the compiler. This is what I got:

    Code:
    /*
    main.c||In function 'main':|
    main.c|10|warning: format '%f' expects type 'float *', but argument 2 has type 'int *'|
    main.c|10|warning: too many arguments for format|
    main.c|3|warning: unused parameter 'argv'|
    main.c|3|warning: unused parameter 'argc'|
    ||=== Build finished: 0 errors, 4 warnings ===|
    */
    The warnings for line 10 are the most relevent. You're passing an *int and a *char to "scanf()", but it is expecting to read just one value (which is a float (%f), at that).

    If you want to read values directly into your array, you don't need to use 'n' at all.

    Your array should be either int or float (depending on the requirements of the assignment).

    I don't understand why you're using character arrays and string functions for a mathematical program.

    I'd recommend starting over, and building up your program gradually, testing as you go.

    First, start with just two loops - one that only reads values into the array, and one that only prints the values of the array.

    When you have this working, add a bit more code to find the sum of all values in the array. Print out the sum and verify it is correct.

    When you have this working, add a bit more code to find the average. Print out the average and verify it is correct.

    Then you're done.

    A development process



    of course it's due today so "URGENT!"
    Shouting "urgent!" will not do you any favors. It's your responsibility to make sure you have enough time to complete your assignments.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you are going to compute the average (presumably the arithmetic mean) by computing the sum, you don't need an array. You can just keep adding to the sum while incrementing the total. If you do want an array, then it should be an array of float, not an array of char. Furthermore, you need to divide by the total, not by strlen(num), which does not make sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with homework assignment
    By haim1404 in forum C Programming
    Replies: 8
    Last Post: 12-14-2013, 06:43 PM
  2. Need help with homework assignment plz..
    By RVDFan85 in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2006, 10:33 PM
  3. Frustration with Homework assignment
    By Jake in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 01:26 PM

Tags for this Thread