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
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'm working on a code that finds all saddle points in a matrix. Both smallest in their row and biggest in their column, and biggest in their row and smallest in their column fall under the definition (of my university) of a saddle point. Being a beginner I managed to get half of it done (finding saddle points which are smallest in their row and biggest in their column) by copying parts of what we've done in class and typing it myself. I have been stuck on it for quite some time and can't figure how to add the saddle points which are biggest in their row and smallest in their column to the program.
This is what I have so far:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int x, y;
int pos_max(int j) //saddle points check
{
int max = 0;
for (int i = 1; i <= x - 1; i++) {
if (a[i][j] > a[max][j]) {
max = i;
}
}
return max;
}
int main() {
cout << "Enter the number of rows: ";
cin >> x;
cout << "Enter the number of columns: ";
cin >> y;
cout << "----------------------------" << endl;
for (int i = 0; i <= x - 1; i++) //input of the matrix
for (int j = 0; j <= y - 1; j++) {
cout << "a[" << i + 1 << ", " << j + 1 << "] = ";
cin >> a[i][j];
}
cout << "----------------------------\n";
for (int i = 0; i <= x - 1; i++) //visualization of the matrix
{
for (int j = 0; j <= y - 1; j++)
cout << a[i][j] << " ";
cout << endl;
}
cout << "----------------------------\n";
int r;
int flag = 0;
int i = y;
for (int j = 0; j <= y - 1; j++) {
r = pos_max(j);
for (i = 0; i <= y - 1; i++) {
if (a[r][i] < a[r][j]) {
break;
}
}
if (i == y) {
cout << "Saddle points are: ";
cout << "a[" << r + 1 << ", " << j + 1 << "] = " << a[r][j] << "\n";
flag = 1;
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
First, there is a logical error with your code. In the pos_max function, it will return the index of the element which is maximum in the column. There can be a case when there are multiple maximum with the same value in the column, however, it returns the one which is not the minimum in the row, hence your program won't be able to print that saddle point.
To solve this, you can either return an array of all indices which are maximum in a column and then check for each of those points if it's minimum in their respective column, but I think it's not a very elegant solution. In any case, you will again have to write the entire code for the other condition for saddle points, minimum in column and maximum in row.
Hence, I would suggest a change in strategy. You create 4 arrays, max_row, max_col, min_row, min_col, where each array stores the minimum / maximum in that row / column respectively. Then you can traverse the array and check if that point satisfies saddle point condition.
Here is the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int max_row[10], max_col[10], min_row[10], min_col[10];
int x, y;
bool is_saddle(int i, int j) {
int x = a[i][j];
return (max_row[i] == x && min_col[j] == x) || (min_row[i] == x && max_col[j] == x);
}
int main() {
/* code to input x, y and the matrix
...
*/
/* code to visualize the matrix
...
*/
/* populating max and min arrays */
for (int i = 0; i <= x-1; ++i) {
max_row[i] = a[i][0], min_row[i] = a[i][0];
for (int j = 0; j <= y-1; ++j) {
max_row[i] = max(max_row[i], a[i][j]);
min_row[i] = min(min_row[i], a[i][j]);
}
}
for (int j = 0; j <= y-1; ++j) {
max_col[j] = a[0][j], min_col[j] = a[0][j];
for (int i = 0; i <= x-1; ++i) {
max_col[j] = max(max_col[j], a[i][j]);
min_col[j] = min(min_col[j], a[i][j]);
}
}
/* Check for saddle point */
for (int i = 0; i <= x-1; ++i) {
for (int j = 0; j <= y-1; ++j) {
if (is_saddle(i, j)) {
cout << "Saddle points are: ";
cout << "a[" << i + 1 << ", " << j + 1 << "] = " << a[i][j] << "\n";
flag = 1;
}
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
#include <iostream>
using namespace std;
int getMaxInRow(int[][5], int, int, int);
int getMinInColumn(int[][5], int, int, int);
void getSaddlePointCordinates(int [][5],int ,int );
void getInputOf2dArray(int a[][5], int, int);
int main()
{
int a[5][5] ;
int rows, columns;
cin >> rows >> columns;
getInputOf2dArray(a, 5, 5);
getSaddlePointCordinates(a,rows,columns);
}
void getInputOf2dArray(int a[][5], int rows, int columns)
{
for (int i = 0; i < rows; i = i + 1)
{
for (int j = 0; j < columns; j = j + 1)
{
cin >> a[i][j];
}
}
}
void getSaddlePointCordinates(int a[][5],int rows,int columns)
{
int flag = 0;
for (int rowNo = 0; rowNo < 5; rowNo++)
{
for (int columnNo = 0; columnNo < 5; columnNo++)
{
if (getMaxInRow(a, rows, columns, rowNo) == getMinInColumn(a, rows, columns, columnNo))
{
flag = 1;
cout << rowNo << columnNo;
}
}
}
if (flag == 0)
cout << "no saddle point";
cout << "\n";
}
int getMaxInRow(int a[][5], int row, int column, int rowNo)
{
int max = a[rowNo][0];
for (int i = 1; i < column; i = i + 1)
{
if (a[rowNo][i] > max)
max = a[rowNo][i];
}
return max;
}
int getMinInColumn(int a[][5], int row, int column, int columnNo)
{
int min = a[0][columnNo];
for (int i = 1; i < row; i = i + 1)
{
if (a[i][columnNo] < min)
min = a[i][columnNo];
}
return min;
}
just take the reference arr(ref[size]) // memorization method to check the minimum and maximum value in it.
Here is the Code Implementation with time complexity O(n *n) & space complexity O(n):
#include <bits/stdc++.h>
using namespace std;
#define size 5
void util(int arr[size][size], int *count)
{
int ref[size]; // array to hold all the max values of row's.
for(int r = 0; r < size; r++)
{
int max_row_val = arr[r][0];
for(int c = 1; c < size; c++)
{
if(max_row_val < arr[r][c])
max_row_val = arr[r][c];
}
ref[r] = max_row_val;
}
for(int c = 0; c < size; c++)
{
int min_col_val = arr[0][c];
for(int r = 1; r < size; r++) // min_val of the column
{
if(min_col_val > arr[r][c])
min_col_val = arr[r][c];
}
for(int r = 0; r < size; r++) // now search if the min_val of col and the ref[r] is same and the position is same, if both matches then print.
{
if(min_col_val == ref[r] && min_col_val == arr[r][c])
{
*count += 1;
if((*count) == 1)
cout << "The cordinate's are: \n";
cout << "(" << r << "," << c << ")" << endl;
}
}
}
}
// Driver function
int main()
{
int arr[size][size];
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
cin >> arr[i][j];
}
int count = 0;
util(arr, &count);
if(!count)
cout << "No saddle points" << endl;
}
// Test case -> Saddle Point
/*
Input1:
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
0 2 3 4 5
Output1:
The cordinate's are:
(0,4)
(2,4)
(4,4)
Input2:
1 2 3 4 5
6 7 8 9 1
10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Output2:
No saddle points
*/
I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/
#include <iostream>
using namespace std;
// Selection Sort function.
// Parameter 'a' is the size of the array.
void ss(int AR[] , int a) {
int small;
for (int i = 0 ; i <a ; i++) {
small = AR[i];
for (int j = i+1 ; j <a ; j++) {
if (AR[j]< small) {
int k = AR[j];
AR[j] = AR[i];
AR[i] = k;
}
}
}
}
int main() {
cout << "Enter the size of Your Aray";
int a;
cin >> a;
int AR[a];
cout << endl;
for (int i = 0; i < a; i++) {
cin >> AR[i];
cout << endl;
}
ss(AR, a);
cout << "The Sorted Array is";
for (int i=0; i < a; i++) {
cout << AR[i] << " ";
cout << endl;
}
}
When I enter the following:
15
6
13
22
23
52
2
The result returned is:
2
13
6
15
22
23
52
What is the bug preventing the list from being sorted numerically as expected?
The function can look like
void ss ( int a[], size_t n )
{
for ( size_t i = 0 ; i < n ; i++ )
{
size _t small = i;
for ( size_t j = i + 1; j < n ; j++ )
{
if ( a[j] < a[small] ) small = j;
}
if ( i != small )
{
int tmp = a[small];
a[small] = a[i];
a[i] = tmp;
}
}
}
It doesn't seem to be the SelectionSort I know. in the algorithm I know during every loop I look for the smallest element in the right subarray and than exchange it with the "pivot" element of the loop. Here's the algorithm
void selectionSort(int* a, int dim)
{
int posMin , aux;
for(int i = 0; i < dim - 1; ++i)
{
posMin = i;
for(int j = i + 1; j < dim; ++j)
{
if(a[j] < a[posMin])
posMin = j;
}
aux = a[i];
a[i] = a[posMin];
a[posMin] = aux;
}
}
and it seems that you change every smaller element you find, but also change the position of the "pivot". I hope the answer is clear.
Everything is ok in the original function, only that the small variable need to be refreshed when two vector elements will be switched.
Also in if statement set the small variable to the new value of AR[i].