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;
Related
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;
}
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);
}
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.
The aim of the matrixMultiplier function is to multiply A * B, and then show the resulting matrix C, for any two square matrices A and B. A and B sizes are limited by 10*10.
When the user is asked the size of the matrix they wish to introduce, although A and B are 10*10, the function only works with the submatrices in A and B up to the dimension which the user has specified, say 3*3.
I've tested this out multiplying the 3*3 identity matrix by itself and it's not giving me the correct answer.
#include <stdio.h>
void matrixMultiplier(int A[][10], int B[][10], int C[][10], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
C[i][k] = 0; /* Initialize output matrix to zero */
}
}
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
for (j = 0; j < n; j++) {
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\n");
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
printf("%4d", C[i][k]);
}
printf("\n");
}
}
int main(void) {
int A[][10] = {{0}};
int B[][10] = {{0}};
int C[][10] = {{0}}; /* Initialize output matrix to zero */
int i, j;
int n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &A[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", A[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &B[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", B[i][j]);
}
printf("\n");
}
matrixMultiplier(A, B, C, n);
return 0;
}
You need to use statically allocated arrays like this:
int A[10][10] = {{0}};
int B[10][10] = {{0}};
int C[10][10] = {{0}}; /* Initialize output matrix to zero */
As other commentators noticed, in C int A[][10] = {{0}}; is basically an equivalent of int A[1][10] = {{0}};, so only first rows are correctly set.
Take a look at this code:
#include <stdio.h>
#define MAX_SIZE 10
void matrixMultiplier(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int n)
{
int i, j, k;
for (i = 0; i < n; i++)
for (k = 0; k < n; k++)
for (j = 0; j < n; j++)
C[i][k] += A[i][j] * B[j][k];
printf("\nProduct Matrix C:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", C[i][j]);
putchar('\n');
}
}
int main(void)
{
//int A[][10] = {{0}}; By initializing like this you are creating this int A[1][10] = {{0}};
int A[MAX_SIZE][MAX_SIZE] = {0};
int B[MAX_SIZE][MAX_SIZE] = {0};
int C[MAX_SIZE][MAX_SIZE] = {0};
int i, j, n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
printf("Input %d values row-wise, for matrix A: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &A[i][j]);
printf("Input %d values row-wise, for matrix B: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &B[i][j]);
printf("\nGiven Matrix A:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
printf("\nGiven Matrix B:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
matrixMultiplier(A, B, C, n);
return 0;
}
If you need more explanation about 2D arrays refer to this link
I need to make a little project but I completely don't know how. Im giving matrix A of size n, and it have to return me matrix B which is matrix A with zeroed first and penultimate column. All I did is
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,j,;
int tab[n][n];
printf("Size of matrix:");
scanf("%d",&n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
printf("A[%d][%d]=",i,j);
scanf("%lf",&tab[i][j]);
}
printf("Data:");
printf("Matrix A[%d][%d]",n,m);
}
Which I think should let me to type my matrix. What I should do next? Please help me.
There are a lot of errors in your code, the variable m is not declared, the double array is declared with n non-initialized. As the size of matrix is only known at runtime (entered by user), you need to use dynamic memory allocation functions to allocate memory for your matrix.
Try this code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j, n;
printf("Size of matrix: ");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int)*n*n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("A[%d][%d]=",i,j);
scanf("%d",(tab+i*n+j));
}
}
for (i = 0; i < n; i++)
{
*(tab+i*n) = 0;
*(tab+i*n+n-2) = 0;
}
//Print tab
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", *(tab+i*n+j));
}
printf("\n");
}
return 0;
}