0

Here is the question: write a complete function definition for a function named findLargest that accepts as array of int as a parameter and accepts the array size as another parameter. The function should use a return statement to return the largest value in the array.

here is my rough code:

int findLargest()

{

    int size;

    cout << "Enter size of array" << endl;
    cin >> size;

    int i;
    int numArray [size];
    {
        for (i = 1; i < size; index++);
        {
            cout << "Enter " << i << "number" << endl;
            cin >> numArray [i];
        }
        }
int findLargest = numArray[1]

    int t;
        for (t = 2; t < 2 size ; index++);
        {
            if numArray[t] < findLargest 
            {
                findLargest = numArray[t];
            }
        }
    cout << "Your highest number is " << findLargest;
}

if anyone could help with the correct solution that would be great.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Probs
  • 23
  • 5
  • What is the problem? What errors/wrong results do you get? – hammar May 09 '11 at 20:14
  • 1
    [The correct return type for `main` is `int`.](http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main/) – James McNellis May 09 '11 at 20:15
  • I do know i will not be using void main in my final question unless that is required for a complete function definition. – Probs May 09 '11 at 20:16
  • the problem is it wont compile correctly – Probs May 09 '11 at 20:16
  • 3
    For one thing, you didn't follow the directions. The assignment is asking for you to make a function that will return this value. AKA `int largestValue = MyFunction(numArray, 10);` – Tejs May 09 '11 at 20:16
  • @Tejes- then what is needed to be done to the code to make it follow the directions. – Probs May 09 '11 at 20:19
  • 1
    @CLM: **read** the directions. They specifically state the function name. Go from there. – Matt Ball May 09 '11 at 20:21
  • in the question im not too sure what array of int being a parameter means. – Probs May 09 '11 at 20:24
  • @Probs: In that case you need to go back and repeat the earlier parts of the course (or book). It should have been covered when you first learned about functions. – molbdnilo May 10 '11 at 12:30

5 Answers5

4

You could start with writing the function findLargest(Your parameters here) first and then call that function with a preinitialized array (e.g. int myArray[10] = {1,2,3,4,5,6,7,9,0}; findLargest(myArray, 10)) to test if it works. With that, you wouldn't need to read the content of the array from standard input and focus on the problem from your assignment instead.

evnu
  • 6,450
  • 2
  • 27
  • 38
3

In C++, you can't do this:

int numArray [size];

as array dimensions must be compile-time constants, although if you are using GCC it may compile. Instead, investigate the use of the standard C++ feature, std::vector.

1

If you're asking for user input for the size of the array, you would need to create a dynamic array.

int size = 0;
cin >> size;

int *a = null; //could use any data type
a = new int[size]; 

before the program ends,

delete a[];
tehman
  • 828
  • 2
  • 11
  • 34
1

You don't have the parameters right yet.

write a complete function definition for a function named findLargest that accepts as array of int as a parameter and accepts the array size as another parameter.

int findLargest(int theArray[], int size);

Next, you have to figure out how to iterate over the entire array:

for(int i=0; i<size; ++i)

Next, figure out if each element is the biggest one:

int max = theArray[0];
if (theArray[i] > max)   max = theArray[i];

When done, return the biggest value you found.

return max;

Then, combine all the parts together:

int findLargest(int theArray[], int size)
{
    int max = theArray[0];

    for(int i=0; i<size; ++i)
    {
        if (theArray[i] > max)   max = theArray[i];
    }

    return max;
 }

If you wish to go futher, test your function.

int main(void)
{
    int testA[6] = {4, 10, 3, 6, 5, 9};  // Answer should be 10.
    int testB[5] = {-8, -5, -1, -7, -6}; // Test of negative numbers.
    int testC[4] = {0, 0, 0, 0};         // Test of zero and repeating numbers.

    cout << "Largest in A is " << findLargest(testA, 6);
    cout << "Largest in B is " << findLargest(testB, 5);
    cout << "Largest in C is " << findLargest(testC, 4);
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • not a big deal but you can save on one iteration if start on `i=1`, also perhaps handling size = 0 case, but perhaps it's assumed valid input with size >= 1 – Luke Hutton May 10 '11 at 20:38
  • While you can save one iteration, the poster is clearly a student and I don't want to confuse him by even hinting that C-arrays might start at 1. going from 0 to – abelenky May 11 '11 at 05:05
0

First of all, the assignment is pretty clear that you must define a function for this task, and can't do it inside main():

int findLargest(int* array, int size)
{
    int largest = 0;
    for (int i = 0; i < size; i++)
    {
        if (array[i] > largest)
            largest = array[i];
    }

    return largest;
}

int main()
{
    int my_array[9] = { 1, 2, 3, 4, 5, 50, 6, 7, 8 } ;

    cout << "* Largest: " << findLargest(my_array, 9) << endl;    
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426