How to get access to each row and column in rectangular matrix - c

Good day to everybody. My task is to determine if a rectangular matrix has two rows of positive elements. I write the code below. At end I try to chect statement about positive row, but it's not working at all. Please explain me how to correct get the access to the each row and column in matrix, and meaybe edit my code.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}

When counting the number of positive elements in a row, you need to set the count back to zero for each row.
So instead of:
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
you need
for (i = 0; i < M; i++){
count = 0;
for (j = 0; j < N; j++) {
if (a[i][j] > 0){
count++;
}
}
printf("row %d has %d positive elements\n", i, count);
}

Related

How to transpose a matrix without using another matrix?

Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.

Arranging columns in a matrix lexicographically

I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
int mat[10][10];
void print_matrix()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (mat[i][j] < mat[k][j])
{
a = mat[i][j];
mat[i][j] = mat[k][j];
mat[k][j] = a;
}
}
}
}
}
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int(mat[m][n]);
print_matrix(mat[m][n]);
arrange(mat[m][n]);
print_matrix(mat[m][n]);
return 0;
}
Try this solution(will work for input 0-8 only), also used global variables:
There have multiple solutions. but is the easiest one.
I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.
Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
int mat[10][10];
int generatedNumber[10];
void print_matrix()
{
printf("The matrix is:\n");
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
int number = 0;
for (i = 0; i < m; ++i)
{
number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
}
generatedNumber[j] = number;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( generatedNumber[j] > generatedNumber[j+1])
{
// swap the elements
int temp = generatedNumber[j];
generatedNumber[j] = generatedNumber[j+1];
generatedNumber[j+1] = temp;
}
}
}
for(i = 0; i < n; i++)///columwise
{
int generatedColumnvalue = generatedNumber[i];
for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
{
mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
generatedColumnvalue/=10;
}
}
}
int main()
{
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int();
print_matrix();
arrange();
print_matrix();
return 0;
}

Determine if a rectangular matrix has two rows of positive elements

I need to determine if a rectangular matrix has two rows of positive elements in C. I write part code for the set matrix and output its elements. I don't know how to check the positive elements in the row. Please help me
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main() {
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++) {
printf("%d line:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
_getch();
return 0;
}
So, I make some changes in my code after reading comments. Thanks a lot. But it's not working.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //обьявление матрицы 3 строки и 4 столбца
int i, j; // индексы строки и столбца
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}
What you need to do is to set a counter to 0 . Every time all the values are positive in row add 1 your counter. You can do that by a loop. If a value of your matrix a[i][j] is less than 0 just continue and move on to then next row.

C language 2d array fill diagonal with numbers from 1 to n

I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;

How to transpose matrix with malloc and print it

I want my code to scan value of rows and cols and then to print the matrix (I don't care from the values in the matrix) and to print the transpose matrix.
What is my problem with mine code?
the code :
#include <stdio.h>
#include <string.h>
void main()
{
int rows1, colums1, i, j;
int**matrix1, **transpose_matrix1;
printf_s("enter rows and colums\n");
scanf_s("%d%d", &rows1, &colums1);
matrix1 = malloc(sizeof(int*)*rows1);
for (i = 0; i < rows1; i++)
matrix1[i] = malloc(sizeof(int)*colums1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
matrix1[i][j] = i+j;
puts("matrix: ");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < colums1; j++)
{
printf_s("%d ", matrix1[i][j]);
}
printf_s("\n");
}
transpose_matrix1 = malloc(sizeof(int*)*colums1);
for (i = 0; i < colums1; i++)
transpose_matrix1[i] = malloc(sizeof(int)*rows1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
transpose_matrix1[j][i] = matrix1[i][j];
puts("transpose matrix:");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < colums1; j++)
{
printf_s("%d ", transpose_matrix1[i][j]);
}
printf_s("\n");
}
}
Input:
2 3
0 1 2
1 2 3
Output:
enter rows and colums
matrix:
transposematrix:
0 1 -33686019
1 2 -33686019
When you initialize the transpose matrix you do it like
transpose_matrix1[j][i] = matrix1[i][j];
Note the order in which you use j and i in transpose_matrix1[j][i].
Then when you print you use transpose_matrix1[i][j]. Note that the order of j and i have changed, even though the looping around both are the same. You need to use the same order for j and i in both loops.
You have confused yourself a little with what is a colum and what is a row
here is a fixed version:
void print_matrix(int ** matrix, int rows, int colums){
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < colums; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int transpose_rows1, transpose_colums1, rows1, colums1, i, j;
int**matrix1, **transpose_matrix1;
printf("enter rows and colums\n");
scanf("%d%d", &rows1, &colums1);
matrix1 = (int**)malloc(sizeof(int*)*rows1);
for (i = 0; i < rows1; i++)
matrix1[i] = (int*)malloc(sizeof(int)*colums1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
matrix1[i][j] = i+j;
puts("matrix: ");
print_matrix(matrix1, rows1, colums1);
transpose_rows1 = colums1;
transpose_colums1 = rows1;
transpose_matrix1 = (malloc(sizeof(int*) * transpose_rows1));
for (i = 0; i < transpose_rows1; i++)
transpose_matrix1[i] = (malloc(sizeof(int) * transpose_colums1));
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
transpose_matrix1[j][i] = matrix1[i][j];
puts("transpose matrix:");
print_matrix(transpose_matrix1, transpose_rows1, transpose_colums1);
return 0;
}
In order to not confuse yourself in the future, try to use function and names with the exact meaning.
It is easier to create a function to print the array than to write the code twice and then thinking twice about what is the row now and what is the column.
And also, it makes it easier if the variable name colums1 is always a column of a matrix, not a column on one and a row on the other, so just create a new variable for the second array column and row with the correct data.
In a transponsed matrix, the number of rows is the number of columns of the normal matrix and the number of columns is equal to the number of rows of the normal matrix. Therefore, i should go from 0 to columns1 and j from 0 to rows1:
for (i = 0; i < colums1; i++)
for (j = 0; j < rows1; j++)
transpose_matrix1[i][j] = matrix1[j][i];
puts("transpose matrix:");
for (i = 0; i < colums1; i++)
{
for (j = 0; j < rows1; j++)
{
printf("%d ", transpose_matrix1[i][j]);
}
printf("\n");
}

Resources