Solved

C Arrays (function, prototype, main program)

Posted on 2003-11-10
13
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9719398
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:Kent Olsen
ID: 9719428

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
ID: 9719489
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:john2789
ID: 9719517
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
ID: 9719600
No Help me? even thought I told you that THIS IS NOT AN ASSIGNMENT.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9719612

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
ID: 9719666
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:Kent Olsen
ID: 9719700

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
ID: 9719726
soo, what are differences from the problem I posted today and this one?
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 50 total points
ID: 9719792
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

636 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