Adding rows the manual way in 2-D arrays - c++

User inputs desired amount of rows and columns, then if any row is found with even numbers the code should add a duplicate row below it. But instead I get no anwswer. Help needed. I only need to add rows with this manual method. I know their are much stable ways with vectors. But right now this is only way for me.
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> columns;
//-----------------------Generating 2-D array---------------------------------------------------------
int **array = new int*[2 * rows]; // allocating memory for the array
for (int i = 0; i < rows; i++)
array[i] = new int[columns];
//-----------------------Input Array Elements---------------------------------------------------------
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < columns; i++)
for (int j = 0; j < rows; j++)
std::cin >> array[i][j];
//--------------------Loop for the array output--------------------------------------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
std::cout << array[i][j] << " ";
}
std::cout << "\n";
}
//-------------------Loop for finding rows with even numbers And adding the clone rows----------------------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
if (array[i][j] % 2 == 0) {
for (int k = rows; k > i; k--) {
for (int j = 0; j < rows; j++) {
array[k][j] = array[k - 1][j];
;
}
}
}
for (int j = 0; j < rows; j++)
array[i + 1][j] = 0;
i++;
columns++;
}
}
std::cout << "\n";
//--------------------Loop for the answer OUTPUT--------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
std::cout << array[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
For example if we enter an array
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
The answer should be
1 1 1 1 1
2 2 2 2 2 -------> even
2 2 2 2 2 --------> Duplicate
3 3 3 3 3
4 4 4 4 4 ---------> Even
4 4 4 4 4 ----------> clone
5 5 5 5 5

Related

How to read the matrix from file and make some change in it and again write it to the file?

How to read the content of matrix from file to again in the matrix. so I can use it for updating some value in it and again copy this in the matrix ...
In this first, I check that file is empty or not if it is empty then I write a matrix with all values zero, if it is not empty then I read the value from the file and put it into the matrix and then I make some update in it and write it back to the file with updating values...but I unable to read the matrix from file.
Thanks in advance (beginner).
please help me, I tried it so much time but I unable to do it, It is part of my project.
#include <bits/stdc++.h>
using namespace std;
class block{
public:
void inc(int arr[][10])
{ int k;
cout<<"by how much value do you want to increment the array's values"<<endl;
cin>>k;
for(int i=0 ; i<10; i++)
for(int j =0 ; j<10 ; j++)
arr[i][j] = arr[i][j] + k;
for(int i=0 ; i<10; i++)
{
for(int j =0 ; j<10 ; j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void details(int arr[][10])
{
int m1 = 10;
int n1 = 10;
int arr1[10][10];
ifstream fin1;
fin1.open("array.txt", ios::ate);
if (!fin1) {
cerr << strerror(errno) << "\n"; // handle open errors
}
if (fin1.tellg() == 0) {
cout << "NULL" << endl;
ofstream fout1;
fout1.open("array.txt");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
fout1 << arr[i][j]; //if file is empty write
//matrix with zero value
arr1[i][j] = arr[i][j];
}
}
fout1.close();
}
else {
fin1 >> m1 >> n1;
for (int i = 0; i < m1; i++) // this is the code of reading of
for (int j = 0; j < n1; j++) //matrix from the file
{
fin1 >> arr[i][j];
arr1[i][j] = arr[i][j];
}
}
fin1.close();
inc(arr1); // updating matrix
ofstream fout2;
fout2.open("array.txt", ios::trunc | ios::out);
for (int i = 0; i < m1; i++)
for (int j = 0; j < n1; j++) { // write back to its file with
//updated value
arr[i][j] = arr1[i][j];
fout2 << arr[i][j];
}
fout2.close();
}
};
int main()
{
block b1;
int arr[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
arr[i][j] = 0;
b1.details(arr);
}
Here is an example of how to read a matrix from a file
#include <fstream>
#include <iostream>
#include <cstring>
int main() {
int arr[10][10];
for (std::size_t i = 0; i < 10; i++)
for (std::size_t j = 0; j < 10; j++)
arr[i][j] = 0;
std::size_t m1 = 10;
std::size_t n1 = 10;
std::ifstream fin1("array.txt");
if (!fin1) {
std::cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin1 >> m1 >> n1;
for (std::size_t i = 0; i < m1; i++) // this is the code of reading of
for (std::size_t j = 0; j < n1; j++) { //matrix from the file
fin1 >> arr[i][j];
}
fin1.close();
for (std::size_t i = 0; i < 10; i++) {
for (std::size_t j = 0; j < 10; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
}
with input file
10
10
1 2 3 4 5 6 7 8 9 0
11 2 3 4 5 6 7 8 9 0
21 2 3 4 5 6 7 8 9 0
31 2 3 4 5 6 7 8 9 0
41 2 3 4 5 6 7 8 9 0
51 2 3 4 5 6 7 8 9 0
61 2 3 4 5 6 7 8 9 0
71 2 3 4 5 6 7 8 9 0
81 2 3 4 5 6 7 8 9 0
91 2 3 4 5 6 7 8 9 0

Matrix with N columns and N rows , columns must have N-1 , N-2 etc value

I'm having trouble with a matrix . The requirements are:
Make a matrix with N columns and N rows, the first column must have the value N , the second column must have N-1, the third N-2 etc until the last column must have value 1.
For example: N[7][7] must be:
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
My code only gets : 7 6 6 6 6 6 6 for each row and column. How can I solve this?
#include <iostream>
using namespace std;
int main()
{
int m[24][24], i, j, k = 0;
int linia, coloana, piesa, lac=0;
int mutari = 0;
int ceva;
cout << "Cate linii si cate coloane ? :";
cin >> lac;
ceva = lac;
if (lac>1 && lac<25) {
for (i = 0; i < lac; i++)
{
for (j = 0; j < lac; j++)
{
m[i][0] = lac;
m[i][1] = ceva-1;
ceva = ceva;
m[i][j] = ceva - 1;
ceva = ceva;
if (i == j) {
m[i][j] = 2;
m[1][1] = 2;
}
};
};
for (i = 0; i < lac; i++)
{
for (j = 0; j < lac; j++)
{
cout << m[i][j] << " ";
}
cout << endl;
}
}
else cout << "Numarul de linii si coloane trebuie sa fie >=2 si <= 24" << endl;
return 0;
}
The following code should neatly initalize the matrix.
if (lac>1 && lac<25) {
for (i = 0; i < lac; i++)
{
ceva = lac;
for (j = 0; j < lac; j++)
{
m[i][j] = ceva;
ceva = ceva-1;
}
} // no need for ';' here
for (i = 0; i < lac; i++)
{
for (j = 0; j < lac; j++)
{
cout << m[i][j] << " ";
}
cout << endl;
}
}
At the beginning of each row, we use ceva = lac; to reset the value that will go into the matrix.
Immediately after setting the value, we decrease ceva by one.
I stripped out a lot of code whose purpose I did not understand.
After Tim Randall showed you how to fix your code, this is the C++ way of doing things:
#include <cstddef>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
std::cout << "Rows: ";
std::size_t rows;
std::cin >> rows;
std::cout << "Columns: ";
std::size_t cols;
std::cin >> cols;
std::size_t elements{ rows * cols };
std::vector<int> m(elements);
std::generate(m.begin(), m.end(), [&]() { return --elements % cols + 1; });
for (std::size_t y{}; y < rows; ++y, std::cout.put('\n'))
std::copy(m.begin() + y * cols, m.begin() + (y + 1) * cols,
std::ostream_iterator<int>(std::cout, " "));
// access m[y][x] with m[y * cols + x]
}
Make it easier on yourself and leverage the power of the standard library.
constexpr auto N = 7;
std::array<int, N> row{};
std::array<std::array<int, N>, N> matrix{};
std::iota(std::rbegin(row), std::rend(row), 1);
std::fill(std::begin(matrix), std::end(matrix), row);
std::array is just a very thin wrapper around c-style arrays, is slightly nicer to work with, and has nice features such as remembering its size.
std::iota fills a range with sequentially increasing values (in this case starting at 1). By using std::rbegin, we tell it to fill our row in reverse.
Finally we use std::fill to set each row of our matrix to row.
The code will work even on c++11, by replacing std::rbegin with .rbegin().

Swapping 2d array minimum row value with reverse diagonal

I am trying to swap minimum row value with reverse diagonal. I managed to print out every row minimum value, but my swap fails. Maybe you could give me some hints.
for (int i = 0; i < n; i++)
{
int min = mas[i][0];
for (int j = 1; j < m; j++)
{
if (mas[i][j] < min)
{
min = mas[i][j];
}
for(int k=n-1;k>0;k--){
for(int h = m-1; h>0;h--){
min = mas[i][j];
mas[i][j]=mas[k][h];
mas[k][h]=min;
}
cout << "New Matrix\n";
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << mas[i][j] << " ";
}
}
}
}
system("pause");
return EXIT_SUCCESS;
}
This is my for for a minimum value and later I am adding another for to swap values.
My result:
I go printed out 3 matrices and none of them are correctly swapping value. I guess it's because of for in for cycle?
My file with with 2d array:
1 2 5 // row min 1, reverse diagonal 5
2 8 9 // row min 2, reverse diagonal 8
5 9 10 // row min 5, revese diagonal 5
What output I expect:
5 2 1 // 5 diagonal swap with min = 1
8 2 9 // 8 diagonal swap with min = 2
5 9 10 // 5 diagonal no swap because 5 is row minimum
If I understand correctly then the "reverse diagonal" can be present only in a square matrix. So there is no sense to enter two values n and m to deal with a square matrix.
If to consider the example of a 3 x 3 matrix shown in your question and to use loops instead of for example standard functions std::max_element and std::swap then the code that converts the matrix can look the following way as it is shown in the demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 5 },
{ 2, 8, 9 },
{ 5, 9, 10 }
};
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N; j++)
{
std::cout << std::setw(2) << a[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << std::endl;
for (size_t i = 0; i < N; i++)
{
size_t min = 0;
for (size_t j = 1; j < N; j++)
{
if (a[i][j] < a[i][min]) min = j;
}
if ( min != N - i - 1 )
{
int tmp = a[i][min];
a[i][min] = a[i][N - i - 1];
a[i][N - i - 1] = tmp;
}
}
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N; j++)
{
std::cout << std::setw(2) << a[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << std::endl;
}
The program output is
1 2 5
2 8 9
5 9 10
5 2 1
8 2 9
5 9 10

Fill an array diagonally

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
*/

C++ Find min and max in each row and column in two-dimensional array

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