looping matrix ordo 4X4 - c++

i have problem with code,where i can't looping data in c++.
so here my code:
int j,i;
int matriks[5][5];
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
matriks[i][j]=j;
}
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
cout<<matriks[i][j]<<" ";
cout<<endl;
getch();
}
the code above only shows
1 2 3 4
i want output like this:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
where is wrong code,how to create looping matrix ordo 4x4?

Try this code; I did not compile or test, but it looks like what you are trying to achieve.
int matrix[5][5];
for (int i = 0, k = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
matrix[i][j] = k++;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}

Related

Finding min and max elements of every column and row in a matrix C++

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

(C++) Removing the rows with duplicates in a matrix bug

Remove the rows where a value is encountered twice from the matrix. Where's the bug in my code that I've already written, so that I do it without using any function or anything that would make it easier?
My code:
#include <iostream>
using namespace std;
int main()
{
int v[99][99], m, n, i, j, k, vlinie[99], a=0;
cout<<"m="; cin>>m;
cout<<"n="; cin>>n;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
cin>>v[i][j];
//finding out each row that has doubles and remembering its index in an array called vlinie
for(i=1; i<=m; i++)
for(j=1; j<=n-1; j++)
for(k=j+1; k<=n; k++)
if(v[i][j]==v[i][k])
{
a++; //a is the number of numbers in vlinie
vlinie[a]=i;
}
//removing duplicates from vlinie, in case there are any
for(i=1; i<=a-1; i++)
for(j=i+1; j<=a; j++)
if(vlinie[i]==vlinie[j])
for(k=i; k<=m; k++)
{
vlinie[k]=vlinie[k+1];
a--;
}
//this is where we move the rows around, and supposedly the line where i got it wrong, so what do i change in this line?
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
for(i=1; i<=m; i++)
{
cout<<endl;
for(j=1; j<=n; j++)
cout<<v[i][j]<<" ";
}
return 0;
}
Example:
1 2 2 3
4 5 6 7
7 7 8 9
7 6 5 4
What it should output:
4 5 6 7
7 6 5 4
0 0 0 0
0 0 0 0 (without the zeros theoretically but the way i wrote it leaves it like this, for now)
What it outputs instead:
1 2 2 3
7 7 8 9
5 4 3 2 (it didn't delete the lines that it should delete and instead deleted some line it should keep)
How do I fix this?
PS: yes, i know i shouldn't have started my indexing at 1 instead of 0, no need to remind me about it again
Your problem was at these lines :
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
There is a first issue with the indices: we should copy v[k+1][] to v[k][].
The second issue is that once a first move has been performed, the table vlinie is no longer valid.
This last point is not so easy to correct.
Moreover, for each suppressed row, you are implementing a shift of several rows, which leads to a poor efficiency.
It is simpler and more efficient to memorize which rows are suppressed (suppress[]) and from this table, to determine which row of the input matrix will end at the ith row of the final matrix (table posi[]).
Hereafter is an example of implementation.
#include <iostream>
int main()
{
int v[99][99], m, n, i, j, k, posi[99];
bool suppress[99];
std::cout << "m = "; std::cin >> m;
std::cout << "n = "; std::cin >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
std::cin >> v[i][j];
//finding out each row that has doubles
for (i = 0; i < m; i++) {
suppress[i] = false;
for (j = 0; (j < n-1) && !suppress[i]; j++)
for (k = j+1; (k < n) && !suppress[i]; k++)
suppress[i] = (v[i][j] == v[i][k]);
}
// determination of the moves to be performed
int n_keep = 0;
for (i = 0; i < m; i++) {
if (!suppress[i])
posi[n_keep++] = i;
}
//this is where we move the rows around
for (i = 0; i < n_keep; i++)
for (j = 0; j < n; j++)
v[i][j] = v[posi[i]][j];
// Set to 0 remaining rows
for (i = n_keep; i < m; i++)
for (j = 0; j < n; j++)
v[i][j] = 0;
for(i = 0; i < m; i++)
{
std::cout << "\n";
for(j = 0; j < n; j++)
std::cout << v[i][j] << " ";
}
return 0;
}

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

Sort 2D Array Row's Depend on the last column

I want to Write a Program in c++ that swap the row but my code only sort the element of the array.
For example:
I made a array of 3*5.
I input data as follows
1 2 3 4 10
5 6 7 8 26
9 1 2 3 15
Now I want to swap the rows by ascending order depends on the last column.i.e.
5 6 7 8 26
9 1 2 3 15
1 2 3 4 10
My code Only Sort the Row form left to Right.
int arr[3][5], sum, temp;
for(int i =0; i<3; i++)
{
sum=0;
for(int j =0; j<4; j++)
{
cout << "Enter "<<j+1<<" Number of "<<i+1<<" Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4]=sum;
}
for(int i = 0; i<3; i++)
{
for(int j = 0; j<5; j++)
{
cout << arr[i][j]<<"\t";
}
cout << endl;
}
cout<<"----------------"<<endl;
for(int i=0;i<3;i++)
{
for(int j = 0;j<4;j++)
{
for(int k =0;k<4;k++)
{
if(arr[i][j]<arr[i][k])
{
temp=arr[i][j];
arr[i][j]=arr[i][k];
arr[i][k]=temp;
}
}
}
}
for(int i =0;i<3;i++)
{
for(int j =0;j<5;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
If performance is not of significant concern, just use the same idea as Bubble sort.
Compare last column element between each pair of rows, and if not in order swap the rows. This can be done with a loop and a temporary variable.
if(matrix[i][cols-1] > matrix[j][cols-1])
{
int temp ;
for(int k = 0; k < cols; ++k)
{
temp = matrix[i][k] ;
matrix[i][k] = matrix[j][k] ;
matrix[j][k] = temp ;
}
}
Since you are using C++, you can use std::array if your matrix has fixed size rows and columns. i.e.
std::array<std::array<int, colums>, rows> matrix ;
Here is a working example which uses std::swap to do the swapping of rows. The sorting routine is essentially the bubble sort.
for(int i = 0; i < rows; ++i)
for(int j = 0; j < i; ++j)
if(matrix[j][cols-1] > matrix[i][cols-1])
std::swap(matrix[i], matrix[j]);
Maybe this is your answer:
int arr[3][5], sum, temp;
for (int i = 0; i<3; i++)
{
sum = 0;
for (int j = 0; j<4; j++)
{
cout << "Enter " << j + 1 << " Number of " << i + 1 << " Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4] = sum;
}
cout << endl << endl;
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << "----------------" << endl << endl;
int tmp[5],i,j,k;
for ( i = 0; i<3; i++)
{
for ( j = 0; j<3; j++)
{
if (arr[j][4]<arr[i][4])
{
for ( k = 0; k <= 4; k++){
int t = arr[i][k];
arr[i][k] = arr[j][k];
arr[j][k] = t;
}
}
}
}
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}

Block Matrix Transpose

I wanted to implement transposition of a matrix by dividing the input matrix into blocks and then transposing them. I referred to the corresponding post A Cache Efficient Matrix Transpose Program? and wrote my code like this:
#include<iostream>
#include<stdlib.h>
#define m 4
#include<sys/time.h>
#include<time.h>
#include<malloc.h>
using namespace std;
int **a, **b, **c;
int count = 0;
clock_t t1, t2;
int blocksize = 2;
int main(){
a = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
a[i] = (int *)malloc(m*sizeof(int));
}
b = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
b[i] = (int *)malloc(m*sizeof(int));
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i][j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
t1 = clock();
// MAIN BLOCK TRANSPOSE CODE
for (int i = 0; i < m; i += blocksize) {
for (int j = 0; j < m; j += blocksize) {
for (int k = i; k < i + blocksize; ++k) {
for (int l = j; l < j + blocksize; ++l) {
b[k + l*m] = a[l + k*m];
}
}
}
}
t2 = clock();
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << b[i][j] << "\t";
}
cout << "\n";
}
free(a);
free(b);
cout << "\n";
cout << (double)(t2-t1)/CLOCKS_PER_SEC << "\n";
return 0;
}
However, the code is not working as expected. I implemented the code that is said to be working in the corresponding post. Please help if possible.
Input Array:
0 3 6 9
2 5 8 11
4 7 10 13
6 9 12 15
Expected Output Array:
0 2 4 6
3 5 7 9
6 8 10 12
9 11 13 15
Obtained Result:
0 3 6 9
Segmentation fault
I think your matrix is supposed to be encoded in a single array, not in an array of arrays (See the Edit 2 of the linked question).
You might want to try that instead:
int *a, *b, *c;
a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i*m+j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i*m+j] << "\t";
}
cout << "\n";
}
cout << "\n";