Why is this function not working? Sigh...

This is a discussion on Why is this function not working? Sigh... within the C++ Programming forums, part of the General Programming Boards category; Why is this function not working? Sigh...I am getting a warning....warning C4700: local variable 'lowest' used without having been initialized...but ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Why is this function not working? Sigh...

    Why is this function not working? Sigh...I am getting a warning....warning C4700: local variable 'lowest' used without having been initialized...but I did initialize it at the start...

    Code:
    double average(int array[], int numts)
    {
    	int lowest, total = 0, total2 = 0; 
    	double avg;
    
    	for (int count = 0; count < numts; count++)
    	
    		total += array[count];
    
    		total2 = total - lowest;
    
    		avg = total2 / numts;
    
    	  return avg;
    
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Because you are doing just what the compiler is telling you that you're doing Try setting lowest to some value, say 0.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > local variable 'lowest' used without having been initialized...but I did initialize it at the start...
    No, you initialised the two totals.
    lowest is most assuredly NOT initialised in the code you posted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by musique View Post
    ...but I did initialize it at the start...
    no, you didnt. 99.9...% of the time compilers are correct and tell you exactly what the problem is, where it is; they are incredibly "intelligent".

    you declared the variable, but didnt assign it a value. also, since this variable never changes, why not make it a constant or simply hard code the value in it, if it is not going to change?

    edit: also, i imagine your for loop is not what you intend. the for loop will only execute the next statement, which in this case is the "total +=..." line. you probably want to surround the indented code with a pair of curly braces
    Last edited by nadroj; 05-03-2009 at 04:26 PM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Thank you, it took away the warning but it is still not working, do you know why? I am trying to get the average of the test scores after removing the lowest value.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I am new to c++, but I understand what you guys mean concerning the initialization...I changed it to...still not working though, see any other errors in it?

    Code:
    double average(int array[], int numts)
    {
    	int lowest = 0, total = 0; 
    	double avg;
    
    	for (int count = 0; count < numts; count++)
    	
    		total += array[count];
    
    		avg = (total - lowest) / numts;
    
    	  return avg;
    
    }

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by nadroj View Post
    edit: also, i imagine your for loop is not what you intend. the for loop will only execute the next statement, which in this case is the "total +=..." line. you probably want to surround the indented code with a pair of curly braces
    Quote Originally Posted by nadroj View Post
    also, since this variable never changes, why not make it a constant or simply hard code the value in it, if it is not going to change?
    for any number X, X - 0 = X. the number 0 is the "identity" for subtraction: it has no effect. so there is no point in doing "total - lowest" in your code, when lowest is the constant value of 0.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I believe I see what you are saying...But before, when I had no zero, I was getting a warning that lowest wasn't initialized? What else can I initialize zero with?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im saying there is no point in doing the calculation "total - lowest" because since "lowest" is a constant value of zero, the calculation can be simplified to exclude the "- lowest" part. did you fix your initial problem of not having curly braces?

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Yes I did (assuming I did it right), I changed it to...

    Code:
    double average(int array[], int numts)
    {
    	int total = 0; 
    	int lowest = 0;
    	double avg;
    
    	for (int count = 0; count < numts; count++)
    	{
    		total +=array[count];
    	}
    
    	avg = (total - lowest) / numts;
    
    	  return avg;
    }

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what you have will return the average of all of the numbers, but you said you wanted to return the average excluding the lowest value. that means in your for loop you need logic to determine the minimum in the array. after you have that set, the rest of the function is almost correct. i dont understand why you want the average excluding the lowest value, but if thats the case then you need to divide by (numts - 1), not just numts; an average is the sum of the numbers divided by how many numbers used in the sum. since you are excluding a number, you use numts-1 numbers, not numts. you must handle the case when the array is only 1 number, in which case you cant divide by numts -1 = 0.

    there is really only 1 way to determine the minimum value from an array of unsorted numbers, and you can do it in your loop you already have. search around for hints if you need it, as it is a common exercise for beginners so there should be a lot of examples.
    Last edited by nadroj; 05-03-2009 at 05:13 PM.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    This is my full program as it is now...the last function is to remove a student's lowest testscore and then calculate an average, I am thinking that I should compare the testscores to find the lowest but hasn't the selection sort function done that? I even tried returning the "lowest" variable in order to remove it but it didn't work then either...

    Code:
    /***This program dynamically allocates an array large enough to
    hold a user-defined number of test scores***/
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Function prototypes
    int getnumtestscores();
    void gettestscores(int[], int);
    void showArray(int [], int);
    void sort(int [], int);
    double average(int[], int);
    
    
    int main()
    {
    	int numts = getnumtestscores();
    
    	int *testscores = new int[numts];		//To dynamically allocate an array
    
    	gettestscores(testscores, numts);
    
    	cout << "The testscores are: \n"; showArray (testscores, numts);
    
    	cout << "The testscores sorted are: \n"; sort (testscores, numts);
    
    	showArray (testscores, numts);
    
        cout << "The average of all " << numts << " test scores is: " 
             << fixed << showpoint << setprecision(2)
             << average(testscores, numts);
    	cout << endl;
    
        // Free dynamically allocated array in memory
        delete [] testscores;
        // Make scores point to null
        testscores = 0;
    
    	return 0;
    }
    
    int getnumtestscores()
    {
        int num;
    
        cout << "\n How many test scores do you want to enter : ";
        for(;;) // loop forever  ... until return
        { 
            cin >> num;
         
            return num;
        }  
    }
    
    void gettestscores(int testscores[], int numts)
    {
    	cout << "What are the testscores for the students?\n";
    
    	for (int count = 0; count < numts; count++)
    	{
    		cout << "Testscore " << setw(2) << (count+1) << ":";
    
    		if (testscores < 0)		//Input validation
    		{
    			cout << "An invalid score was entered, please enter a score more than 0\n";
    		}
    
    		cin >> testscores[count];
    	}
    }
    
    void sort(int array[], int numts)
    {
    	int lowIndex, lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (array[index] < lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}
    		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    	}
    
    	return;
    }
    
    void showArray(int array[], int numts)
    {
    	for (int count = 0; count < numts; count++)
    		cout << array[count] <<" \n";
    	cout << endl;
    }
    
    double average(int array[], int numts)
    {
    	int total = 0; 
    	int lowest = 0;
    	double avg;
    
    	for (int count = 0; count < numts; count++)
    	{
    		total +=array[count];
    	}
    
    	avg = (total - lowest) / (numts-1);
    
    	  return avg;
    }

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im not sure what you mean about selection sort or returning lowest, in your description of your last post, but oh well.

    aside, in your getnumtestscores you dont need to have a loop, as in the first iteration it simply returns, so in every run of the program that for loop has only one iteration, so there is no point of having a loop. that function therfore only has 2 lines of code, which could easily be put into main directly to ask for the number and get it. if you were doing validation of the input (ie a user shouldnt be able to enter 'x' as their input, which you blindly save), then it would be good to have a separate function as you have now, and validate the input, and loop while the input is not valid. so either remove the loop and unnecessary function (no validation) or keep the function, fix the loop to add validation until they enter a positive non-zero integer (validation). a similar case would arise in other cases you get input of numbers.

    oh! i just noticed your sort function! this is very difficult to spot because you have more than one statement on a line, at the top of main where you have sort statement and another statement on the same line: one statement = one line, you should follow that. if you sort your list in ascending order then you dont have to find the minimum, you know it is (assuming you correctly implemented sort, which im not checking ), you know the minimum is always the 0th index of your array. since you have this, change your average function to skip this score, that is, start summing at the 1th index, not 0th, and divide by numts-1.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    sorry, I made an error there, this is what it is now...

    Code:
    /***This program dynamically allocates an array large enough to
    hold a user-defined number of test scores***/
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Function prototypes
    int getnumtestscores();
    void gettestscores(int[], int);
    void showArray(int [], int);
    void sort(int [], int);
    double average(int[], int);
    
    
    int main()
    {
    	int numts = getnumtestscores();
    
    	int *testscores = new int[numts];		//To dynamically allocate an array
    
    	gettestscores(testscores, numts);
    
    	cout << "The testscores are: \n"; showArray (testscores, numts);
    
    	cout << "The testscores sorted are: \n"; sort (testscores, numts);
    
    	showArray (testscores, numts);
    
        cout << "The average of all " << numts << " test scores is: " 
             << fixed << showpoint << setprecision(2)
             << average(testscores, numts);
    	cout << endl;
    
        // Free dynamically allocated array in memory
        delete [] testscores;
        // Make scores point to null
        testscores = 0;
    
    	return 0;
    }
    
    int getnumtestscores()
    {
        int num;
    
        cout << "\n How many test scores do you want to enter : "; 
        cin >> num;
         
        return num;
       
    }
    
    void gettestscores(int testscores[], int numts)
    {
    	cout << "What are the testscores for the students?\n";
    
    	for (int count = 0; count < numts; count++)
    	{
    		cout << "Testscore " << setw(2) << (count+1) << ":";
    
    		if (testscores < 0)		//Input validation
    		{
    			cout << "An invalid score was entered, please enter a score more than 0\n";
    		}
    
    		cin >> testscores[count];
    	}
    }
    
    void sort(int array[], int numts)
    {
    	int lowIndex, lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (array[index] < lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}
    		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    	}
    
    	return;
    }
    
    void showArray(int array[], int numts)
    {
    	for (int count = 0; count < numts; count++)
    		cout << array[count] <<" \n";
    	cout << endl;
    }
    
    double average(int array[], int numts)
    {
    	double total = 0;
    
    	for (int count = 0; count < numts; count++)
    	
    		total +=array[count];
    
    	  return total/numts;
    }

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im not going to reread the code unless you specifically point out what is different, let me know if you have questions about my previous post, as i think you can solve it from that.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21