1

So we were given a project involving two arrays (one a string and the other including values), and I decided to use movies and years. One of the parameters of the project is to display the maximum and minimum values along with their string. Now, the max works fine, but when I try to run min, it says it isn't initialized. What am I doing wrong?

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

int avgYear(int arr[], int size);
int indexMin(int arr[], int size);
int indexMax(int arr[], int size);

int main()
{
    int total = 0;

    string name[] = {"Toy Story", "A Bug's Life", "Toy Story 2", "Monster's Inc.", "Finding Nemo", "The Incredibles", "Cars", "Ratatouille", "WALL-E", "Up"};
    int year[] = { 1995, 1998, 1999, 2001, 2003, 2004, 2006, 2007, 2008, 2009};
    for (int x = 0; x < 10; x++)
        cout << name[x] << "     " << year[x] << endl;

    cout << "The average year of release was " << avgYear(year, 10) << endl;
    cout << "The First Year of Release was " << name[indexMin(year, 10)] << " in " << year[indexMin(year, 10)] << endl;
    cout << "The Last Year of Release was "<< name[indexMax(year, 10)] << " in " << year[indexMax(year, 10)] << endl;


    return 0;
}

int avgYear(int arr[], int size)
{
    int avg;
    int total=0;
    for (int x = 0; x < size; x++)
        total += arr[x];
    avg = total / size;

    return avg;
}

int indexMin(int arr[], int size)
{
    int iMin;
    int min = arr[0];
    for (int x = 1; x < size; x++)
        if (arr[x] < min)
        {
            min = arr[0];
            iMin = x;
        }
    return iMin;
}

int indexMax(int arr[], int size)
{
    int iMax;
    int max = arr[0];
    for (int x = 0; x < size; x++)
        if (arr[x] > max)
        {
            max = arr[x];
            iMax = x;
        }
    return iMax;
}   
Rebeckah
  • 11
  • 1

2 Answers2

0

If minimum value is arr[0] then iMin won't ever be initialized because if (arr[x] < min) will never return true. You max function also has the same problem but works because max element is not at index 0.

int iMin = 0;

Should fix your problem. Also it's a good idea to develop a habit to always initialize your variables and fields. Value stored in an uninitialized variable is indeterminate, reading from it is undefined behaviour.

Community
  • 1
  • 1
Rinat Veliakhmedov
  • 1,021
  • 1
  • 19
  • 36
  • Oh thank you, I really appreciate. I've been staring at this project for hours. Also, thanks for explaining why, it was really helpful! – Rebeckah May 04 '17 at 02:39
0

if you initialize years like this:

int year[] = { 2009, 2008, 2007, 2006, 2004, 2003, 2001, 1999, 1998, 1995};

and then, the min work fine, and the max is error.^_^

you must initialize the iMin and iMax, and the initial number need to be the same with arr index, like this:

// min
int nStartIndex = 0;
int iMin = nStartIndex;
int min = arr[nStartIndex];
// max
int nStartIndex = 0;
int iMax = nStartIndex;
int max = arr[nStartIndex];
上山老人
  • 412
  • 4
  • 12