2015-12-09 2 views
-1

Я использую Visual Studios Express 2013, и он продолжает давать мне ошибку, говоря, что режим переменной используется без инициализации, но инициализируется. Как вы можете видеть, я закончил все, и я понятия не имею, что я сделал неправильно. Поэтому, пожалуйста, мне нужна ваша помощь.Он продолжает говорить, что переменный режим используется без инициализации

Это код, вызывающий проблему

  double getMode(int *array, int size) 
     { 
      int Mode, Most, Count; 
      Count = Most = 0; 

      for (int i = 0; i < size; i++) 
      { 
       Count++; 
       if (*(array + i) < *(array + i + 1)) 
       { 
        if (Count > Most) 
        { 
         Mode = *(array + i); 
         Most = Count; 
        } 
        Count = 0; 
       } 
      } 
      return Mode; 
     } 

Вся программа

#include <iostream> 
#include <iomanip> 
using namespace std; 

// Function prototype 
void getData(int *, int); 
void selectionSort(int *, int); 
double getAvg(int *, int); 
double getMedian(int *, int); 
int getMode(int *, int); 

int main() 
{ 
    int *Movies, Stud, Mode; 
    double Avg, Med; 

    // Ask user how many students were surveyed. 
    cout << "How many students were surveyed? "; 
    cin >> Stud; 

    Movies = new int[Stud]; 

    getData(Movies, Stud); 

    selectionSort(Movies, Stud); 

    Avg = getAvg(Movies, Stud); 

    Med = getMedian(Movies, Stud); 

    Mode = getMode(Movies, Stud); 

    cout << "Statistical results for the number of movies\n" 
     << "  college students see in a month.\n"; 
    cout << fixed << showpoint << setprecision(2); 
    cout << "Average: " << Avg << endl; 
    cout << "Median: " << Med << endl; 
    cout << "Mode: " << Mode << endl; 

    delete[] Movies; 
    Movies = 0; 

    return 0; 
} 

//******************************************************************************* 
//         getData          * 
// The function asks the user to enter the survey data and stores it in an  * 
// integer array. The size parameter holds the size of the array.    * 
//******************************************************************************* 
void getData(int *array, int size) 
{ 
    cout << "Enter the number of movies each student has seen in the month.\n"; 
    for (int i = 0; i < size; i++) 
    { 
     cout << "Student " << (i + 1) << ": "; 
     cin >> *(array + i); 
    } 
} 

//******************************************************************************* 
//         selectionSort        * 
// This function performs an ascending-order selection sort on array. The  * 
// parameter size holds the number of elements in the array.     * 
//******************************************************************************* 
void selectionSort(int *array, int size) 
{ 
    int scan, minIndex, minValue; 

    for (int scan = 0; scan < (size - 1); scan++) 
    { 
     minIndex = scan; 
     minValue = *(array + scan); 
     for (int i = scan + 1; i < size; i++) 
     { 
      if (*(array + i) < minValue) 
      { 
       minValue = *(array + i); 
       minIndex = i; 
      } 
     } 
     *(array + minIndex) = *(array + scan); 
     *(array + scan) = minValue; 
    } 
} 

//******************************************************************************* 
//         getAvg          * 
// This function calculates and returns the average of the values in array. The * 
// parameter size holds the number of elements in array.      * 
//******************************************************************************* 
double getAvg(int *array, int size) 
{ 
    double Sum = 0; 
    for (int i = 0; i < size; i++) 
    { 
     Sum += *(array + i); 
    } 
    return Sum/size; 
} 

//******************************************************************************* 
//         getMedian         * 
// This function calculates and returns the median of the values in the array. * 
// The parameter size holds the number of elements in the array.    * 
//******************************************************************************* 
double getMedian(int *array, int size) 
{ 
    int Mid = (size - 1)/2; 
    double Med; 

    if (size % 2 == 0) 
    { 
     Med = (*(array + Mid) + *(array + (Mid + 1)))/2; 
    } 
    else 
     Med = *(array + Mid); 

    return Med; 
} 

//******************************************************************************* 
//         getMode          * 
// This function calculates and returns the mode of the values in the array. * 
// The parameter size holds the number of elements in the array.    * 
//******************************************************************************* 
int getMode(int *array, int size) 
{ 
    int Mode, Most, Count; 
    Count = Most = 0; 

    for (int i = 0; i < size; i++) 
    { 
     Count++; 
     if (*(array + i) < *(array + i + 1)) 
     { 
      if (Count > Most) 
      { 
       Mode = *(array + i); 
       Most = Count; 
      } 
      Count = 0; 
     } 
    } 
    return Mode; 
} 
+0

В функции 'getMode', что произойдет, если' size' равен нулю или меньше? –

+0

Функция 'getMode' может не устанавливать режим в каких-либо обстоятельствах. – ajshort

+1

Также вы прочтете один конец конца массива в этой строке: 'if (* (array + i) <* (array + i + 1))'. Что, если i == (размер - 1)? –

ответ

0

Сделать контрольную точку на этой функции. Вы можете видеть, что в if (*(array + i) < *(array + i + 1)) иногда оно возвращает значение false, поэтому режим не инициализируется.

+0

Когда я делаю точку останова в if (* (массив + i) <* (массив + i + 1)), программа просто останавливается там и не будет продолжать. – aqibu

+0

Положите точку останова в точку, а затем нажмите F10, чтобы показать поток процесс. –