Finding largest value in every array's column - c++

Currently I have a 2D array and I need to find largest value of every array's column and find their sum.
My code seems to not work properly because I'm getting largest values of every row, not column, also the program sometimes prints the same value two times.
I wrote this code based on a tutorial and I don't know where I made a mistake. There should be basic C++ functions (cycles, if statements, arrays).
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, m, a[10][10], sum = 0, max;
cin >> n >> m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[j][i];
}
}
for (int i = 0; i < m; i++) {
max = a[0][i];
for (int j = 0; j < n; j = j + 1) {
if (a[j][i] >= max) {
max = a[j][i];
sum = sum + a[j][i];
cout << max << endl; //control printing to see the values
}
}
}
cout << sum;
}
Input:
3 3
3 2 3
7 5 1
6 3 5
Output I need to get is 17 but I am getting 19 right now.

Assuming your array has m columns and n rows.
int sum = 0, max = 0;
for( int i = 0 ; i < m ; ++i )
{
max = 0;
for( int j = 0 ; j < n ; ++j )
{
if(max < a [j][i])
{
max = a[j][i];
}
}
sum += max;
}

Related

Finding min and max elements of every column and row in a matrix C++

I was trying to do the following:
Have a matrix, print the entire thing out, print at end of every row the biggest element of said row and print at the bottom of every column the smallest element of said column.
I'm pretty much a beginner at C++.
So here's what I've done so far:
#include <iostream>
#include <iomanip>
#define M 50
#define N 50
using namespace std;
int main()
{
int m,n;
int a[M][N];
int b[M],c[N];
do {
cout<<"m=";
cin>>m;
cout<<endl<<"n=";
cin>>n;
cout<<endl;
}
while(m!=n);
for(int i=0;i<m; i++) {
for(int j=0; j<n; j++){
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
int max_row;
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
for (int i=0; i<m; i++)
{ for(int j=0; j<n; j++){
cout<<setw(3)<<a[i][j]<<"\t";
}
cout<<"|"<<b[i];
cout<<endl;
}
for(int i=0; i<m; i++){
cout<<setw(3)<<"-";}
cout<<endl;
for(int j=0; j<n; j++)
{cout<<c[j]<<"\t";
}
system("pause");
}
Most of the time the max_row are the correct ones such as this case:
3 2 1 |3
4 6 5 |6
7 8 9 |9
Other times they get messed up and it goes like this:
1 2 3 |3
4 33 6 |33
7 8 9 |-858993460
I really have no idea what causes it and since there are no error messages I got really confused. Also I have no idea how to make the min column ones. Any help would be appreciated.
The problem with these loops
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
is that the value of max_row should be initialized with each iteration of the outer loop. Otherwise all rows after the first row deal with the maximum value of the previous row and in general can not have en element that is greater than the current value of max_row. So the corresponding element of the array b will not be initialized.
Also the user can enter for the matrix negative values in this case your program will output zeroes instead of maximum values.
To find maximum elements in rows and minimum elements in columns it is enough to have one pair of nested loops/
Here is a demonstrative program/
#include <iostream>
#include <iomanip>
int main()
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 33, 6 },
{ 7, 8, 9 }
};
int b[N], c[N];
for ( size_t i = 0; i < N; i++ )
{
b[i] = a[i][0];
c[i] = a[0][i];
for ( size_t j = 1; j < N; j++ )
{
if ( b[i] < a[i][j] ) b[i] = a[i][j];
if ( a[j][i] < c[i] ) c[i] = a[j][i];
}
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
std::cout << std::setw( 3 ) << a[i][j] << '\t';
}
std::cout << '|' << b[i] << '\n';
}
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << '-' << '\t';
}
std::cout << '\n';
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << c[i] << '\t';
}
std::cout << '\n';
return 0;
}
Its output is
1 2 3 |3
4 33 6 |33
7 8 9 |9
- - -
1 2 3

C++ 2D array index compare

I need help in a C++ for a school task.
I don't really know where the error is.
It seems like it skips the first row.
I should compare the highest value with an other row's avarage value.
Task:
In the first row of the standard input there are the count of the cities (1≤N≤1000) and the count of days (1≤M≤1000). In the following N row there are the daily forecast M temperature values (-50≤Hi,j≤50).
In the standard output's first row, you have to write the city number, which maximal forecast has to be lower than some other city's avarage temperature! If there is none you should write -1!
Example:
Input
3 5
11 11 11 11 20
18 16 12 16 20
10 15 12 10 10
The code:
#include <iostream>
using namespace std;
int main() {
int N, M;
cin >> N;
cin >> M;
int homerseklet[N][M];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> homerseklet[i][j];
}
}
int maxh[N] = {0}, osszh[N] = {0};
for (int i = 0; i < N; i++)
{
maxh[i] = homerseklet[i][0];
for (int j = 0; j < M; j++)
{
osszh[i] = osszh[i] + homerseklet[i][j];
if (homerseklet[i][j] > maxh[i])
{
maxh[i] = homerseklet[i][j];
}
}
}
int atlag[N] = {0};
for (int i = 0; i < N; i++)
{
atlag[i] = osszh[i] / M;
}
bool van = false;
for (int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if (i != j && maxh[i] < atlag[j])
{
if (van = true)
{
cout << i + 1 << endl;
}
}
}
}
if (!van)
{
cout << -1 << endl;
}
return 0;
}

I need my sub-square-matrix sum program to output the sum of the largest square but it is finding the sum of the largest rectangle instead

I need to Write a program that takes in a square matrix of integers and outputs the largest sub-SQUARE-matrix sum. The first line of input is an integer which indicates the dimension of the square matrix(n*n), followed by the actual matrix row-by-row.
I have a program working however, it outputs the largest sum of a rectangle and not a square which is required.
Example input:
3
1 2 3
4 5 6
-7 -8 -9
Output:
Should be 16 (2+3+5+6) however it is outputting 21 (1+2+3+4+5+6)
As you can see, it is taking the sum of the rectangle but I need it to find a square
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int n;
int mat[100][100];
cin >> n;
int sum = 0;
int maxSum = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin>>mat[i][j];
}
}
for (int k = 0; k<n; k++)
{
for (int l = 0; l<n; l++)
{
sum = sum + mat[k][l];
if (sum < 0)
{
sum = 0;
}
if (sum > maxSum)
{
maxSum = sum;
}
}
}
cout << maxSum;
return 0;
}
You were close with your loops. But you have to sum the square for each iteration if that is what you are looking for. Here I'll assume an input and go from there.
Edit: So the summing is clearer.
#include <iostream>
#include <vector>
int main()
{
//initalize(using std::cin here) You can size then load the vectors
std::vector<std::vector<int>> mat = { {1,2,3},{4,5,6},{-7,-8,-9,} };
// then do the work...
int max = std::numeric_limits<int>::min();
//assuming 'square' is 2x2, adjust 'size()-1` if not
for (size_t i = 0; i < mat.size() - 1; ++i)
for( size_t j = 0; j < mat.at(0).size() - 1; ++j)
{
int sum =
mat[i][j] +
mat[i][j+1] +
mat[i+1][j] +
mat[i+1][j+1];
if (sum > max)
max = sum;
}
std::cout << max << std::endl;
}

Range max sum query using sparse table

I Implemented range max sum query using sparse Table ,I Know more efficient approach would be using segment trees.
What I have tried:
I am calculating the max sum in the range (i,2^j-1) for all possible value of i and j and storing them in a table
where i is the index and j denotes the power of 2 (2^j denotes the length of segment from i for which we are calculating max sum)
Now using the above table we can answer the queries
Input:
3
-1 2 3
1
1 2
expected output:
2
actual output:
"wrong answer(garbage value)"
we actually have to tell the max contiguous sum in a given query
Link to the ques spoj gss1
Please help:
#include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
const int k = 16;
const int N = 1e5;
const int ZERO = 0; // ZERO + x = x + ZERO = x (for any x)
long long table[N][k + 1]; // k + 1 because we need to access table[r][k]
long long Arr[N];
int main()
{
int n, L, R, q;
cin >> n; // array size
for(int i = 0; i < n; i++)
cin >> Arr[i];
// build Sparse Table
for(int i = 0; i < n; i++)
table[i][0] = Arr[i];
for(int j = 1; j <= k; j++) {
for(int i = 0; i <= n - (1 << j); i++)
//table[i][j] = table[i][j - 1] + table[i + (1 << (j - 1))][j - 1];
table[i][j] = max(table[i][j-1],max(table[i+(1<<(j-1))][j-1],table[i+(1<<(j-1))][j-1]+table[i][j-1]));
}
cin >> q; // number of queries
for(int i = 0; i < q; i++) {
cin >> L >> R; // boundaries of next query, 0-indexed
long long int answer = LLONG_MIN;
for(int j = k; j >= 0; j--) {
if(L + (1 << j) - 1 <= R) {
answer = max(answer,answer + table[L][j]);
L += 1 << j; // instead of having L', we increment L directly
}
}
cout << answer << endl;
}
return 0;
}
link to the question Spoj Gss1

Selection sort ascending

That is my function:
int main() {
double data[100];
int num;
cout<<"num= ";
cin>>num;
for(int i = 1; i <= num; i++) {
cout<<i<<" element = ";
cin>>data[i];
}
Sort(data, num);
for (int i = 1; i <= num; i++) {
cout<<data[i]<<endl;
}
return 0;
}
void Sort(double data[], int n) {
int i,j,k;
double min;
for(i = 0; i < n-1; i++) {
k = i;
min = data[k];
for(j = i+1; j < n; j++)
if(data[j] < min) {
k = j;
min = data[k];
}
data[k] = data[i];
data[i] = min;
}
}
if I write for exp. three elements: 8,9,1 again cout 8,9,1?
for(int i = 1; i <= num; i++) { // WRONG
I think you mean:
for(int i = 0; i < num; i++) { // RIGHT
Arrays in C are 0-indexed remember.
Your sorting function is fine. The only problem is that you enter elements at positions 1 through n, inclusive, while you should use 0 through n-1, inclusive, in both loops of the main() function.
If you need to print numbers 1 through n, use
cout<<(i+1)<<" element = ";
You should get used of the 0 index begin in the for loop
for(int i = 0; i < N; ++i)
so fixing these two index errors will make your code run properly.
the reason is:
if you write data to data[] using 1 as the begining, your data array's first item will be a random number:
if you insert 3 elements, the array will be like this:
data[0] = ??? // maybe a very very big number
data[1] = 8
data[2] = 9
data[3] = 1
and in your Sort function, your index begins at 0 and ends before num, that means your code would only sort data[0], data[1], data[2].
if you use: num = 3, 3 2 1 as your input data for the origin code you could see that 3 and 2 is sorted
I guess your Sort code is googled from somewhere, please try to understand it.
Good online algorithm course: https://www.coursera.org/course/algs4partI
a very good algorithm online book: http://algs4.cs.princeton.edu/home/
btw, for(j = i+1; j < n; j++) in the Sort function would be better if it has { } braces.