Thread: Problem with 2D array

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

    Cool Problem with 2D array

    I don't understand logically what is causing this too happen, but the "Dropped Grade" & "Average" are always coming out as "-858993460" & "286331275.00".

    The program is suppose to have you enter 4 scores then find the lowest score & then calculate the average & then display the lowest score & average. Then it loops back and starts on a new student and repeats the process 3 times, but the outcomes of average & lowest come out all wrong.

    If someone could please explain what logically is going wrong that would be great & what I should do differently.

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    void getData(int score[][4], int rows, int &low, double &avg)
    {
    	for(int R=0; R < rows; R++)
    	{
    		int TOT=0;
    		low = score[R][0];
    
    		for(int C=0; C < 4; C++)
    		{
    			do
    			{
    				cout << "Enter test score " << (C+1) << " for student " << (R+1) << ": ";
    				cin >> score[R][C];
    			}while(score[R][C] < 0 || score[R][C] > 100);
    
    			TOT += score[R][C];
    
    			if (score[R][C] < low)
    				low = score[R][C];
    		}
    
    		avg = (TOT - low) / 3.0;
    
    		cout << fixed << setprecision(2);
    		cout << endl;
    		cout << "Dropped Grade: " << low << endl;
    		cout << "Average: " << avg << endl;
    	}
    }
    
    int main()
    {
    	int sc[3][4], low;
    	double avg;
    
    	getData(sc, 3, low, avg);
    	return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One thing that catches the idea is that low is set to an uninitialized value.

    Code:
    low = score[R][0];
    One possibility, since 100 is the maximum score, is low = 100.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8 Queens, problem with searching 2D array
    By Sentral in forum C++ Programming
    Replies: 35
    Last Post: 03-11-2009, 04:12 PM
  2. Problem with a 2D array.
    By earth_angel in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2005, 11:28 AM
  3. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  4. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM
  5. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 02:23 PM