# third largest number 2d arrays

• (2 Pages)
• 1
• 2

## 20 Replies - 2560 Views - Last Post: 26 February 2013 - 07:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313248&amp;s=12a28ee7fb65859cdf33580249a0aa74&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

# third largest number 2d arrays

Posted 23 February 2013 - 01:14 PM

my professor has asked us to solve the following problem but im having trouble understanding it

**Write a function that takes in a 2 dimensional array of integers and returns the third largest
number. Let the number of columns of your 2 dimensional array be 5. Call this function from
the main program with 2 different arrays to show that it works.
**

Ive tried to make sense of it and create a code but i am not sure how to call the function in the main function. i tried to create a variable int a[][5], which gave me an error saying that that the first bracket can't be empty. He doesn't specify the number of rows, so should it be 1, by default?

I am trying to understand a function he used in class as example. This was a code to find the second maximum class
which is

```int SecondMax2D(int a[][5], int rows)
{
int max1;
int max2;

if (a[0][0]>a[0][1])
{
max1= a[0][0];
max2=a[0][1];
}
else
{
max1=a[0][1];
max2=a[0][0];
}

for (int row=0; row<rows; row++)
{
for (int col=0; col<10; col++)
{
if(row = = 0 && (col == 0 || col ==1))
continue;
if (a[row][col]> max1)
{
max2=max1;
max1=a[row][col];
}
else if (a[row][col]>max2)
max2=a[row][col];
}

}

return max2;
}
```

I am not quite sure what int rows is supposed to be. isnt a[] supposed to be the number of rows?

If wanted to call this function in the main function, how would i do it?

```
int main()
{

SecondMax2d(__, ___,___)
// If he gives a question(similar to the one above) where he says it should have 10 columns, without specifying rows, i am not sure what's supposed to go in the first blank, and the last, which is supposed to be int rows.
system("pause");
return 0;
}
```

Thanks for your help

Is This A Good Question/Topic? 0

## Replies To: third largest number 2d arrays

### #2 tlhIn`toq

• Freelance developer

Reputation: 6122
• Posts: 13,524
• Joined: 02-June 10

## Re: third largest number 2d arrays

Posted 23 February 2013 - 01:49 PM

So basically "I don't know where to start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.

In short, the teacher isn't doing their job of actually teaching. You can't expect to keep posting after every lecture and have the volunteers here be your free private tutor to make up for a bad teacher. We don't have the resources to do that for each student in each class around the country. And its not right that the teacher get away with doing a bad job and still getting paid.
Was This Post Helpful? -1

### #3 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 02:05 PM

tlhIn`toq, on 23 February 2013 - 01:49 PM, said:

So basically "I don't know where to start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.

In short, the teacher isn't doing their job of actually teaching. You can't expect to keep posting after every lecture and have the volunteers here be your free private tutor to make up for a bad teacher. We don't have the resources to do that for each student in each class around the country. And its not right that the teacher get away with doing a bad job and still getting paid.

yeah i hear what you're saying except the problem is...he doesn't answer his emails
Was This Post Helpful? -1

### #4 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: third largest number 2d arrays

Posted 23 February 2013 - 02:21 PM

Quote

He doesn't specify the number of rows, so should it be 1, by default?

Quote

Call this function from
the main program with 2 different arrays to show that it works.

Looking at these two lines I would say use an array of array[2][5].

Quote

I am not quite sure what int rows is supposed to be. isnt a[] supposed to be the number of rows?

Normally when you pass an array into your function you should also pass the size of this array as well. So rows should be the size of your array.

Jim
Was This Post Helpful? 1

### #5 tlhIn`toq

• Freelance developer

Reputation: 6122
• Posts: 13,524
• Joined: 02-June 10

## Re: third largest number 2d arrays

Posted 23 February 2013 - 02:34 PM

helloworld1234, on 23 February 2013 - 03:05 PM, said:

yeah i hear what you're saying except the problem is...he doesn't answer his emails

Then go knock on his office door!

If the teacher is that bad you need to go over his head to his boss. You're not the first person with a bad teacher. This is from my FAQ list:

Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?
A: If you are struggling with how to do this you need to talk to your professor. Let him/her know you are struggling at this early stage in class. Make your professor do the job (s)he gets paid to do by either helping you or finding you a tutor or giving you more exercises from earlier in the book. You are paying for this education. Be an active part in not allowing the teacher to brush you off and let you slip through the cracks. If the other 50 people in class aren't struggling then don't blame the teacher. If the other 50 people are struggling then you all need to go to the professor's superior and tell him/her. If the prof is really incompetent then you don't need to help him keep his cushy 100,000/year job. If the prof isn't incompetent then you need to study harder or consider buying another book on programming. You aren't required to ONLY use the text book provided for the class.
"How do you resolve your issues" survey.

This post has been edited by tlhIn`toq: 23 February 2013 - 02:34 PM

Was This Post Helpful? -1

### #6 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 02:50 PM

tlhIn`toq, on 23 February 2013 - 02:34 PM, said:

helloworld1234, on 23 February 2013 - 03:05 PM, said:

yeah i hear what you're saying except the problem is...he doesn't answer his emails

Then go knock on his office door!

If the teacher is that bad you need to go over his head to his boss. You're not the first person with a bad teacher. This is from my FAQ list:

Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?
A: If you are struggling with how to do this you need to talk to your professor. Let him/her know you are struggling at this early stage in class. Make your professor do the job (s)he gets paid to do by either helping you or finding you a tutor or giving you more exercises from earlier in the book. You are paying for this education. Be an active part in not allowing the teacher to brush you off and let you slip through the cracks. If the other 50 people in class aren't struggling then don't blame the teacher. If the other 50 people are struggling then you all need to go to the professor's superior and tell him/her. If the prof is really incompetent then you don't need to help him keep his cushy 100,000/year job. If the prof isn't incompetent then you need to study harder or consider buying another book on programming. You aren't required to ONLY use the text book provided for the class.
"How do you resolve your issues" survey.

His office hours are "by appointment." i don't want to sound like i am coming up with excuses but he put up this assignment on Thursday, due on Monday the 25th. i didn't have a chance to look at it until yesterday. and now that i am unclear about the question, i have no way to contact him other than by email, which he won't respond to.

If i had another class in between the time that he put up the assignment and the time the assignment was do, i would have definitely gone up to him and asked him to clear it up, believe me.

jimblumberg, on 23 February 2013 - 02:21 PM, said:

Quote

He doesn't specify the number of rows, so should it be 1, by default?

Quote

Call this function from
the main program with 2 different arrays to show that it works.

Looking at these two lines I would say use an array of array[2][5].

Quote

I am not quite sure what int rows is supposed to be. isnt a[] supposed to be the number of rows?

Normally when you pass an array into your function you should also pass the size of this array as well. So rows should be the size of your array.

Jim

thanks!

i took your advice and changed my code to the following:

```[#include <iostream>

using namespace std;

int SecondMax2D(int a[2][5], int rows)
{
int max1;
int max2;

if (a[0][0]>a[0][1])
{
max1= a[0][0];
max2=a[0][1];
}
else
{
max1=a[0][1];
max2=a[0][0];
}

for (int row=0; row<rows; row++)
{
for (int col=0; col<10; col++)
{
if(row == 0 && (col == 0 || col ==1))
continue;
if (a[row][col]> max1)
{
max2=max1;
max1=a[row][col];
}
else if (a[row][col]>max2)
max2=a[row][col];
}

}

return max2;
}
int main()
{
int a[2][5]={{34,2,33,45,12},{7,58,46,23,5}};

cout<< SecondMax2D(0, 10) ;

system("pause");
return 0;

}/code]

when i debug it, it freezes and says," Unhandled exception at 0x779415de in 2darray.exe: 0xC0000005: Access violation reading location 0x00000000."

Sorry about the coding tags

[code]
#include <iostream>

using namespace std;

int SecondMax2D(int a[2][5], int rows)
{
int max1;
int max2;

if (a[0][0]>a[0][1])
{
max1= a[0][0];
max2=a[0][1];
}
else
{
max1=a[0][1];
max2=a[0][0];
}

for (int row=0; row<rows; row++)
{
for (int col=0; col<10; col++)
{
if(row == 0 && (col == 0 || col ==1))
continue;
if (a[row][col]> max1)
{
max2=max1;
max1=a[row][col];
}
else if (a[row][col]>max2)
max2=a[row][col];
}

}

return max2;
}
int main()
{
int a[2][5]={{34,2,33,45,12},{7,58,46,23,5}};

cout<< SecondMax2D(0, 2); ;

system("pause");
return 0;

}

```

Was This Post Helpful? -1

### #7 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 02:58 PM

when i debug it, it freezes and says," Unhandled exception at 0x779415de in 2darray.exe: 0xC0000005: Access violation reading location 0x00000000.
Was This Post Helpful? -1

### #8 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 03:12 PM

new code

```#include <iostream>

using namespace std;

int SecondMax2D(int a[2][5], int rows)
{
int max1;
int max2;

if (a[0][0]>a[0][1])
{
max1= a[0][0];
max2=a[0][1];
}
else
{
max1=a[0][1];
max2=a[0][0];
}

for (int row=0; row<rows; row++)
{
for (int col=0; col<5; col++)
{
if(row == 0 && (col == 0 || col ==1))
continue;
if (a[row][col]> max1)
{
max2=max1;
max1=a[row][col];
}
else if (a[row][col]>max2)
max2=a[row][col];
}

}

return max2;
}
int main()
{
int a[2][5]={{34,2,33,45,12},{7,58,46,23,5}};

cout<< SecondMax2D(a, 2); ;

system("pause");
return 0;

}
```

This post has been edited by helloworld1234: 23 February 2013 - 03:18 PM

Was This Post Helpful? 0

### #9 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 03:17 PM

the result gives me 46

This post has been edited by helloworld1234: 23 February 2013 - 03:19 PM

Was This Post Helpful? -1

### #10 tlhIn`toq

• Freelance developer

Reputation: 6122
• Posts: 13,524
• Joined: 02-June 10

## Re: third largest number 2d arrays

Posted 23 February 2013 - 03:21 PM

helloworld1234, on 23 February 2013 - 03:50 PM, said:

... due on Monday the 25th. i didn't have a chance to look at it until yesterday.

Got it: Not a failing on the part of the teacher, but a failing on the part of the student.

We all live and learn. I'm sure you won't make this mistake again. I'm confident you've learned to examine the assignment more closely as soon as you get it, ask questions and so on even if you don't plan to start it until 48 hours before it is due.
Was This Post Helpful? -1

### #11 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 23 February 2013 - 03:29 PM

tlhIn`toq, on 23 February 2013 - 03:21 PM, said:

helloworld1234, on 23 February 2013 - 03:50 PM, said:

... due on Monday the 25th. i didn't have a chance to look at it until yesterday.

Got it: Not a failing on the part of the teacher, but a failing on the part of the student.

We all live and learn. I'm sure you won't make this mistake again. I'm confident you've learned to examine the assignment more closely as soon as you get it, ask questions and so on even if you don't plan to start it until 48 hours before it is due.

It's not failing on the part of the student(other than the fact that i might be stupid too understand this). I have his class on Mondays and Wednesdays. How can i get in touch with someone about something that's not posted until a day later we meet. I don't think it was that big of a crime for me to not see it till the next day. I have other classes, assignments due as well. I didn't not do it out of laziness or indifference.

I am not a computer science major but i've taken programming before, which i did well on. There's failure on both his part and mine(since my brain can't seem to grasp this).

This post has been edited by helloworld1234: 23 February 2013 - 03:33 PM

Was This Post Helpful? 0

### #12 Adak

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: third largest number 2d arrays

Posted 23 February 2013 - 03:59 PM

So the logic is:

*if a number is greater than max1, then make max2=max1, and assign the new value to max1

Which would work well except when the greatest number is the first number, then max2 is left with no value.

*so in the else part, you check if the number is greater than max2. (and it must be less than max1 or it wouldn't be here).

If it is, then assign it to max2.

Sounds like it covers all the bases in the logic. Do you understand that OK?

To get the third largest number, you need to extend that concept and add max3.

Treat it just the same as max2, but have max2 feed down to max3, when it receives a new (higher) value.

That appears to be what the instructor wants you to work on, in this assignment. If you're stuck on that, specify what you're stuck on, and we'll focus in on that.

This post has been edited by Adak: 23 February 2013 - 04:04 PM

Was This Post Helpful? 1

### #13 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 24 February 2013 - 12:35 PM

Adak, on 23 February 2013 - 03:59 PM, said:

So the logic is:

*if a number is greater than max1, then make max2=max1, and assign the new value to max1

Which would work well except when the greatest number is the first number, then max2 is left with no value.

*so in the else part, you check if the number is greater than max2. (and it must be less than max1 or it wouldn't be here).

If it is, then assign it to max2.

Sounds like it covers all the bases in the logic. Do you understand that OK?

To get the third largest number, you need to extend that concept and add max3.

Treat it just the same as max2, but have max2 feed down to max3, when it receives a new (higher) value.

That appears to be what the instructor wants you to work on, in this assignment. If you're stuck on that, specify what you're stuck on, and we'll focus in on that.

yeah im mostly stuck on the for loop part and everything under it. i'll try different things and see how i can make it happen. thanks
Was This Post Helpful? 0

### #14 trollolololol

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 25-February 13

## Re: third largest number 2d arrays

Posted 25 February 2013 - 05:38 PM

http://www.baruch.cu...ic_honesty.html
Was This Post Helpful? 0

### #15 helloworld1234

• New D.I.C Head

Reputation: -3
• Posts: 24
• Joined: 09-February 13

## Re: third largest number 2d arrays

Posted 25 February 2013 - 08:39 PM

???

trollolololol, on 25 February 2013 - 05:38 PM, said:

think you need to take your own advice, mr. troll=). you're basically trying to do the same thing i am.

how is asking for help on a program plagiarizing anyway? never asked anyone to write it for me. i actually wrote it myself. besides, the majority of this post consisted of a lecture from a dude about me how i've failed as a student.

This post has been edited by helloworld1234: 25 February 2013 - 08:49 PM

Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }