我不知道自己遇到了什么问题。我建立没有错误,一切都很好。老实说,我得到了这个程序。但是,我的教授不允许全局变量。所以,我将变量放入main函数和其他void函数中。但是,当我运行它时,它会说" program.exe已停止工作"。我不知道如何解决它。我可能会在某处错误地使用指针,或者我可能错误地计算了标准偏差。我不知道。任何帮助将不胜感激。非常感谢你 !!! 这是我的inputFile" scores.txt" :
46
85 100 90 85 97 92 72 88 79 86 97 89 67 96 84 96 93 77 56 77 85 100 84 92 88 67 97 86 95 94 73 68 76 80 99 78 87 96 85 64 93 81 92 93 74 65
这是我的代码:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int SIZE =46; // This is the maximum student's scores
// Here is my function prototyoe
void readScores ( int array[], int *pLast);
void insertionSort( int *ptr, int SIZE);
void displayArray ( int array[], int *pLast);
double calAverage ( int array[], int *pLast);
int *calLowest ( int array[], int *pLast);
int *calHighest ( int array[], int *pLast);
double calStandDevi (int array[], int *pLast);
void printResult ( int array[], int *pLast);
int main()
{
int num_scores;
int array[SIZE];
int *ptr = array; // Here is my pointer that point to the array
int *pLast = array + SIZE-1; // Here is my last pointer.
// I need to initialize the average, lowest, highest scores and stand deviation so that I can call my function.
double avg, stand_deviation;
int *get_lowest, *get_highest;
// to open, work and save my file. I need to initialize this.
ifstream inputFile;
ofstream outputFile;
cout << "Welcome to my third program!!!" << endl;
cout << endl;
// Here is my eight function call.
readScores( array, pLast);
insertionSort(&array[0],SIZE);
displayArray(array,pLast);
avg = calAverage(array,pLast);
get_lowest = calLowest(array,pLast);
get_highest = calHighest(array,pLast);
stand_deviation = calStandDevi(array,pLast);
printResult( array, pLast);
//cout << num_scores << endl; // just the test if I read the correct scores from the inputfile
return 0;
}
// Here is my first function definition which is read scores from the input file.
void readScores ( int array[], int *pLast )
{
int num_scores;
ifstream inputFile;
inputFile.open("scores.txt"); // I open the file.
inputFile >> num_scores; // I read the file.
for ( int *ptr = array; ptr <= pLast; ptr++)
{
inputFile >> *ptr;
}
inputFile.close(); // I close the file.
}
// Here is my second function definition to sort the array in ascending order using Insertion Sort.
void insertionSort(int *ptr, int SIZE)
{
for (int curr =1; curr < SIZE; curr++)
{
int hold = *(ptr+curr);
int back = *(ptr-1+curr);
while( hold < *(ptr-1+curr) && (curr > 0))
{
*(ptr+curr)= *(ptr-1+curr);
curr--;
}
*(ptr+curr) = hold;
}
}
// Here is my third function definition which sorted array 10 numbers per line.
void displayArray ( int array[], int *pLast)
{
int count =0;
for (int *ptr = array; ptr <= pLast; ptr++)
{
count++;
cout << *ptr << " ";
if ( count % 10 == 0)
{
cout << endl;
}
}
cout << endl;
}
// Here is my fourth function definition which calculate the average.
double calAverage( int array[], int *pLast)
{
double sum1 =0;
double avg;
for ( int *ptr = array; ptr <= pLast; ptr++)
{
sum1 += *ptr;
avg = sum1/SIZE;
}
return avg;
}
// Here is my fifth function definition which calculate the lowest score.
int *calLowest ( int array[], int *pLast)
{
int *get_lowest = array;
for ( int *ptr = array; ptr <= pLast; ptr++)
{
if(*get_lowest > *ptr)
get_lowest = ptr;
}
return get_lowest;
}
// Here is my sixth function definition which calculate the highest score.
int *calHighest ( int array[], int *pLast)
{
int *get_highest = array;
for ( int *ptr = array; ptr <= pLast; ptr++)
{
if ( *get_highest < *ptr)
get_highest=ptr;
}
return get_highest;
}
// Here is my seventh definition which calculate the stand deviation
double calStandDevi (int array[], int *pLast)
{
double sum2=0;
double avg;
double sum_deviation=0.0;
double stand_deviation = 0.0;
for ( int *ptr = array; ptr <= pLast; ptr++)
{
sum2 += *ptr;
avg = sum2/SIZE;
sum_deviation += pow((*ptr-avg), 2.0);
stand_deviation = sqrt(sum_deviation/SIZE-1);
}
return stand_deviation;
}
// Here is my last function definition which print the result.
void printResult ( int array[], int *pLast)
{
double avg, stand_deviation;
int *get_lowest, *get_highest;
cout << "The average score is " << avg << endl;
cout << "The lowest score is " << *get_lowest << endl;
cout << "The highest score is " << *get_highest << endl;
cout << "The standard deviation is " << stand_deviation << endl;
cout << endl;
cout << "Have a great day!!!" << endl;
}
答案 0 :(得分:5)
tl; dr:您的局部变量不是量子纠缠的!
在将全局变量定义移动到使用这些变量的函数之后,您继续将现在局部变量视为全局变量,在一个函数中写入它们,但在另一个函数中读取它们。
但那些是不同的变量。
以下是您将遇到严重问题的一个特别明显的例子:
void printResult ( int array[], int *pLast)
{
double avg, stand_deviation;
int *get_lowest, *get_highest;
cout << "The average score is " << avg << endl;
cout << "The lowest score is " << *get_lowest << endl;
cout << "The highest score is " << *get_highest << endl;
cout << "The standard deviation is " << stand_deviation << endl;
cout << endl;
cout << "Have a great day!!!" << endl;
}
你声明了,未初始化,指针get_lowest
和指针get_highest
,然后立即取消引用它们,从不让它们指向任何有用的地方。对此没有任何效力。
在这里阅读来自avg
和stand_deviation
也是非法的,这些内容也完全没有初始化,没有任何价值。但是,这不太可能导致像你一样的崩溃;通常你只是从他们那里得到随机的数字。
您的教授告诉您不要使用全局变量,因为他希望您考虑如何使用参数来传递程序周围的信息。你不能将局部变量看作是全局变量,神奇地与具有相似外观名称的不同范围内的其他局部变量共享状态;如果可以的话,就没有必要告诉你不要使用全局变量!