I have to sort the matrix by the sum of the rows, the smallets sum has to be first and the the bigger and at the end has to be the biggest sum.
I have done this but I can't finish it:
#include <iostream>
using namespace std;
int main ()
{
int **matrix;
int i, j, count, row, col, sum, temp;
cout << "\n Enter the number of rows and columns";
cin >> row >> col;
matrix = new int *[row];
for (count = 0; count < row; count++)
matrix[count] = new int[col];
cout << "\nNow enter the element for the matrix.";
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
cout << "\nRow " << (i + 1) << " Col " << (j + 1) << " :";
cin >> *(*(matrix + i) + j);
}
}
for (i = 0; i < row; i++)
{
sum = 0;
for (j = 0; j < col; j++)
sum = sum + matrix[i][j];
cout << sum << endl;
}
for (int count = 0; count < row; count++)
delete[]matrix[count];
delete[]matrix;
matrix = 0;
return 0;
}
You can use standard algorithms std::sort and std::accumulate. Below there is a demonstrative program that shows how these algorithms can be used together.
#include <iostream>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <ctime>
int main()
{
const size_t M = 3, N = 5;
std::srand( ( unsigned int )std::time( nullptr ) );
int **matrix = new int *[M];
for ( size_t i = 0; i < M; i++ ) matrix[i] = new int[N];
for ( size_t i = 0; i < M; i++ )
{
for ( size_t j = 0; j < N; j++ ) matrix[i][j] = std::rand() % ( M * N );
}
for ( size_t i = 0; i < M; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << matrix[i][j] << ' ';
std::cout << '\n';
}
std::cout << '\n';
auto sort_by_sum = [N]( const auto &left, const auto &right )
{
return std::accumulate( left, left + N, 0ll ) <
std::accumulate( right, right + N, 0ll );
};
std::sort( matrix, matrix + M, sort_by_sum );
for ( size_t i = 0; i < M; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << matrix[i][j] << ' ';
std::cout << '\n';
}
std::cout << '\n';
return 0;
}
The program output might look like
11 2 4 14 0
9 7 9 4 14
10 7 5 0 7
10 7 5 0 7
11 2 4 14 0
9 7 9 4 14
Related
I want to delete column with max integer in 2d array, I do it in this way, but why is deleting the column and also row? Can I fix that and delete only column? The task was do it with delete command, but now I think it's impossible
#include <iostream>
using namespace std;
int main()
{
int row = 3, col = 3;
int** arr = new int* [row];
for(int i = 0; i < row; i++){
arr[i] = new int[col];
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cin >> arr[i][j];
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << " ------------- " << endl;
int max = 0, index = 0;
for(int i =0; i < row; i++){
for(int j = 0; j < col; j++){
if(arr[i][j] > max){
max = arr[i][j];
index = i;
}
}
}
delete [] arr[index];
int** tmp = new int*[index - 1];
int tmpI = 0;
for(int i = 0; i < col; i++){
if(i != index){
tmp[tmpI++] = arr[i];
}
}
delete [] arr;
arr = tmp;
col = col - 1;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
For starters the variable index is set to a row number
index = i;
Then this row is deleted
delete [] arr[index];
But you are going to remove a column instead of a row. So this code does not make a sense.
Also you are incorrectly searching the maximum element. If the user will enter all negative values then the maximum value will be equal to 0 though an element with such value is not present in the array.
In this declaration
int** tmp = new int*[index - 1];
you allocated an array with rows that one less than the number of rows in the original array. Moreover if index is equal to 0 then there is allocated a very large extent of memory.
This statement
delete [] arr;
produces a memory leak.
It seems what you need is something like the following
#include <iostream>
int main()
{
size_t row = 3, col = 3;
int **arr = new int * [row];
for ( size_t i = 0; i < row; i++ )
{
arr[i] = new int[col];
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cin >> arr[i][j];
}
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << "------------- " << std::endl;
size_t max_i = 0, max_j = 0;
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
if ( arr[max_i][max_j] < arr[i][j] )
{
max_i = i; max_j = j;
}
}
}
int **tmp = new int*[row];
for ( size_t i = 0; i < row; i++ )
{
tmp[i] = new int[col-1];
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0, k = 0; j < col; j++ )
{
if ( j != max_j ) tmp[i][k++] = arr[i][j];
}
}
for ( size_t i = 0; i < row; i++ )
{
delete [] arr[i];
}
delete [] arr;
arr = tmp;
--col;
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
for ( size_t i = 0; i < row; i++ )
{
delete [] arr[i];
}
delete [] arr;
return 0;
}
The program output might look like
1 2 3
6 5 4
7 9 8
-------------
1 3
6 4
7 8
Here's an alternate suggestion: You are storing the data in row x column format. If you change to column x row format, it becomes easier to delete a column. I also made a helper function to print the matrix and I combined the input loop and the loop that finds the max.
#include <iostream>
#include <iomanip>
#include <cstdlib> // for rand
#include <climits>
using namespace std;
void print_matrix(int** arr, int rows, int columns)
{
for(int row = 0; row < rows; row++){
for(int col = 0; col < columns; col++) {
cout << setw(2) << arr[col][row] << " ";
}
cout << "\n";
}
}
int main()
{
srand(time(nullptr)); // For testing
int rows = 3, columns = 3;
// Create the matrix
int** arr = new int *[columns];
for (int col = 0; col < columns; col++) {
arr[col] = new int[rows];
}
// Input values - finding max, too
int max_value = INT_MIN, max_value_column = -1;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
//cin >> arr[col][row];
arr[col][row] = rand() % 50; // Using rand for testing.
if (arr[col][row] > max_value) {
max_value = arr[col][row];
max_value_column = col;
}
}
}
print_matrix(arr, rows, columns);
cout << " ------------- " << endl;
// Delete the column with max
delete [] arr[max_value_column];
columns--;
// Shift columns to the right of the deleted column left one
for (int col = max_value_column; col < columns; col++) {
arr[col] = arr[col + 1];
}
print_matrix(arr, rows, columns);
return 0;
}
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
So I have this code:
main.cpp
#include "matrix.h"
int main(int argc, char *argv[])
{
matrix m;
regularMatrix rMatrix;
rMatrix.setDetails(3, "1 2 3 4 5 6 7 8 9");
//rMatrix.displayMatrix();
//int i, j, r = 0, c = 0, a;
//cout << "Enter number of Rows and Columns: " << endl;
//cin >> r ;
system("PAUSE");
return EXIT_SUCCESS;
}
matrix.cpp
#include "matrix.h"
int rows = 3, columns = 3;
int **a;
void matrix::displayMatrix(int **arr)
{
cout << "Values Of 2D Array [Matrix] Are : ";
for (int i = 0; i < rows; i++ )
{
cout << " \n ";
for (int j = 0; j < columns; j++ )
{
cout << arr[i][j] << " ";
}
}
}
void matrix::setDetails(int dimension, string y)
{
int f = dimension;
rows = dimension;
columns = rows;
string input = y;
istringstream is(input);
int n;
a = new int *[rows];
for(int i = 0; i <rows; i++)
{
a[i] = new int[columns];
}
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < columns; j++ )
{
while ( is >> n)
{
a[i][j] = n;
//cout << a[i][j] << endl;
}
}
}
matrix::displayMatrix(a);
//cout << f << endl << g << endl;
}
matrix.h
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class matrix
{
public:
virtual void displayMatrix(int** arr);
virtual void setDetails(int dimension, string y);
//virtual void setMatrix(int m[]);
//virtual void printArray(int** i);
};
class regularMatrix : public virtual matrix
{
public:
};
It runs without error but the problem is, I'm getting different value when I'm displaying the matrix? I think I'm getting the address of the array.How will I get the value out of it? I think I'm right about passing my array.
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
{
while ( is >> n)
{
a[i][j] = n;
//cout << a[i][j] << endl;
}
}
}
This is actually pretty wrong. Look what you're doing here.
At beggining, you have i = 0 and j = 0
Then you got into the while loop.
And here until you input ints from stringstream you assign a[0][0] to new value.
See it? You never go to a[0][1] etc. Only first element will be valid and the rest will stay uninitialized, because after first execution of your while loop
there is no characters left in the istringstream object.
So to correct it:
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
{
if ( is >> n )
a[i][j] = n;
}
}
I need to find the mimimum element in two-dimensional(4,4) array by row and maximum element by column and store them in another array (5,5).Maybe I did not explain properly.
That's how it should look new array (5,5):
1 2 3 4 min
1 2 3 4 min
1 2 3 4 min
m m m m 0
*m - max
So this is the first array:
int array[4][4];
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
cout << "\n array[" << i + 1 << "][" << j + 1 <<"]=";
cin >> array[i][j];
}
}
With this I try to find the min of each row:
int min;
for (i = 0; i<4; i++) {
min[i] = array[0][i];/*expression must have pointer-to-object type*/
for (j = 1; j<4; j++) {
if (min[i]>array[i][j])/*expression must have pointer-to-object type*/
min[i] = array[i][j];/*expression must have pointer-to-object type*/
}
}
Where I'm wrong? I can't undrestand this error "expression must have pointer-to-object type".
And with this i will try to make new array:
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
newarr[i][j]=array[i][j];
newarr[i][5]=max[i];
}
}
for(j = 0; j < 4; j++)
newarr[5][j]=min[j];
Is this okay? There is no way to check it because I just can not find the min and max.
int min;
min[i]
is the source of error. min is an integer variable not an integer array.
change int min to int min[4];
You have int min;. This variable can hold only single value and following is ill-formed min[i], as min is not an array.
Just making it an array should solve the issue: int min[4];
Also note, that first index of 2D array traditionally denotes row and second a column. And check indices usage in your loops: why do you use i as bot first and second index in different places?
Here is shown a straightforward approach. You may use it as a base for your program
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
int main()
{
const int MAX_VALUE = 100;
const size_t N = 4;
int a[N][N];
int b[N+1][N+1];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( auto &row : a )
{
for ( int &x : row ) x = std::rand() % MAX_VALUE;
}
for ( const auto &row : a )
{
for ( int x : row ) std::cout << std::setw( 2 ) << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
for ( size_t i = 0; i < N; i++ ) b[N][i] = a[0][i];
b[N][N] = 0;
for ( size_t i = 0; i < N; i++ )
{
int min = a[i][0];
for ( size_t j = 0; j < N; j++ )
{
b[i][j] = a[i][j];
if ( a[i][j] < min ) min = a[i][j];
if ( b[N][j] < b[i][j] ) b[N][j] = b[i][j];
}
b[i][N] = min;
}
for ( const auto &row : b )
{
for ( int x : row ) std::cout << std::setw( 2 ) << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
}
The program output might look like
77 53 41 90
67 57 90 94
20 41 29 38
3 72 33 43
77 53 41 90 41
67 57 90 94 57
20 41 29 38 20
3 72 33 43 3
77 72 90 94 0
The idea is to fill the last row of the new array with values of the first row of the original array and then compare each value of the last row with a value of the current row in the current column.
Well that's the whole code:
#include <iostream>
using namespace std;
int main() {
int A[4][4];
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
{
int min[4];
for (i = 0; i < 4; i++) {
min[i] = A[0][i];
for (j = 1; j < 4; j++) {
if (min[i] > A[i][j])
min[i] = A[i][j];
}
}
int newarr[5][5];
int max[5] = { 1,2,3,4,5 };
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
newarr[i][j] = A[i][j];
newarr[i][5] = max[i];
}
}
for (j = 0; j < 4; j++)
newarr[5][j] = min[j];
cout << newarr[5][j] << "\t";
cout << "\n";
}
}
I put random elements to max. Because so far I only test. But once I started my program it show correct only the first array. And where should be the new array it shows zero. Here it is the outcome of the debuging:
5 4 3 1
5 6 7 9
4 2 3 9
4 8 4 6
0
#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].