• C

C Arrays (function, prototype, main program)

1. Write a function that will return the largest value in any integers array. (HINT: The function should accept 2 parameters)

--> void array(largest_value[], int n)

2. Write a prototype for the function in part 1.

--> int largest_value[int a, int b];

3. Write a main program that declares two arrays of different sizes and calls the function written in part 1.

"Two arrays"---> This means 2-dimentional Array? like, int a[][];?
john2789Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tinchosCommented:
Sorry john2789,

but I believe that this is homework and we're not here to do that.

If you want, you can try to start thinking it and we'll help you, but we won't do homework for you.

Cheers!

Tincho
0
Kent OlsenData Warehouse Architect / DBACommented:

Hi John2789,

Thanks for posting, but please go back and reread the terms of membership at this link:

http://experts-exchange.com/Programming/Programming_Languages/C/memberAgreement.jsp

You will discover that you agreed to not ask for anyone to do your homework for you and agreed that Experts would not do your homework.


However, your question has recently been discussed at the next link:

http://experts-exchange.com/Programming/Programming_Languages/C/Q_20792577.html

Please take note that the poster asked for guidance and help.  The Experts here will gladly give you all the help that you ask for -- short of actually coding your homework.

Good Luck!

Kent

0
john2789Author Commented:
Excuse me.... BUT!!! This is not a homework or any kind of assignments!!!

I just have been solving problems on my reference & practice books and I try to understand C Programming. If THIS is an assignment, I won't ask you!!!

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

john2789Author Commented:
I just want to know how I can write the function and prototype on this kind of problem. And I have a little confused if my book asked me about two array. What does this two arrays mean??
2-dimentional array or what?

Oh, yeah I read the policy of this website and I have understood.
0
john2789Author Commented:
No Help me? even thought I told you that THIS IS NOT AN ASSIGNMENT.
0
Kent OlsenData Warehouse Architect / DBACommented:

Hi John,

Please follow the second link that I gave.  It offers a pretty good discussion of the same problem that you've posted.  If you still have questions please feel free to ask them and we'll be glad to answer them.

But what you've posted to initiate this thread IS right out of the text book that quite a number of students are currently using.  We can not post answers that we feel are most likely homework assignments.


Kent
0
john2789Author Commented:
Well. HOW CAN I IMPROVE THAT THIS IS NOT AN ASSIGNMENT?

Umm... how about contacting to my mom...? Wait, Mom is going work tonight because she is a emergency doctor. Dad, he went to Iraq and didn't come back yet... Well letsee.. Oh! You can contact to MY Older brother. He knows what I have been studying C. His name is Jon and I think he is still in his office. His office number is 316-978-2187. ASK him!!!

I don't go to school, but I am interested in C programming because IT'S COOL.

What is 256? 256 MB on PC MEMORY? Where is the 256 coming from?
0
Kent OlsenData Warehouse Architect / DBACommented:

There are 256 unique codes in the ASCII character set.  This makes sense -- a byte is 8 bits and 2**8 = 256.

So if you start with an array that is big enough to be indexed by any 8-bit value, you have an array of 256 elements.


Kent
0
john2789Author Commented:
soo, what are differences from the problem I posted today and this one?
0
rstaveleyCommented:
I've just checked your question history and I see that you've been subjected to the Spanish Inquisition with each question you've posted from your teach yourself C book.

When you pass an array to a function in C, you typically pass it "by reference" rather than passing a copy of it. When the pass an array by reference in C you are actually passing a pointer to the array to the function and many C programmers write the function and prototype using a pointer instead of using [].

Here's your program:
--------8<--------
#include <stdio.h>
#include <limits.h>

/* This is a prototype for the function - see how the parameter names aren't used here. In prototypes, parameter names are optional. Compare this with the function itself. */
int largest_value(int [],size_t);

int main()
{
int my_array[] = {1,3,5,6,8,3,5};
int max;
   
        max = largest_value(
                my_array  /* This gives us a pointer to the array */
                ,sizeof(my_array)/sizeof(int)    /* This gives us the number os elements in the array */
                );

        printf("Max value is %d\n",max);
}

/* Here is the actual function. There are two parameters, the first being a pointer to the array and the second being something which says how large the array is */
int largest_value(int array[],size_t length)
{
int retval = INT_MIN,i;

        for (i = 0;i < length;i++)
                if (array[i] > retval)
                        retval = array[i];

        return retval;
}
--------8<--------

Here's the same thing using a pointer for the array.
--------8<--------
#include <stdio.h>
#include <limits.h>

int largest_value(int*,size_t);

int main()
{
int my_array[] = {1,3,5,6,8,3,5};
int max;
   
        max = largest_value(my_array,sizeof(my_array)/sizeof(int));
        printf("Max value is %d\n",max);
}

/* Here is the actual function. Note how the pointer is used just the same as [] */
int largest_value(int *array,size_t length)
{
int retval = INT_MIN,i;

        for (i = 0;i < length;i++)
                if (array[i] > retval)
                        retval = array[i];

        return retval;
}
--------8<--------

You may be wondering why you don't often see the following:
--------8<--------
#include <stdio.h>
#include <limits.h>

int largest_value(int [7]);

int main()
{
int my_array[] = {1,3,5,6,8,3,5};
int max;
   
        max = largest_value(my_array);
        printf("Max value is %d\n",max);
}

int largest_value(int array[7])
{
int retval = INT_MIN,i;

        for (i = 0;i < 7;i++)
                if (array[i] > retval)
                        retval = array[i];

        return retval;
}
--------8<--------

The version above passes a copy of the array to the function, which means that you get inefficient code (lots of data being moved around). It also means that the largest_value() function couldn't be used for different array sizes.

I expect question 3 means 2 arrays, rather than a 2-D array. 2-dimensional arrays are a lot more complicated than 1-dimensional arrays in C.

Sometimes (usually?) they are not strictly speaking 2-dimsional arrays at all, actually being handled as arrays of pointers to arrays. The reason for this complexity is that C functions need to know the size of the last dimension of the 2-D array for true 2-D arrays to know where to look for the storage for the next row.

Here's your function using a "true" 2-D array:
--------8<--------
#include <stdio.h>
#include <limits.h>

/* This is a prototype for the function */
int largest_value(int [][7],size_t);

int main()
{
int my_array[][7] = {{1,3,5,6,8,3,5},{1,2,3,4,5,6,7},{9,8,7,6,5,4,3}};
int max;
   
        max = largest_value(
                my_array  /* This gives us a pointer to the 2-D array */
                ,sizeof(my_array)/sizeof(int[7])    /* This gives us the size of the first dimension */
                );

        printf("Max value is %d\n",max);
}

int largest_value(int array[][7],size_t first_dimension)
{
int retval = INT_MIN,i,j;

        for (i = 0;i < first_dimension;i++)
                for (j = 0;j < 7;j++)
                       if (array[i][j] > retval)
                               retval = array[i][j];

        return retval;
}
--------8<--------

Notice that we are stuck with the inflexibility of specifying the size of the second dimension.

It is more flexible when you use an array of pointers to arrays.
--------8<--------
#include <stdio.h>
#include <limits.h>

/* This is a prototype for the function */
int largest_value(int *[],size_t,size_t);

int main()
{
int a1[] = {1,3,5,6,8,3,5};
int a2[] = {1,2,3,4,5,6,7};
int a3[] = {9,8,7,6,5,4,3};
int *my_array[] = {a1,a2,a3};
int max;

        max = largest_value(
                my_array  /* This gives us a pointer to the 2-D array */
                ,sizeof(my_array)/sizeof(int*)    /* This gives us the size of the first dimension */
                ,sizeof(a1)/sizeof(int) /* No mechanism here for getting the size of the second dimension directly from my_array */
                );

        printf("Max value is %d\n",max);
}

/* In this function we need to know the size of our array of pointers (the first dimension) and the size of the arrays pointed to, which we assume all have the same size - i.e. are not jagged. The size of the arrays pointed to is the second dimension */
int largest_value(int *array[],size_t first_dimension,size_t second_dimension)
{
int retval = INT_MIN,i,j;

        for (i = 0;i < first_dimension;i++)
                for (j = 0;j < second_dimension;j++)
                       if (array[i][j] > retval)
                               retval = array[i][j];

        return retval;
}
--------8<--------

I hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.