7 Replies - 1098 Views - Last Post: 25 February 2007 - 11:40 AM Rate Topic: -----

#1 2late2fail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 07

Adding an average func to array

Post icon  Posted 25 February 2007 - 10:47 AM

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!



# 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 :D

Is This A Good Question/Topic? 0
  • +

Replies To: Adding an average func to array

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Adding an average func to array

Posted 25 February 2007 - 10:52 AM

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:
float avg(float arr[],int size)
{
   float total=0;
   for(int i=0;i<size;i++)
   {
	  total += arr[i];
   }
   return total/size;
}


Was This Post Helpful? 0
  • +
  • -

#3 2late2fail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 07

Re: Adding an average func to array

Posted 25 February 2007 - 10:56 AM

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 :D !
Was This Post Helpful? 0
  • +
  • -

#4 2late2fail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 07

Re: Adding an average func to array

Posted 25 February 2007 - 11:02 AM

on your snippet..
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
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Adding an average func to array

Posted 25 February 2007 - 11:05 AM

The sorting can go anywhere, it will not matter to calculating the average.
Was This Post Helpful? 0
  • +
  • -

#6 2late2fail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 07

Re: Adding an average func to array

Posted 25 February 2007 - 11:08 AM

alright ill work on it

thanks
Was This Post Helpful? 0
  • +
  • -

#7 2late2fail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 07

Re: Adding an average func to array

Posted 25 February 2007 - 11:35 AM

hmmmmmmm


# 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?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Adding an average func to array

Posted 25 February 2007 - 11:40 AM

The avg function appears to be inside another piece of code, it should be removed so it's on its own, as so:

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:
float avg(int arr[],int size);
int main(void)
{
//your main function
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1