[英]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. 这是一个问题:为名为findLargest的函数编写完整的函数定义,该函数接受int数组作为参数,并接受数组大小作为另一个参数。 The function should use a return statement to return the largest value in the array. 该函数应使用return语句返回数组中的最大值。
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. 如果有人可以提供正确解决方案的帮助,那将是很好的。
You could start with writing the function findLargest(Your parameters here)
first and then call that function with a preinitialized array (eg int myArray[10] = {1,2,3,4,5,6,7,9,0}; findLargest(myArray, 10)
) to test if it works. 您可以先编写函数findLargest(Your parameters here)
,然后使用findLargest(Your parameters here)
初始化的数组调用该函数(例如, int myArray[10] = {1,2,3,4,5,6,7,9,0}; findLargest(myArray, 10)
)进行测试。 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: 在C ++中,您不能执行以下操作:
int numArray [size];
as array dimensions must be compile-time constants, although if you are using GCC it may compile. 因为数组维必须是编译时常量,尽管如果您使用的是GCC,则可以编译。 Instead, investigate the use of the standard C++ feature, std::vector
. 相反,请研究标准C ++功能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. 为名为findLargest的函数编写完整的函数定义,该函数接受int数组作为参数,并接受数组大小作为另一个参数。
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()
: 首先,分配非常清楚,您必须为此任务定义一个函数,并且不能在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.