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;
}
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;
}
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;
My code shows segmentation fault on hackerrank.What will happen if I use long long int?
Here is the link https://www.hackerrank.com/challenges/crush/problem?isFullScreen=true
My code is:
#include <stdio.h>
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int queries[m][3];
for (int i = 1; i <= m; i++) {
scanf("%d", &queries[i][1]);
scanf("%d", &queries[i][2]);
scanf("%d", &queries[i][3]);
}
int a[n];
for (int i = 1; i <= n; i++)
a[i] = 0;
for (int i = 1; i <= m; i++) {
for (int j = queries[i][1]; j <= queries[i][2]; j++)
a[j] = a[j] + queries[i][3];
}
int max;
max = 0;
for (int i = 1; i <= n; i++) {
if (max < a[i])
max = a[i];
}
printf("%d", max);
return 0;
}
Array index out of bounds at .
a[j]=a[j]+queries[i][3];
because Array index 3 is past the end of the array (which contains 3 elements) thus 2 is the last index.
and when using the for loops to access the array you might need to start from 0
Code
#include<stdio.h>
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int queries[m][3];
for( int i=0;i<m;i++)
{
scanf("%d",&queries[i][0]);
scanf("%d",&queries[i][1]);
scanf("%d",&queries[i][2]);
}
int a[n];
for(int i=0;i<n;i++)
a[i]=0;
for(int i=0;i<m;i++)
{
for(int j=queries[i][0];j<=queries[i][1];j++)
a[j]=a[j]+queries[i][2];
}
int max;
max=0;
for(int i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("%d",max);
return 0;
}
You have array index out of bounds error at this line:
a[j] = a[j] + queries[i][3];
What value can i take?
To avoid similar errors in the future you may want to consider usage of tools for static code analysis. For example, cppcheck is a free and open source tool that can detect this error:
stackoverflow/c-issue.c:23:34: error: Array 'queries[2147483648][3]' accessed
at index queries[*][3], which is out of bounds.
[arrayIndexOutOfBounds]
a[j] = a[j] + queries[i][3];
In every loop you are passing boundaries of your array which will cause undefined behavior.
when you declare int arr [num] , if you access arr[x] while x=>num , you are passing boundaries of your array and that will cause undefined behavior , which means you can't predict what will happen.
look
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int queries[m][3];
for (int i = 0; i < m; i++) {
scanf("%d", &queries[i][0]);
scanf("%d", &queries[i][1]);
scanf("%d", &queries[i][2]);
}
int a[n];
for (int i = 0; i < n; i++)
a[i] = 0;
for (int i = 0; i < m; i++) {
for (int j = queries[i][0]; j <= queries[i][1]; j++)
a[j] = a[j] + queries[i][2];
}
int max;
max = 0;
for (int i = 0; i < n; i++) {
if (max < a[i])
max = a[i];
}
printf("%d", max);
return 0;
}
I need help. I want to learn how to create and use dynamic matrix which is element of structure, I want to fill matrix with zeros (0) and print it out, I tried many ways but no luck. Here is the code
#include <stdio.h>
#include <stdlib.h>
typedef struct matrica
{
int **mat;
int dim; //this is dimension of squared matrix
}MATRICA;
void form_matrix(MATRICA *matrica);
int main()
{
MATRICA matrix;
form_matrix(&matrix);
return 0;
}
void form_matrix(MATRICA *matrica)
{
int i, j;
MATRICA *br;
do
{
printf("Size of matrix ");
scanf("%d", &br->dim);
}while(br->dim < 4 || br->dim > 6);
matrica->mat = (int **) calloc(br->dim, sizeof(int *));
for(i = 0; i < br->dim; i++)
{
matrica->mat[i] = (int *) calloc(br->dim, sizeof(int));
for(j = 0; j < br->dim; j++)
{
matrica->mat[i][j] = 0;
}
}
for(i = 0; i < br->dim; i++)
for(j = 0; j < br->dim; j++)
printf("%d ", matrica->mat[i][j]);
}
what am I doing wrong, my loop inside function goes only once, can someone explain to me why?
Your program exhibits undefined behavior because you are dereferencing an uninitialized pointer br. You don't need it, you simply need a variable to store your dimension input.
int i, j, dim;
do
{
printf("Size of matrix ");
if (scanf("%d", &dim) != 1) {
printf("scan failed\n");
exit(EXIT_FAILURE);
}
}while(dim < 4 || dim > 6);
matrica->dim = dim;
/* ... replace all instances of br->dim with dim */
I want to create 2D array using structure and read from user, then display it. But I can't find out what's wrong with this.
/Sorry guys.First, I want it to read a matrix elements from user, then I want to display whole matrix. But it doesn't print properly, it prints address of elements. Why is it printing address, not value? /
Here is my code:
#include <stdio.h>
struct Matrix {
int n,m;
int a[100][100];
}array;
int main() {
struct Matrix *p;
int i,j;
p = malloc(sizeof(array));
scanf("%d%d", &p->n, &p->m);
for (i = 0;i < p->n;i++)
for (j = 0;j < p->m;j++)
scanf("%d", p->a[i][j]);
for (i = 0;i < p->n;i++){
for (j = 0;j < p->m;j++)
printf("%d ", (p->a[i][j]));
printf("\n");
}
}
Change scanf("%d", p->a[i][j]); to scanf("%d", &p->a[i][j]);. Better to check the result of scanf() to ensure code received the expected data.
if (scanf("%d", &p->a[i][j]) != 1) Oops();
Enable all warning on your compiler. This problem popped up right away.
#include <stdio.h>
struct Matrix {
int n, m;
int a[100][100];
} array;
int main() {
struct Matrix *p;
int i, j;
p = malloc(sizeof(array));
scanf("%d%d", &p->n, &p->m);
for (i = 0; i < p->n; i++)
for (j = 0; j < p->m; j++) {
// scanf("%d", p->a[i][j]);
scanf("%d", &p->a[i][j]);
}
for (i = 0; i < p->n; i++) {
for (j = 0; j < p->m; j++)
printf("%d ", (p->a[i][j]));
printf("\n");
}
}