13 Replies - 4693 Views - Last Post: 26 April 2012 - 10:48 AM Rate Topic: -----

#1 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 07:29 PM

For instance, I know that inorder to find the overall highest number the code would look similar to this:
double getHighest ( double st [s][t])
{
	double highest = 0;
	for (int student = 0; student < s; student ++)
	{
		for (int test = 0; test < t; test ++)
		{
			if (st [student] [test] > highest)
			{
				highest = st [student] [test];
			}
		}
	}

	return highest;	
}



However, when it comes to finding the largest number in a column and/or row, I am confused.
I tried looking it up and doing little things like this:

double highest;

	for ( int test = 0; test < t ; test ++)
	{
		highest = 0; 

		for ( int student = 0 ; student < s ; student ++)
		{
			if ( st [test] > 0 )
			{
				highest = st [test] ; 
			}
		}

		
		cout << "The highest grade from test " << test + 1 << " is  a " << highest << "%. \n";



But I still cannot pinpoint it. Any help or guide is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about finding largest number in 2d array (column)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 08:39 PM

You still need to access the array using st [student] [test].
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 08:44 PM

To search through a single row, keep the row constant and iterate through the columns. To search a single column, iterate through the rows keeping the column constant.
Was This Post Helpful? 0
  • +
  • -

#4 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 08:51 PM

Sorry, I am new. How exactly would I iterate through the columns? Would I do that through my for loop?

double highest;

	for ( int test = 0; test < t ; test ++)
	{
		highest = 0; 

		for ( int student = 0 ; student < s ; student ++)
		{
			if ( st [student] [test] > 0 )
			{
				// Is there something I'm missing here now?
			}
		}

		
		cout << "The highest grade from test " << test + 1 << " is  a " << highest << "%. \n";


Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 08:55 PM

You iterate through part of the array the same way that you iterate through the entire array -- in for loops -- except that you keep either test or student constant.

If you think about it, you should realize that for these situations, you only need a single for loop (instead of nested for loops).

And by the way, when looking for the max or min of an array, instead of starting out setting your variable to 0, set it to the first actual value you find in the array.

This post has been edited by r.stiltskin: 24 April 2012 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 24 April 2012 - 09:19 PM

Look, you're still dealing with a 2D array, so you still have to refer to the elements as st[student][test].

But if you're only interested in looking at Student0's tests, you can declare
int student = 0;
and just iterate ("loop") through all of the possible values of test.

And if you're only interested in looking at all of the Students' scores on Test2, you can declare
int test = 2;
and iterate through all of the possible values of student.
Was This Post Helpful? 0
  • +
  • -

#7 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 25 April 2012 - 09:59 PM

So something like this?
void getHighestofEachTest (double st [s] [t])
{
	double highest;

	for ( int test = 0; test < t ; test ++)
	{
		highest = 0;
		int student = 0;
		
		if ( st [student] [test] > highest )
		{
			highest = st [student] [test];
		}
		
		cout << "The highest grade from test " << test + 1 << " is  a " << highest << "%. \n";
	}

	cout << "Press enter to continue. \n";
	cin.get();
}



It's still not calculating right, though.
I know it's probably something really simple.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 25 April 2012 - 10:14 PM

No. That is finding the highest test score only for Student 0.

If you want to find the highest grade for each test, your outer loop should iterate through the tests from 0 through t, so that will look at all of the tests, one at a time. But then you also need an inner loop that looks at all of the grades for each of those tests, so the inner loop will iterate through the students from 0 through s. See if you can figure out where to print the results.
Was This Post Helpful? 0
  • +
  • -

#9 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 09:47 AM

So, here's my code and an example output. The code is working for the 1st 3 tests, but the 4th one is not being calculated right. Thanks for your help!
void getHighestofEachTest (double st [s] [t])
{
	double highest = 0;

	for ( int test = 0; test < t ; test ++)
	{
		for (int student = 0; student < s; student ++)
		{
			 if ( highest < st [student] [test] )
			 {
				 highest = st [student] [test]; 
			 }
		}
		cout << "The highest grade from test " << test + 1 << " is  a " << highest << "%. \n";
	}

	cout << "Press enter to continue. \n";
	cin.get();
}


Enter 4 test scores for each student.

Student #1
-----------
Enter grade for test number 1: 78
Enter grade for test number 2: 89
Enter grade for test number 3: 54
Enter grade for test number 4: 38

Student #2
-----------
Enter grade for test number 1: 65
Enter grade for test number 2: 78
Enter grade for test number 3: 85
Enter grade for test number 4: 66

Student #3
-----------
Enter grade for test number 1: 33
Enter grade for test number 2: 35
Enter grade for test number 3: 99
Enter grade for test number 4: 56

Total from all tests is a 776%.
Press enter to continue.

Average of all tests is a 64.6667%.
Press enter to continue.

The class average for test 1 is  a 58.6667%.
The class average for test 2 is  a 67.3333%.
The class average for test 3 is  a 79.3333%.
The class average for test 4 is  a 53.3333%.
Press enter to continue.

The average for student 1 is  a 64.75%.
The average for student 2 is  a 73.5%.
The average for student 3 is  a 55.75%.
Press enter to continue.

The highest test score is a 99%.
Press enter to continue.

The highest grade from test 1 is  a 78%.
The highest grade from test 2 is  a 89%.
The highest grade from test 3 is  a 99%.
The highest grade from test 4 is  a 99%.
Press enter to continue.



Was This Post Helpful? 0
  • +
  • -

#10 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 09:59 AM

It seems like whenever I enter a 99% the highest grades for all tests become a 99, otherwise the code seems to work.. Any ideas why?

Nevermind, the code is off ):

OUTPUT:

Enter 4 test scores for each student.

Student #1
-----------
Enter grade for test number 1: 42
Enter grade for test number 2: 12
Enter grade for test number 3: 52
Enter grade for test number 4: 63

Student #2
-----------
Enter grade for test number 1: 65
Enter grade for test number 2: 21
Enter grade for test number 3: 45
Enter grade for test number 4: 25

Student #3
-----------
Enter grade for test number 1: 21
Enter grade for test number 2: 66
Enter grade for test number 3: 54
Enter grade for test number 4: 58

Total from all tests is a 524%.
Press enter to continue.

Average of all tests is a 43.6667%.
Press enter to continue.

The class average for test 1 is a 42.6667%.
The class average for test 2 is a 33%.
The class average for test 3 is a 50.3333%.
The class average for test 4 is a 48.6667%.
Press enter to continue.

The average for student 1 is a 42.25%.
The average for student 2 is a 39%.
The average for student 3 is a 49.75%.
Press enter to continue.

The highest test score is a 66%.
Press enter to continue.

The highest grade from test 1 is a 65%.
The highest grade from test 2 is a 66%.
The highest grade from test 3 is a 66%.
The highest grade from test 4 is a 66%.
Press enter to continue.
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 10:06 AM

That's because each time through the loop you're finding the highest score so far (cumulative for all tests). Do you see why?
Was This Post Helpful? 0
  • +
  • -

#12 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 10:33 AM

Moving this statement should do the job " cout << "The highest grade from test " << test + 1 << " is a " << highest << "%. \n";, correct?

Thank you so so so much rstiltskin for all your help

If I move it inside the if statement, it does not show up at all
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 10:37 AM

The problem isn't in where you're printing the results. You had that part right in Post #9.

But think -- if you want to find highest independently for each test, don't you have to reinitialize the variable each time?
Was This Post Helpful? 0
  • +
  • -

#14 sunny801  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-April 12

Re: Question about finding largest number in 2d array (column)

Posted 26 April 2012 - 10:48 AM

Of course!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1