I want to make a program that converts an array into matrix. I made something that sort of works but I'm not quite satisfied with the result. I am getting output that actually makes sense but I don't know what I should change to get the one I want.
The size of matrix doesn't have to be 4x4 it can be whatever in fact I want to make one that can make me a 4x4 out of an array[15].
#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
typedef unsigned int uint;
void print(const std::vector<int>& array)
{
for(uint i=0; i< array.size(); i++)
std::cout << array[i] << " ";
std::cout << "\n" << std::endl;
}
void random(std::vector<int>& array, int size = 4)
{
srand(time(0));
std::vector<std::vector<int>> mat;
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
array.push_back(rand()%10);
}
mat.push_back(array);
}
print(array);
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
}
main()
{
std::vector<int> A;
random(A);
}
Example of an output since it's random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
The one i would like to get would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After you've added array to mat with mat.push_back(array), you need to clear array, otherways array will always grow.
std::vector<std::vector<int>> mat;
for (uint i = 0; i < 4; i++)
{
for (uint j = 0; j < 4; j++)
{
array.push_back(rand() % 10);
}
mat.push_back(array);
array.clear(); // <<<< add this
}
Change the code that prints mat to this:
for (uint i = 0; i < mat.size(); i++)
{
for (uint j = 0; j < mat[i].size(); j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
and you'll see what happens if you don't call array.clear() as shown above.
Related
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
So, I am trying to print an array Fi[rows][columns] in a table format of 11 rows and 11 columns. The code I am using is:
for (int i = 0; i < rows ; i++){
for (int j=0; j< columns; j++)
std::cout << Fi[i][j]<<"\t";
std::cout << "\n";
}
And my problem is, that the element printed on Fi[0][10] is not the one that it's supposed to be. Actually, if I simply print out
std::cout<< Fi[0][10];
I get the correct value.
Can someone help me to figure out what I am doing wrong?
Are you looking for something like this? I changed the part std::cout << "\n"; to std::cout << std::endl;
#include <array>
int main() {
int rows = 4;
int columns = 5;
int my_array[rows][columns] = {{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,13},{14,15,16,17,18}};
for (int i = 0; i < rows ; i++){
for (int j=0; j< columns; j++)
std::cout << my_array[i][j]<<"\t";
std::cout << std::endl;
}
return 0;
}
With the output:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 13
14 15 16 17 18
using namespace std;
#include<iostream>
int main(){
int num[3][3],i=0,sum=0;
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>num[i][j];
sum+=num[i][j];
}
}
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<num[i][j]<<"\t";
}
cout<<endl;
}
cout<<sum<<endl;
return 0;
}
I am attempting to print out an array in a specific order, where it is formatted with columns and rows in ascending order, but with the bottom row containing the lowest values. The array is created via a for loop and a pointer.
here is my code so far:
#include <iostream>
#include <iomanip>
int main()
{
// Creation of the array
int* array = new int[24];
for (int i = 0; i < 24; i++)
{
array[i] = i;
}
// Displaying in grid format with 3 rows and 8 columns
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::setw(2) << *array << ' ';
array++;
}
std::cout << '\n';
}
}
The output of my code is:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
The desired output is:
16 17 18 19 20 21 22 23
8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
How would I go about "reversing" the rows to reach the desired output?
SOLVED WITH THE FOLLOWING
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int* table = new int[40];
for (int i = 0; i < 40; ++i)
{
table[i] = i;
}
for (int i = 4; i >= 0; --i)
{
for (int j = 0; j < 8; ++j)
{
cout << setw(2) << table[j + (8 * i)] << ' ';
}
cout << "\n";
}
}
Thanks for the help.
I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.
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