How to initialize, create matrix and then print with using structure - c

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");
}
}

Related

How can I move all elements in an array with a K number and make the numbers rotate when it comes to the end?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}

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;

array manipulation question on hackerrank

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;
}

Matrix columns zeroing, C language

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;
}

Dynamic matrix inside struct, C programming

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

Resources