473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Functions that accept various type?

QQ
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?

Thanks.

-------------------
#include <stdio.h>

double int_mean (int * array, int num_elem)
{
int i, sum = 0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return (double)sum/(double)num_elem;
}

double double_mean (double * array, int num_elem)
{
int i;
double sum = 0.0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return sum/num_elem;
}

int main ()
{
int i;
int array_size = 5;
int * int_array = (int *)malloc(sizeof(int) * array_size);
double * double_array = (double *)malloc(sizeof(double) * array_size);
for(i = 0; i < array_size; i++)
{
int_array[i] = 0;
double_array[i] = 0.0;
}
int_array[1] = 4;
double_array[3] = 2.1;

printf ("%lf, %lf\n", int_mean (int_array, array_size), double_mean
(double_array, array_size));

return 1;
}
Nov 14 '05 #1
6 1263
On Fri, 10 Dec 2004 22:49:23 -0500, "QQ" <q@q.q> wrote in comp.lang.c:
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?

Thanks.
Yes, but it doesn't save you much.
-------------------
#include <stdio.h>
enum array_type { INT, DOUBLE };

double int_mean (int * array, int num_elem)
{
int i, sum = 0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return (double)sum/(double)num_elem;
}

double double_mean (double * array, int num_elem)
{
int i;
double sum = 0.0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return sum/num_elem;
}
Now replace these two functions with this one:

double mean(void * array, int num_elem, array_type type)
{
double result = 0.0;

if (INT == type)
{
/* code from your int_mean function, change */
/* return statement to: */
result = (double)sum/num_elem; /* only need to cast one */
}
else if (DOUBLE == type)
{
/* code from your double_mean function */
}
else
{
/* decide if you are happy with just returning 0.0, */
/* or calling exit() or abort() or some other error */
/* handling */
}
return result;
}
int main ()
{
int i;
int array_size = 5;
int * int_array = (int *)malloc(sizeof(int) * array_size);
double * double_array = (double *)malloc(sizeof(double) * array_size);
for(i = 0; i < array_size; i++)
{
int_array[i] = 0;
double_array[i] = 0.0;
}
int_array[1] = 4;
double_array[3] = 2.1;

printf ("%lf, %lf\n", int_mean (int_array, array_size), double_mean
(double_array, array_size));

return 1;
}


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?


Yes, it is called C++.
Nov 14 '05 #3

"QQ" <q@q.q> wrote
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?

The C++ way is to use templates.

In C, you could use a typedef to toggle between versions. If the functions
are static this means you can have two versions in one program, but
unfortunately not in scope at the same time.

There is no other practical way of doing it.

For the general problem, however, you would solve it by writing

/*
take mean of addable objects
el - elemets
elsize - size of each element
N - number of elements
add - function to add an element to a double
*/
double mean(void *el, size_t elsize, int N, double (*add)(void *, double))
{
int i;
double tot = 0;
unsigned char *ptr = el;

for(i=0;i<N;i++)
tot += add( &ptr[i*elsize], tot);

return tot/N;
}

This is obviously ridiculous for "mean", but the general principle can be
used for more complex problems, where it may make some sense.
Nov 14 '05 #4
On Sat, 11 Dec 2004 03:49:23 UTC, "QQ" <q@q.q> wrote:
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?

Thanks.

-------------------
#include <stdio.h> #include <stdlib.h>
missing

double int_mean (int * array, int num_elem)
{
int i, sum = 0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return (double)sum/(double)num_elem;
}

double double_mean (double * array, int num_elem)
{
int i;
double sum = 0.0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return sum/num_elem;
}
replace with

/* closed in itself */
#define MEAN(sum, a, n) { \
int i; \
for (i = 0, sum = 0; i < (n)); i++) \
sum += a[i]; \
}

OR
/* requires defition of i outside this macro */
#define MEAN(sum, a, n) \
for (i = 0, sum = 0; i < (n); i++) \
sum += a[i];

int main ()
{
int i;
int array_size = 5;
int * int_array = (int *)malloc(sizeof(int) * array_size); superflous cast, hiding the fact that stdlib.h is not included,
producing undefined behavior.

int isum;
double * double_array = (double *)malloc(sizeof(double) * array_size); superflous cast, hiding missing #include <stdlib.h>, producing
undefined behavior
double dsum;
for(i = 0; i < array_size; i++)
{
int_array[i] = 0;
double_array[i] = 0.0;
}
int_array[1] = 4;
double_array[3] = 2.1;

printf ("%lf, %lf\n", int_mean (int_array, array_size), double_mean
(double_array, array_size)); mismatch type in format list with actual parameter

replace with
MEAN(isum, int_array, array_size);
MEAN(dsum, double_array, array_size);
prinf("%d, %lf\n", isum, dsum);

return 1;
}

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #5
On Fri, 10 Dec 2004 22:49:23 -0500, QQ wrote:
I have two 'mean' functions, one to handle an integer array, and one to
handle a double array (see code provided below). Is there a way to write a
single 'mean' function that would take either an array of ints or an array
of doubles?
It can be done as others have indicated. It isn't usually a good solution
however.
Thanks.

-------------------
#include <stdio.h>

double int_mean (int * array, int num_elem) {
consider making that const int *array
int i, sum = 0;
for (i = 0; i < num_elem; i++)
sum += array[i];
return (double)sum/(double)num_elem;
}


Also consider that INT_MAX need not be larger than 32767. Are you sure
that is always enough to represent the sum for the array? Could num_elem
be zero?

Lawrence
Nov 14 '05 #6
>>double int_mean (int * array, int num_elem)
...
return (double)sum/(double)num_elem;
...
double double_mean (double * array, int num_elem)
...
printf ("%lf, %lf\n", int_mean (int_array, array_size), double_mean
(double_array, array_size));
mismatch type in format list with actual parameter


No way, both functions return a double. You could be a bit more careful
before correcting others.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
4
by: Mattias Brändström | last post by:
Lets say we have some interfaces declared: class A { virtual void a() = 0; }; class B { virtual void b() = 0; }; class C { virtual void c() = 0; }; We also have some classes implementing none...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
2
by: Michael Glaesemann | last post by:
Just finding out about the tinterval type (thanks again, Alvaro!), I've been looking to see what operators and functions are already built-in to PostgreSQL that involve this type. I thought I'd...
19
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
10
by: strife | last post by:
Hey everyone, I was making a program for a class, and I ran into a problem that is driving me crazy. I first suspected Vista, and since I am not home I cannot verify if it just my Vista...
2
by: alan | last post by:
Hello all, in a project I have I have an abstract base type with some virtual functions: class Base{ public: virtual Base* t1() =0; virtual Base* t2() =0; } I have a bunch of derived...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.