Thread: Average function is not averaging...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Average function is not averaging...

    Hi everyone. I have enclosed my sorting program and for some reason the average function is not averaging. I cannot find my mistake. The program sorts no problem at all but my average takes the smallest integer and uses it as the average. Can anyone take a looksee and tell me where my mistake is? Thanks!
    Code:
    //list.h
    #ifndef LIST_H_INCLUDED
    #define LIST_H_INCLUDED
    
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    #define MAX 100
    
    bool isAscending (int *array, int len);
    bool isDescending (int *array, int len);
    bool isInOrder (int *array, int len);
    double average (const int *array, int len);
    int *findMax (const int *x, int n);
    int getInfo (int *array, int max);
    int total (const int *array, int len);
    void sort (int *x, int n);
    void swap (int &x, int &y);
    
    #endif // LIST_H_INCLUDED
    
    //main.cpp
    #include "list.h"
    
    int main()
    {
        int array[MAX];
        int counter = 0;
        int len;
        len = getInfo (array, MAX);
        double x = average (array, len);
    
        if (len > 0) cout << "\nAvg: " << x << endl;
    
        for (;counter < len; counter++) cout << setw(4) << *(array + counter);
        cout << endl;
    
        if (isInOrder (array, len)) cout << "its in order" << endl;
        else sort (array, len);
    
        if (isInOrder (array, len)) cout << "its in order" << endl;
    
        for (counter = 0; counter < len; counter++) cout << setw(4) << *(array + counter);
        cout << endl;
    
        return 0;
    }
    
    //isAscending.cpp
    #include "list.h"
    
    bool isAscending (int *array, int len)
    {
        for (int counter = 0; counter < len - 1; counter++)
            if (*(array + counter) > *(array + counter + 1)) return false;
        return true;
    }
    
    //isDescending.cpp
    #include "list.h"
    
    bool isDescending (int *array, int len)
    {
        for (int counter = 0; counter < len - 1; counter++)
            if (*(array + counter) < *(array + counter +1)) return false;
        return true;
    }
    
    //isInOrder.cpp
    #include "list.h"
    
    bool isInOrder (int *array, int len)
    {
        return isAscending (array, len) || isDescending (array, len);
    }
    
    //average.cpp
    #include "list.h"
    
    double average (const int *array, int len)
    {
        int tot = total (array, len);
        return (double) tot/len;
    }
    
    //findMax.cpp
    #include "list.h"
    
    int *findMax (const int * x, int n)
    {
        int temp = 0;
        int counter = 1;
        while (counter <= n)
        {
            if (*(x + counter) > *(x + temp)) temp = counter;
            counter++;
        }
        return (const_cast < int *> (x + temp));
    }
    
    //getInfo.cpp
    #include "list.h"
    
    int getInfo(int *array, int max)
    {
        int counter = 0;
        while (cin >> *array)
        {
            array++;
            counter++;
            if (counter == max) break;
        }
        return counter;
    }
    
    //total.cpp
    #include "list.h"
    
    int total (const int *array, int len)
    {
        int sum = 0;
        while (len--) sum +=*array;
        return sum;
    }
    
    //sort.cpp
    #include "list.h"
    
    void sort (int *x, int n)
    {
        int *j;
        while (n--)
        {
            j = findMax (x, n);
            swap (*j, *(x + n));
        }
    }
    
    //swap.cpp
    #include "list.h"
    
    void swap(int &x,int &y)
    {
        int temp = x;
        x = y;
        y = temp;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your total function is wrong. It keeps summing the first element in the array only.
    Also consider using std::vector instead and use array[n] instead of *(array + n).
    And naturally, use a reference for the vector (or iterators) instead of pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Thanks Elysia! We have not gotten to OOP yet even though it is an OOP class. As for the pointer to arrays, this is the way the professor wants it right now. I appreciate your input very much!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alright then. But you could at least remember it for future reference. It should be helpful!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    I will do! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM