Reputation: 25
My program is supposed to find two largest elements in 2D array. There's everything fine with the first element, program counts it right, but there's a big problem with the second element because I get wrong value. I know there are info about finding second largest element in array, but I couldn't find anything for 2D array.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,m,sum=0;
int max=-1000001;
int maxsecond=-1000001,maxdu=0;
cin>> n>>m;
int a[10][10];
for (int i=1; i<n; i++)
{
for (int j=1; j<m; j++)
{
cin>>a[i][j];
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j]>max)
{
maxsecond=max;
max=a[i][j];
}
else if (a[i][j]>maxsecond)
{
maxsecond=a[i][j];
}
}
}
cout<<endl;
cout<<max<<" "<<maxsecond;
}
Input:
2 3
4 8 5
2 1 7
Output I should get:
8 7
Output I am getting right now:
8 4
Upvotes: 0
Views: 3362
Reputation: 463
In this code block which you have used to input your 2D array if you observe
for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { cin >> a[i][j]; } }
You are only entering elements from position 1 to n-1 and m-1 respectively.
so if your input is 2 3
then
a[1][1] ->> 4
a[1][2] ->> 8
The input will stop since the condition becomes false. Then after the rest of the code execute with the same giving you the result as 8 and 4;
In C++ arrays start form 0
to n-1
. Say for array arr[5], we are able to access a[0], a[1], a[2], a[3], a[4].
So, We write loops as to iterate arrays as
for ( int i = 0; i < arr_size; i++ )
Please find the corrected solution for reference.
#include <iostream>
#include <limits>
int main()
{
int arr[10][10];
int size1 = 0, size2 = 0;
std::cin >> size1 >> size2;
for (size_t i = 0; i < size1; i++)
{
for (size_t j = 0; j < size2; j++)
{
std::cin >> arr[i][j];
}
}
int max, secondMax;
max = secondMax = std::numeric_limits<int>::min();
for (size_t i = 0; i < size1; i++)
{
for (size_t j = 0; j < size2; j++)
{
if (arr[i][j] > max)
{
secondMax = max;
max = arr[i][j];
}
else if (arr[i][j] > secondMax)
{
secondMax = arr[i][j];
}
}
}
std::cout << max << " " << secondMax << std::endl;
return 0;
}
Upvotes: 0
Reputation: 174
First: You are scanning the inputs wrong. You are trying to read the inputs into 2D Array from range (int i=1; i<n; i++)
& (int j=1; j<m; j++)
with staring from 1 and going till <n
you are reading only n-1
elements. Your code for comparison is right but the way of indexing is wrong.
You can just modify the indexing for both reading and comparison to be (int i=0; i<n; i++)
& (int j=0; j<m; j++)
and it will solve the issue.
Second: Your main function is 'int main()
' but you forgot to add 'return 0
' at end
Suggestion: Since you are taking the size of 2D Array (Matrix) from user, i will suggest to use a STL vector for this. You can resize vectors after taking input. Here's an example
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<vector<int> >a;
int max=-1000001;
int maxsecond=-1000001,maxdu=0;
int n,m;
cin>>n>>m;
a.resize(n,vector<int>(m,0)); //Creates n x m matrix
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
cin>>a[i][j];
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[i][j]>max)
{
maxsecond=max;
max=a[i][j];
}
else if (a[i][j]>maxsecond)
{
maxsecond=a[i][j];
}
}
}
cout<<"\nMax: "<<max;
cout<<"\nSecond Max: "<<maxsecond;
return 0;
}
Upvotes: 1
Reputation: 106
Putting aside the issues pointed in the comments, your code is missing equal signs in the input lines, so it does not read in all the user inputs. For example, the code reads only two numbers from your sample inputs. Adding equal signs in the comparison operators as below would fix the problem:
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cin >> a[i][j];
}
}
Upvotes: 0