how to do this?

M

Malcolm

jim said:
Write a C program to read through an array of any type. Write a C
program to scan through this array to find a particular value.

Can somebody tell me how to write it?
You must have garbled the assignment. In fact it is hard to think of any
language that will run through an array of _any_ type, though Visual Basic
might come close with its tagged types.
In C you have to know the type of an array before you can process it.
 
P

pete

Malcolm said:
You must have garbled the assignment.
In fact it is hard to think of any
language that will run through an array of _any_ type,
though Visual Basic might come close with its tagged types.
In C you have to know the type of an array before you can process it.

bsearch can run through an array of any type.

#include <stdlib.h>
void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

You have to write code for that kind of interface
if you want to go through an array of any type.

void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t bytes;
unsigned char *i, *j, *k, *p1, *p2, *end, swap;
unsigned char *const array = base;
unsigned char *const after = nmemb * size + array;

if (nmemb > (size_t)-1 / 3) {
nmemb = (size_t)-1 / 3;
}
do {
bytes = nmemb * size;
for (i = bytes + array; i != after; i += size) {
j = i - bytes;
if (compar(j, i) > 0) {
k = i;
do {
p1 = j;
p2 = k;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (bytes + array > j) {
break;
}
k = j;
j -= bytes;
} while (compar(j, k) > 0);
}
}
nmemb = nmemb != 2 ? 3 * nmemb / 7 : 1;
} while (nmemb != 0);
}
 
D

Default User

Malcolm said:
You must have garbled the assignment. In fact it is hard to think of any
language that will run through an array of _any_ type, though Visual Basic
might come close with its tagged types.

No, that's what the web page says:

Exercise 12335

Write a C program to read through an array of any type. Write a C
program to scan through this array to find a particular value.
In C you have to know the type of an array before you can process it.

I'm interpreting it to mean, "an array whose type is of your choosing".
I think it was poorly worded.



Brian Rodenborn
 
K

Keith Thompson

Malcolm said:
You must have garbled the assignment. In fact it is hard to think of any
language that will run through an array of _any_ type, though Visual Basic
might come close with its tagged types.
In C you have to know the type of an array before you can process it.

<OT>Perl can run through an array of any type; in fact, the elements
of the array can be of different "types" (they all have to be scalars,
but that covers a lot of distinct C types.</OT>

C's qsort() and bsearch() functions are designed to work with arrays
of any type.
 
D

Does It Matter

I am learning c right now and found the following tutorial quite
interesting
http://www.cs.cf.ac.uk/Dave/C/node7.html#SECTION00700000000000000000

Here is one of the practive it listed.

Write a C program to read through an array of any type. Write a C
program to scan through this array to find a particular value.

Can somebody tell me how to write it?

The wording is not clear. Often on tutorials the author assumes you have
read through the preceding sections and would try to complete the exercise
with the knowledge you have thus far.

Some of the solutions I could come up with might be touching on things the
tutorial has not covered yet. One solution is:

typedef MYDATATYPE char

int find(MYDATATYPE array[], MYDATATYPE n, int size)
{
int i;

for(i = 0; i < size; i++)
if(array == n)
return i;

return -1;
}

If you want to search an array of int just change the typedef statement.
If you have not been taught about typedef then this is not the solution
the author is looking for. Maybe you are taught about void*. If that is
the case then have a look at the bsearch function.
 
E

Emmanuel Delahaye

jim wrote on 12/08/04 :
I am learning c right now and found the following tutorial quite
interesting
http://www.cs.cf.ac.uk/Dave/C/node7.html#SECTION00700000000000000000

Here is one of the practive it listed.

Write a C program to read through an array of any type. Write a C
program to scan through this array to find a particular value.

Can somebody tell me how to write it?

If this tutorial don't give you the information to do that, how is it
"quite interesting" at all?

Read again the lesson about array and index. If there is something you
don't inderstand, feel free to ask for details, but don't ask for doing
your assignments instead of you. It's your life. Not ours.
 
M

Michael Mair

Hi there,

The wording is not clear. Often on tutorials the author assumes you have
read through the preceding sections and would try to complete the exercise
with the knowledge you have thus far.

Some of the solutions I could come up with might be touching on things the
tutorial has not covered yet. One solution is:

typedef MYDATATYPE char

You want
typedef char MYDATATYPE;
int find(MYDATATYPE array[], MYDATATYPE n, int size)
{
int i;

for(i = 0; i < size; i++)
if(array == n)
return i;

return -1;
}


Note: It is safer to use size_t instead of int for
size and i. Because size_t is an unsigned type, even the
return value -1 makes sense, as it cannot be the index of
an array in a real program (yes, if you could have all the
memory and the representation of NULL would be not an address
you can get by indexing with a size_t index, and if your
program and all other variables were stored somwhere else...,
then you could have it ;-)).

If you want to search an array of int just change the typedef statement.
If you have not been taught about typedef then this is not the solution
the author is looking for. Maybe you are taught about void*. If that is
the case then have a look at the bsearch function.

However, D.I.M.'solution will not work for "an array of any type":
> cat arbfind.c
#include <stdio.h>

#define MYDATATYPE INT_VECTOR
typedef int INT_VECTOR[3];

int find(MYDATATYPE array[], MYDATATYPE n, int size)
{
int i;

for(i = 0; i < size; i++)
if(array == n)
return i;

return -1;
}

int main(void)
{
INT_VECTOR a[]={{1,2,3},{4,5,6},{7,8,9}}, b={1,2,3};
int index;

index = find(a, b, sizeof a/sizeof(INT_VECTOR));
printf("index: %d\n", index);

return 0;
}



The problem obviously is in the comparison, i.e. you cannot do without
either memcmp(), requiring the size of your type (and with the
disadvantage of being unable to get the right result for different
representations of the same "value"), or -- the better solution --
passing a function pointer to a comparison function, i.e. a way
similar to bsearch().

Hmmm, you could use something like find() as a template to
create int_find, char_find and so on, using a different
template for other types with the opportunity to #define
your own comparison. In some cases, this might give you an
edge over bsearch() with its overhead due to the generality.


cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top