Solved

C Arrays (function, prototype, main program)

Posted on 2003-11-10
13
668 Views
Last Modified: 2010-04-15
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[][];?
0
Comment
Question by:john2789
13 Comments
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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
 

Author Comment

by:john2789
Comment Utility
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
 

Author Comment

by:john2789
Comment Utility
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
 

Author Comment

by:john2789
Comment Utility
No Help me? even thought I told you that THIS IS NOT AN ASSIGNMENT.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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
 

Author Comment

by:john2789
Comment Utility
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
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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
 

Author Comment

by:john2789
Comment Utility
soo, what are differences from the problem I posted today and this one?
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 50 total points
Comment Utility
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now