Thread: Simple Array Functions

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    Question Simple Array Functions

    Hello,

    I am trying to write a program that populates an array (10) with random values and then gets the Biggest Number, Smallest Number, and Average Value of those numbers.

    Code:
    // CalcArray.h
    
    void InitializeArray(int *X)
    {
        int k,Holder;
        k = 0;
        while ( k < 10 )
        {
            Holder = rand()% 100;
            if ( Holder > 9 )
            {
                k = k + 1;
                *(X++) = Holder;
            }
        }
    }
    
    
    void TheSmallestValue(int *X, int *Y)
    {
    
    
        int k,Biggest,Holder;
     
        Biggest = *X;
     
        k = 0;
        while ( k < 10 )
        {
    
    
            Holder = *(X++);
            if ( Holder < Biggest )
            {
                Biggest = Holder;
            }
    
    
            k++;
        }
    
    
        *Y = Biggest;
    }
    
    
    void TheSmallestNumber()
    {
        int N[10];
        int k;
        int *P;
        int BIG;
    
    
        P = &N[0];
    
    
        InitializeArray(P);
        TheSmallestValue(P,&BIG);
    
    
        cout << "\t\t\t The Smallest Member of the Array is = " << BIG;
    
    
        cout << "\n\n\t\t\t The Values of the Array are :";
    
    
            
        for ( k = 0; k < 10; k++)
        {        
            cout <<"\n\t\t\t\t\t\t\t" << N[k];
        }
        
    }
    
    
    
    
    
    
    
    
    void TheBiggestValue(int *X, int *Y)
    {
        int k,Biggest,Holder;
     
        Biggest = *X;
     
        k = 0;
        while ( k < 10 )
        {
    
    
            Holder = *(X++);
            if ( Holder > Biggest )
            {
                Biggest = Holder;
            }
    
    
            k++;
        }
    
    
        *Y = Biggest;
    
    
    }
    
    
    void TheLargestNumber()
    {
        int N[10];
        int k;
        int *P;
        int BIG;
    
    
        P = &N[0];
    
    
        InitializeArray(P);
        TheBiggestValue(P,&BIG);
    
    
        cout << "\t\t\t The Biggest Member of the Array is = " << BIG;
    
    
        cout << "\n\n\t\t\t The Values of the Array are :";
    
    
            
            for ( k = 0; k < 10; k++)
            {        
                cout << "\n\t\t\t\t\t\t\t" << N[k];
            }
    
    }
    
    // main.cpp
    
    #include "CalcArray.h"
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        TheLargestNumber();
        TheSmallestNumber();
    
    }
    Now my question is, how would I get the Average Value of all the numbers? I have been trying different things and nothing is working.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Is all that code really in a .h file? That should not be the case. You should have the prototypes in the .h file, the implementation in a CalcArray.cpp file, include CalcArray.h in the .cpp files and compile and link all the .cpp files together.

    As far as calculating the average, how would you do in on paper with a list of numbers?

  3. #3
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    After I put it on a piece of paper and thought about it for a while I figured it out. And I will follow your suggestions for the functions. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with simple functions
    By buzz271 in forum C Programming
    Replies: 3
    Last Post: 09-06-2010, 05:25 AM
  2. Simple expressions using functions
    By jusfry01 in forum C Programming
    Replies: 13
    Last Post: 05-28-2010, 11:12 AM
  3. simple functions
    By kbpsu in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2009, 08:01 PM
  4. Help on simple file functions
    By enjoyincubus in forum C Programming
    Replies: 20
    Last Post: 12-01-2006, 04:41 AM
  5. Help on a simple functions program
    By blankstare77 in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2005, 07:06 AM