开发者

help with newbie c++ code

I am learning C++ and need help with my code. I don't understand how I get compiler errors in my second function. It's almost a mirror of the first, but I don't get any errors from the first.

#include <iostream>
using namespace std;

// Function prototypes
int getLargest(int[], int);
int getSmallest(int[],int);

int main()
{
    const int ARRAY_SIZE = 10;
    int array[ARRAY_SIZE];
    int count = 0;

    cout << "Please enter 10 numbers into the array.\n\n";
    for (count; count < ARRAY_SIZE; count++)
    {
        cout << "Number " << (count +1) << ": " ;
        cin     >> array[count];
    }

    // Call function to calculate largest number. 
    int largest = getLargest(array, ARRAY_SIZE);

    // Display the largest number
    cout << "The largest number is " << largest << endl;

    // Call function to calculate smallest number. 
    int smallest = getSmallest(array, ARRAY_SIZE);

    // Display the smallest number.
    cout << "The smallest number is " << smallest << endl;

    return 0;
}

//**********************************************************************
// Function definition getLargest.
// This function accepts an array argument and returns t开发者_如何学运维he largest element.
//**********************************************************************
int getLargest(int arrays[], int size1)
{
    // Find the largest  number in the array. 
    int large = arrays[0];
    for (int count = 1; count < size1; count++)
    {
        if(arrays[count] > large)
            large= arrays[count];
    }
    return large;
}

//**********************************************************************
// Function getSmallest definition. 
//  This function accepts an array argument and returns the smallest element.
//**********************************************************************
int getSmallest(int arrays[], int size2)
{
    // Find the smallest number in the array. 
    int small = arrays[0];
    for (count = 1; count < size2; count++)
    {
        if (arrays[count] < small)
            small = arrays[count];
    }
    return small;
}

Here are my errors:

777.cpp: In function ‘int getSmallest(int*, int)’:
777.cpp:62: error: overloaded function with no contextual type information
777.cpp:62: error: ‘size2’ cannot appear in a constant-expression
777.cpp:62: error: parse error in template argument list
777.cpp:62: error: could not convert ‘count<<expression error> >’ to ‘bool’
777.cpp:62: error: no post-increment operator for type
777.cpp:64: error: invalid types ‘int*[<unresolved overloaded function type>]’ for array subscript
777.cpp:65: error: invalid types ‘int*[<unresolved overloaded function type>]’ for array subscript


In getSmallest()

for (count = 1; count < size2; count++)

You forgot to declare count

for (int count = 1; count < size2; count++)


Here's your problem:

for (count = 1; count < size2; count++)

in your second method. You have forgotten the int:

for (int count = 1; count < size2; count++)


I get error on count in second function and it's because you didn't define it. Use this:

for (int count = 1; count < size2; count++) // int count; instead of count
    {
        if (arrays[count] < small)
            small = arrays[count];
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜