Thread: Average Algorithm

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    58

    Post Average Algorithm

    Hi all

    Im currently trying to write a simple average algorithm in flat C and i am unable to load my array with elements. Please could somebody offer me some advice on where i am going wrong?

    Also is it correct i have made num and array as global variables? If these where in the average algorithm would they be reset to zero each time the function was called?

    Please see attachment for sourcecode

    Look forward to your advice

    Tuurbo 46

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int main, always int main

    don't use globals
    Code:
    int array[3]
    printf("Please Enter An Integer Number\n\n");
    scanf("%d", &array[0]);
    use loop for reading
    Code:
    for(i=0;i<3;i++)
    {
       printf("Please Enter An Integer Number\n\n");
       scanf("%d", &array[i]);
    }
    Pass array as a parameter
    Code:
    float average_algorithm(int array[], int size)
    {
    //do your calculations
    return avg;
    }
    do your output in the main

    Code:
    int main()
    {
       int array[3];
       float avg = 0.0;
       //fill the array
       avg = average_algorithm(array,3);
       //output results
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    58
    Hi

    Thanks for your advice. Was my code awful, or ok for a newbie?

    cheers tuurbo46

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Well it was full of newbish type mistakes, but other than that, it pretty sound. As you evolve, though, you'll probably want to get rid of those scanfs.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Divide and Average Algorithm
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-25-2002, 03:30 PM