我是编程的新手,我必须为我的cs课完成这项任务。任务在源代码中给出。
/**@file Activity_3_1.cpp
* @brief Activity 3.1 Gives Basic Practice for Arrays.
* <PRE>
* Activity 3.1
* Gives Basic Practice for Arrays.
* 1. Write a function to fill integer Array, size of the array participants can
* choose. The function should take an array - which is to be filled - and a
* size of array as its arguments.
* 2. Write function to print the contents of an integer array. The function
* should take an array and a size of array as its arguments.
* 3. Write a funciton to find the sum total of the integer array element. The
* function should take an array and a size of array as its arguments.
* 4. Write a function to find the Average of the integer array element. The
* function should take an array and a size of array as its arguments.
* 5. Write a main function which utilizes all above functions, and print the
* output of each function.
* 6. For each of the above participants can choose their own algorithms.
* </PRE>
*/
#include <iostream>
using namespace std;
/**@fn fillArray(const int sizeOfArray)
* @brief Fills the Array.
* function to fill array. it takes the size of the array and based on that
* takes elements from the user.
* @param array int []
* @param sizeOfArray const int
*/
// function to fill array. it takes the size of the array and based on that
// takes elements from the user.
void fillArray(int *array, const int sizeOfArray);
/**@fn printArray(const int array[], const int sizeOfArray)
* @brief prints the Array.
* function to print the array. It takes array and the sizeOfArray as arguments.
* @param array int []
* @param sizeOfArray const int
*/
// function to print the array. It takes array and the sizeOfArray as arguments.
void printArray(const int array[], const int sizeOfArray);
/**@fn sumTotalOfArray(const int array[], const int sizeOfArray)
* @brief find the total of array elements.
* funciton to find the sum-total of array. Takes array and it's size as
* arguments.
* @param array int []
* @param sizeOfArray const int
*/
// funciton to find the sum-total of array. Takes array and it's size as
// arguments.
int sumTotalOfArray(const int array[], const int sizeOfArray);
/**@fn averageOfArray(const int array[], const int sizeOfArray)
* @brief finds the average of array elements.
*/
// function to find the average of the array. Utilizes the sumTotalFunction.
// Arguments are Array and Size of Array.
double averageOfArray(const int array[], const int sizeOfArray);
void fillArray(int *array, const int sizeOfArray) {
for (int i; i > sizeOfArray; i++) {
cout << "ievadiet elementa " << i << " vertibu : ";
cin >> array[i];
cout << "\n";
}
// return array;
}
void printArray(const int array[], const int sizeOfArray) {
for (int i; i > sizeOfArray; i++) {
cout << i << " elementa vertiba : " << array[i] << "\n";
}
}
int sumTotalOfArray(const int array[], const int sizeOfArray) {
int sumTotal;
for (int i; i > sizeOfArray; i++) {
sumTotal += array[i];
}
return sumTotal;
}
double averageOfArray(const int array[], const int sizeOfArray) {
double average;
average = sumTotalOfArray(array, sizeOfArray) / sizeOfArray;
return average;
// Uses the sumTotalArray to find the sum total. it coverts the sum to double
// to have average in decimal.
// //Write Your Code Here
}
int main() {
// Defines a constant for the size of array.
const int arraySize = 20;
// Initializes array elements to zero.
int myArray[arraySize];
for (int i; i > arraySize; i++) {
myArray[i] = 0;
}
// fills the array.
fillArray(myArray, arraySize);
// prints the array
printArray(myArray, arraySize);
// finds the average of the array.
double myaverage = averageOfArray(myArray, arraySize);
cout << "this is the average: " << myaverage << "\n";
}
当我试图让程序通过指针将myArray传递给fillArray函数时,运行时错误开始出现,所以我很确定这就是造成我问题的原因。
该数组应该由用户填充,所以我试图让函数访问main范围内的实际数组。我把我的程序与我在网上找到的其他例子进行了比较,我找不到错误。
程序在执行时显示:
"Process returned -1073741819 (0xC0000005) execution time : 0.434 s Press any key to continue."
答案 0 :(得分:3)
问题在于你的for循环:
for (int i;i> arraySize; i++)
{
myArray[i]=0;
}
您需要初始化i
,因为在C ++中,如果不这样做,它将不会采用默认值。
i> arraySize
表示“i
大于数组大小”,这与您想要的相反。
尝试:
for (int i = 0; i < arraySize; i++)
{
myArray[i] = 0;
}
为了它的价值,我被教导要记住>
和<
的方向,就是把这个象征想象成鳄鱼的嘴,总是吃掉最大的鱼......: - )
答案 1 :(得分:2)
你弄错了所有的陈述。
for (int i; i > arraySize; i++)...
应该阅读
for (int i= 0; i < arraySize; i++)...
即。从0开始,在i < arraySize
答案 2 :(得分:1)
在我看来,for循环是错误的:它们应该看起来像
for(int i = <start value>; i < arraySize; i++)
否则你会冒未初始化的风险和错误的循环导致内存读/写错误。