#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Question about error in Function program


    Hello,

    So I followed this tutorial today:

    And everything looks pretty verbatim, plus I notated every single part of the program.

    The only "warning" I'm getting in Visual C++ 2008 is that the ' largest' local variable is uninitialized.

    Anyone know why I'm getting this warning?

    The program runs, but after I input 10 numbers, it pretty much crashes. I declared largest as double in the beginning of the main part, so I don't understand how it wasn't initialized.. Hmm...

    The warning is from the line in red.

    Here's my code:

    #include <iostream>

    using namespace std;

    void userInput(double array[]);

    // This is a function that has a double array, that will not return its value

    double findLargest(double largest, double arr[]);

    // This will contain an array of 10 elements and we'll use the second function to
    // find the largest element in the array

    int main ()
    {

    const int size = 10;

    // This is a constant used to maintain only 10 elements within the array

    double arr[size];

    // This creates a double array with only 10 elements from the size variable

    double largest;

    // This variable will store the answer for the largest number in the array

    userInput(arr);

    // This calls the userInput function to obtain an array of values from the user

    cout << "The largest number you entered was: " << findLargest(largest, arr) << endl;

    // This calls the second function to find the largest number in the array

    return 0;


    }

    void userInput(double arr[])
    {
    cout << "Please enter 10 numbers" << endl;

    for(int i=0; i<10; i++)
    {
    cin >> arr[i];

    }

    // This will start the array at 0 and end at the 9th element

    }

    double findLargest(double largest, double arr[])
    {

    // This is the findLargest function that will take in the largest number,
    // and all the elements of the array

    for(int i=0; i<10; i++)
    {

    // i will begin at zero, and then reach 9 after being added to 1 each
    // time through the for loop


    if (arr[i] > arr[i+1] && arr[i] > largest)

    // This is the condition if the array element is greater than i + 1,
    // and the array element is greater than the largest element

    { largest = arr[i];

    }

    else
    {
    if(arr[i] < arr[i+1] && arr[i+1] > largest)
    {

    largest = arr[i+1];

    }
    }
    }

    return largest;

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Oops, here's the video link:

    http://www.youtube.com/watch?v=8s7Oqcztzqs
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Nevermind, a user within the comments section had the same error, forgive me for posting guys!

    Apparently, I have to set double largest = 0.0;

    and then it worked fine.

    It might be just a semantics issue, since the tutorial maker used Netbeans instead of Visual C++, and he did exactly the same code as I posted above, and it totally worked.

    Visual C++ is finicky haha.

    Have a good one!
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Originally Posted by HauntJemimah
    Nevermind, a user within the comments section had the same error, forgive me for posting guys!

    Apparently, I have to set double largest = 0.0;

    and then it worked fine.

    It might be just a semantics issue, since the tutorial maker used Netbeans instead of Visual C++, and he did exactly the same code as I posted above, and it totally worked.

    Visual C++ is finicky haha.

    Have a good one!
    Actually, it is not Visual C++ that is finicky. It is doing the correct thing by warning you of a potential problem. Uninitialized variables could have any value in them. I bet the guy who made the tutorial never wrote production quality code, because it would have given him the wrong answer from time to time (because there is no guarantee that the value of largest will be set to 0 initially, unless you explicitly set it.)

    Personally, instead of setting it to 0, I would set it like this:
    Code:
    #include <limits.h>
    
    double largest = -DBL_MAX;
    This way, largest is set to the minimum possible double value of your compiler.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by Scorpions4ever
    Actually, it is not Visual C++ that is finicky. It is doing the correct thing by warning you of a potential problem. Uninitialized variables could have any value in them. I bet the guy who made the tutorial never wrote production quality code, because it would have given him the wrong answer from time to time (because there is no guarantee that the value of largest will be set to 0 initially, unless you explicitly set it.)

    Personally, instead of setting it to 0, I would set it like this:
    Code:
    #include <limits.h>
    
    double largest = -DBL_MAX;
    This way, largest is set to the minimum possible double value of your compiler.
    Cool, its always fun to learn new libraries, I'll add it to the top of the code and also add the revision to the variable, thanks a bunch man!
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Originally Posted by HauntJemimah
    Code:
    	for(int i=0; i<10; i++)
    	{
    
    		// i will begin at zero, and then reach 9 after being added to 1 each
    		// time through the for loop
    
    		if (arr[i] > arr[i+1] && arr[i] > largest)
    When i == 9 you are checking the 10th (and last) element of the array so i + 1 == 10 which is the 11th element of the array and so out of bounds of your 10 wide array.

    However if you are building / running a debug version then the IDE will (probably) have padded the array with debug info and so hidden this error.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Ah I see.

    So it does go to 11, but since the value is invalid due to the argument, the program exits the loop.

    Interesting.

    I'm actually making a Text Based adventure game as a side project given what I know now, to reference different functions that just store 5 different events that happen to the player.

    Obviously, its going to pretty stupid to the average user, but it seems like a fun thing to tackle though.

    I'll post what I have done so far tomorrow, for I had just slight trouble calling each function that only held 1 event the player went through which would lead to just a "y or n" option to either continue or die haha.

IMN logo majestic logo threadwatch logo seochat tools logo