Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,081 people online right now. Registration is fast and FREE... Join Now!




Adding an average func to array

 
Reply to this topicStart new topic

Adding an average func to array, wow i need some insight

2late2fail
25 Feb, 2007 - 09:47 AM
Post #1

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 5


My Contributions
Hello all!
Well I seem to have reached a boiling point -- I programmed my array to take 10 numbers and print them out but i dont know how to have them print in ascending order and then take those same 10 numbers and print the average .. yeah i've been doin this for so long i've confused myself!



CODE

# include  <stdio.h>

    int   ask_for_numbers ( float [ ], int );
    void  display_float ( float *, int );

    void main( void )
    {
    
        int elements_count;
        float F_array[10];

        elements_count = ask_for_numbers ( F_array, 10 );
        display_float ( F_array, elements_count );


     }

        int ask_for_numbers ( float ar[ ], int size )
        {
            float number = 1.1;
            int count = 0;
            printf ("\n-->Input: Max of 10 numbers\n");
            printf ("\nEnter 10 Numbers or '0' to exit program.\n");
            while ( number != 0.0 && count < size )
            {
                    printf ( "%d: ", count + 1 );
                    scanf ( "%f", &number);
                    if ( number != 0.0 )
                        ar[count++] = number;
            }
        return ( count );
        }
        void display_float ( float *ar, int size )
        {
            int index;
            printf ("\n--> Your output is:\n");  
            for ( index=0; index < size; ++index )
                  printf ("[%.1f] ", *ar++ );
            printf ("\n");
        return;
        }


and yeah it works now -- i just need some, no wait, ALOT of help
--> so go ahead and chop it up if you need to, whatever helps will b good - thanks biggrin.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding An Average Func To Array
25 Feb, 2007 - 09:52 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
To average all the values in an array, you can simply add them together and divide by the number of elements. Assuming that you know the size of the array, and are passing it to the function:
CODE

float avg(float arr[],int size)
{
   float total=0;
   for(int i=0;i<size;i++)
   {
      total += arr[i];
   }
   return total/size;
}

User is offlineProfile CardPM
+Quote Post

2late2fail
RE: Adding An Average Func To Array
25 Feb, 2007 - 09:56 AM
Post #3

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 5


My Contributions
Hmm seems easy enough. ty

I'll play around with it, and see what I can do, then post the results.

The max numbers it takes is 10, so its fairly easy - just a simple project im doing with a friend for programming.

The only thing I cannot get is the sorting, I was thinking bubblesort or qsort, but after reading posts - im back at [square]-1, I'm sure ill end up using a quick comparison and printf func. to return my results.

Any other insight would be nice biggrin.gif !
User is offlineProfile CardPM
+Quote Post

2late2fail
RE: Adding An Average Func To Array
25 Feb, 2007 - 10:02 AM
Post #4

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 5


My Contributions
on your snippet..
CODE

float avg(float arr[],int size)
{
   float total=0;
   for(int i=0;i<size;i++)
   {
      total += arr[i];
   }
   return total/size;
}


Should i throw this in after sorts or before? I don't think it will matter much, I just have so many { } brackets hahahaha i'm losing my mind.

I just need visuals!!!

Rest i can work on my own

thanks
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding An Average Func To Array
25 Feb, 2007 - 10:05 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The sorting can go anywhere, it will not matter to calculating the average.
User is offlineProfile CardPM
+Quote Post

2late2fail
RE: Adding An Average Func To Array
25 Feb, 2007 - 10:08 AM
Post #6

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 5


My Contributions
alright ill work on it

thanks
User is offlineProfile CardPM
+Quote Post

2late2fail
RE: Adding An Average Func To Array
25 Feb, 2007 - 10:35 AM
Post #7

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 5


My Contributions
hmmmmmmm

CODE


# include  <stdio.h>

        int   ask_for_numbers ( float [ ], int );
        void  display_float ( float *, int );

    void main( void )
    {
    
        int elements_count;
        float F_array[10];

        elements_count = ask_for_numbers ( F_array, 10 );


        display_float ( F_array, elements_count );


     }
        int ask_for_numbers ( float ar[ ], int size )
        {
            float number = 1.1;
            int count = 0;
            printf ("\n<<< Array Initialization - Max 10 numbers >>>\n");
            printf ("\nEnter floating point numbers or 0 to exit.\n");
            while ( number != 0.0 && count < size )
            {
                    printf ( "%d: ", count + 1 );
                    scanf ( "%f", &number);
                    if ( number != 0.0 )
                        ar[count++] = number;
            }

        return ( count );
        }
        {
        float avg(float arr[],int size);
        {
         float total=0;
         for(int i=0;i<size;i++);
          {
            total += arr[i];
        }
          return total/size;
        }
        }



Roughly to display the avg correct? Yet i still have to define arr[] and size i do believe --> but i am getting two errors one on the syntax { after return total and on float avg (missing header?)

any ideas?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding An Average Func To Array
25 Feb, 2007 - 10:40 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The avg function appears to be inside another piece of code, it should be removed so it's on its own, as so:

CODE

int main(void)
{
//your main function
}
//avg function
float avg(float arr[],int size);
        {
         float total=0;
         for(int i=0;i<size;i++);
          {
            total += arr[i];
        }
          return total/size;
        }
//other functions as required
var1 func1(param)
{
}

When defining your functions after the main function, you need to use a function prototype to let the compiler know what to expect as far as usage goes:
CODE

float avg(int arr[],int size);
int main(void)
{
//your main function
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month