开发者

Array homework question need help

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 gre开发者_JS百科at.


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.


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.


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[];


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);
}


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;    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜