I am trying to create a program which prints a matrix of integers, but the output returns weird numbers before the actual matrix. There are no compiling errors.
This is what my code looks like: //ignore void function for now, focus on main function::
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int array[30][30], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d", array[i][j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i][j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i][j]);
}
printf("\n");
}
return 0;
}
And this is my output (I only want the last three lines)
123
-514159984327663
-51415932632766-514159305
1 2 3
4 5 6
7 8 9
You are missing a space in "%d" in printmatrix, and, more importantly, it is not proper to pass an int [n][n] array for an int [30][30] parameter unless n is 30.
Change void printmatrix(int array[30][30], int n) to void printmatrix(int n, int array[n][n]), and change printmatrix(ints2D, n); to printmatrix(n, ints2D);. That makes the type of the argument you are passing match the type of the parameter.
In your function args you defined the array as fixed size ([30][30]) but you are passing a VLA ([3][3] in your example) which makes it find uninitialized memory and why you are seeing the strange numbers.
#Eric Postpischil answer is spot on. Another way to solve it: 2d arrays could be flatten into 1d. Here is a working code for you:
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int *array, int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i * n + j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n * n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i * n + j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i * n + j]);
}
printf("\n");
}
return 0;
}
Related
I am wondering why I cannot get the value in the function, it always cause segmentation fault...
`
void multiply(int M, int N, int K, int **matrixA, int **matrixB, int **matrixC){
for (int i = 0; i < M; i++){
for (int j = 0; j < K; j++){
int sum = 0;
for (int k = 0; k < N; k++){
sum += (*(*(matrixA + j) + k)) * (*(*(matrixB + k) + j));
}
*(*(matrixC + i) + j) = sum;
}
}
}
int main(){
int M, N, K;
scanf("%d%d%d", &M, &N, &K);
int matrixA[M][N];
int matrixB[N][K];
int matrixC[M][K];
for(int i=0; i<M; i++){
for(int j=0; j<N; j++){
scanf("%d", matrixA[i]+j);
}
}
for(int i=0; i<N; i++){
for(int j=0; j<K; j++){
scanf("%d", matrixB[i]+j);
}
}
multiply(M, N, K, (int **)matrixA, (int **)matrixB, (int **)matrixC);
for(int i=0; i<M; i++){
for(int j=0; j<K; j++){
printf("%d ", matrixC[i][j]);
}
printf("\n");
}
return 0;
}
`
I want to print out the result "matrixC", but in the function, it would cause segmentation fault. I have tried several times, and it seems like it would miss the addresses of the pointer under the double pointers.
Change the prototype of the function multiply to this:
void multiply(int M, int N, int K, int matrixA[M][N], int matrixB[N][K], int matrixC[M][K]);
make your life easier like this (body of function multiply):
for (int i = 0; i < M; i++) { //for each row of matrixA
for (int j = 0; j < K; j++) { //for each column of matrixB
matrixC[i][j] = 0; //set field to zero
for (int k = 0; k < N; k++) { //for each col of A and each row of B
//take the dot product of row i (matrixA) and col j (matrixB)
matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
You have an error in this line
sum += (*(*(matrixA + j) + k)) * (*(*(matrixB + k) + j));
which has been corrected to
matrixA[i][k] //index 'i' not 'j'
The var sum is not needed, therefore opted out.
Based on your comment below
Consider the following code:
int arr[2][2];
int n=0;
for (int i=0; i < 2; ++i) {
for (int j=0; j < 2; ++j) {
arr[i][j] = ++n;
printf("%p (%d) ", &arr[i][j], arr[i][j]);
}
printf("\n");
}
Possible output:
0x7fff7c729470 (1) 0x7fff7c729474 (2)
0x7fff7c729478 (3) 0x7fff7c72947c (4)
As you can see, nicely packed into consecutive integers (basically one array of ints - but that is not guaranteed).
Now have a look at this:
int **parr = (int**) arr;
for (int i=0; i < 2; ++i) {
for (int j=0; j < 2; ++j) {
printf("%p ", *(parr + i) + j);
}
printf("\n");
}
Possible output:
0x200000001 0x200000005
0x400000003 0x400000007
Now, that looks (dangerously) ugly.
As always: pointer != array. Pointer to pointer means, an address of another address, whereas an array is a consecutive block of a type (you could for example take the address of the first element, which is done if the array decays to a pointer).
You have to give the compiler enough information, e.g.
int (*parr)[2] = arr;
See also: https://en.cppreference.com/w/c/language/array#Multidimensional_arrays
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.
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;
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;
}
I am trying to send a pointer of a matrix to function for printing the values. However, my following code prints some long numbers so I assumed it prints the addresses instead! How can I print the value of the matrix after passing the pointer of it?
#include <stdio.h>
#include <string.h>
#include <math.h>
void printing(int *edge);
void main(){
int N=3;
int i,j;
int *edge[N];
for (i = 0; i < N; i++){
*(edge+i) = (int *)malloc(N * sizeof(int));
}
srand(0);
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
if(i == j)
*(*(edge+i)+j) = 0;
else
*(*(edge+i)+j) = 1; //rand() % 10;
}
}
printing(edge); // Pass the pointer of the matrix
}
void printing(int *edge){
int i,j;
int N= 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", ((edge+i)+j)); // I think I have to do something in this line.
}
printf("\n");
}
}
The parameter type of printing is incorrect. It should be int *edge[]. Then when you print, use *(*(edge+i)+j), or better yet edge[i][j].
The result:
void printing(int *edge[]){
int i,j;
int N = 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", edge[i][j]);
}
printf("\n");
}
}
Also, be sure to #include <stdlib.h>, as it's needed for malloc and srand.