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
Related
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 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
I have generate random number from 1 to 500 and want to store in 2 arrays says array1(store 1 to 250 elements) and array2(store 251 to 500 elements). Size of Both array is 250. I successfully store in array1 but not in array2.
I tried but the loop goes on Infinite loop
#define MAX 250
int a1[MAX], a2[MAX];
srand((int)time(0));
for (i = 0; i < 500; i++) {
int c = (rand() % 500) + 1;
if (i >= 0 && i < 250) {
cout << "Array 1 ";
for (j = 0; j < 250; j++) {
a1[j] = c;
cout << a1[j] << " ";
}
}
if (i >= 250 && i < 500) {
cout << endl
<< "Array 2 ";
for (int k = 0; k < 250; k++) {
a2[k] = c;
cout << a2[k] << " ";
}
}
It's not looping infinitely; it's just looping too much.
That's because you put a loop in a loop (dawg).
You don't want those inner for loops at all.
Here you are.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
const size_t MAX = 250;
int a1[MAX], a2[MAX];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( auto a : { a1, a2 } )
{
for ( size_t i = 0; i < MAX; i++ ) a[i] = std::rand() % ( 2 * MAX ) + 1;
}
}
As for your code then the inner loops
for (j = 0; j < 250; j++) {
and
for (int k = 0; k < 250; k++) {
are executed 500 times because they are enclosed in the outer loop
for (i = 0; i < 500; i++)
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
*/
I am trying to find min (by row) and max (by column) element in two-dimensional (4,4) array and then store them in new array (5,5).
That is how it should look for new array (5,5):
1 2 3 4 min
5 6 7 8 min
4 4 4 5 min
3 5 5 6 min
m m m m 0
*m - max
Here it is the entire code:
#include <iostream>
using namespace std;
int main() {
int A[4][4];/*First array*/
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];/* find min on each row*/
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];/* here i create the new array 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 debugging:
5 4 3 1
5 6 7 9
4 2 3 9
4 8 4 6
0
How to fix it?
And how to put zero in the last element (as you can see in the first table for the new array).
You can do it in a single pass over all the elements:
// returns a (rows+1, cols+1) matrix
int* make_min_max_vectors(const int* arr, size_t rows, size_t cols) {
size_t out_size = (rows+1) * (cols+1);
int* res = malloc(out_size * sizeof(int));
// set up initial values in the right/bottom vectors
res[out_size - 1] = 0;
for (size_t col = 0; col < cols; ++col)
res[rows*(cols+1) + col] = INT_MIN;
for (size_t row = 0; row < rows; ++row)
res[row*(cols+1) + cols] = INT_MAX;
for (size_t row = 0; row < rows; ++row)
for (size_t col = 0; col < cols; ++col) {
const int* cell = &arr[row*cols + col];
res[row*(cols+1) + col] = *cell; // copy
if (*cell < res[row*(cols+1) + cols]) // min
res[row*(cols+1) + cols] = *cell;
if (*cell < res[rows*(cols+1) + col]) // max
res[rows*(cols+1) + col] = *cell;
}
}
return res;
}
That is, you simply run over all the input elements once, copying each one to the output plus checking if each one is less than its row minimum or greater than its column maximum. You don't need temporary vectors for min and max, and you don't need to run over the entire input twice.
John Swincks answer is awesome (+1'd) and I would definitely recommend his answer simply for future proofing your code. I am relatively new to programming myself and understand how intimidating the above code can seem (especially malloc). Because of this I have written the a version of your code that is very similar however gets rid of the need to have the original 4x4 matrix and instead uses a 5x5. The code is shown below, let me know if you have any issue with it. The code can be compiled as is and will demonstrate what you are trying to acheive.
#include <iostream>
int main()
{
// Initialised inline to simplify example.
int A[5][5] = {0};
// Only uses 4x4 of the 5x5 array meaning that you don't need to use two arrays.
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
std::cout << "Enter in value for row " << x << ", column " << y << ".\n";
std::cin >> A[x][y];
}
}
std::cout << "Input:" << std::endl;
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
std::cout << A[x][y] << "\t";
}
std::cout << "\n";
}
// Finds the max down each column in the array
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
if (A[x][4] < A[x][y])
A[x][4] = A[x][y];
}
}
// Finds the min across the array (along row)
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
// Assign the min to a value in that row.
A[4][y] = A[1][y];
if (A[4][y] > A[x][y])
A[4][y] = A[x][y];
}
}
std::cout << "Output:" << std::endl;
for (int x = 0; x < 5; ++x)
{
for (int y = 0; y < 5; ++y)
{
std::cout << A[x][y] << "\t";
}
std::cout << "\n";
}
std::cin.get();
std::cin.get();
return 0;
}
Edit: Sample input and output for clarification.
Input:
1 2 3 4
5 62 4 6
8 9 1 2
4 6 8 9
Output:
1 2 3 4 1
5 62 4 6 4
8 9 1 2 1
4 6 8 9 4
8 62 8 9 0