So I have task like this:
"Write a program that will calculate determinants of matrixes. Size of the matrix should be typed as a command line parameter of the program. The user should type the matrix elements from the keyboard once the program is executed. The determinants should be calculated for different square matrix sizes (<=3) and not for the single fixed one. Divide your program into several functions. Use pointers. The size of arrays you'll use is determined by user during the program execution, therefore there is no need to use dynamic memory allocation."
I've heard that it's not possible to do this task with command line parameters, but not dynamic memory allocation. I'm just a beginner, so I wouldn't know. I haven't gotten to the part with calculating the determinants, I only wrote the functions to input and print a matrix, but there are already problems. I'm really lost as to what should I do.
This is what I have so far (it is not compiling right):
void inputMatrix(int size);
void printMatrix(int *matrix, int size);
int main(int argc, char *argv[])
{
int size = atoi(argv[1]);
int *matrix;
inputMatrix(size);
printMatrix(*matrix, size);
return 0;
}
void inputMatrix(int size)
{
int i, j;
int *matrix;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
scanf("%d", (*(matrix + i) + j));
}
}
}
void printMatrix(int *matrix, int size)
{
int i, j;
int *matrix;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
printf("%d ", *(*(matrix +i) + j));
}
printf("\n");
}
}
The answer by Joni addresses the main issue, but there are other things to be fixed in OP's code.
For starters, we have to decide whether to use a an array of arrays (like int mat[3][3];) or a simple array (like int mat[9];), while in OP's code, there's some confusion about it:
int main(int argc, char *argv[])
{
// ...
int *matrix; // <-- This pointer is uninitialized, its value is indeterminated
// ...
printMatrix(*matrix, size);
// ^ dereferencing it, you are passing an 'int'
}
// ...
void printMatrix(int *matrix, int size)
{ // ^ a pointer to an int is expected
int i, j;
int *matrix; // <-- This is a local variable that will shadow the parameter
// with the same name and it is also uninitialized.
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
printf("%d ", *(*(matrix +i) + j));
// ^^^^ this is equivalent to 'matrix[i][j]',
// but 'matrix' is only a pointer to 'int'
}
printf("\n");
}
}
So, if any dynamic memory allocation must be avoided, we can write somthing like this:
// ...
#define MAX_SIZE 3
int main(int argc, char *argv[])
{
int matrix[MAX_SIZE][MAX_SIZE];
// read 'size' from command line arguments, then
inputMatrix(size, matrix); // <-- Note that I'm passing 'matrix' here too.
printMatrix(size, matrix);
// ...
}
// The functions must be modified accordingly, e.g.:
void printMatrix(int size, int matrix[][MAX_SIZE])
// The inner dimension must be specified ^^^^^^
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
printf("%d ", *(*(matrix + i) + j)); // <-- "Use pointers." they said
}
printf("\n");
}
}
In case you want to use a plain array instead, it can be written like this:
// ...
#define MAX_SIZE 9 // <-- the total size: 3*3
int main(int argc, char *argv[])
{
int matrix[MAX_SIZE];
// ...
printMatrix(size, matrix);
// ...
}
void printMatrix(int size, int matrix[])
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
printf("%d ", *(matrix + (i * size + j));
// Note the math ^^^^^^^^^^^^^^^^^
// In this simple case of row wise traversal, it could be as simple as
// *(matrix++)
}
printf("\n");
}
}
Since you're not allowed to use dynamic memory allocation, you'll have to create the matrix in the main function and pass it to the other functions as a parameter. This uses a feature of C called variable length arrays:
int matrix[size*size];
inputMatrix(size, matrix);
If variable length arrays are not allowed either, just make it matrix[9] - size is at most 3 in your assignment
Finding determinant of square matrix of order n (n is finite) is quite easy with dynamic memory allocation. But, what is important in this case, is to free the memory at the end of program.
You can view my code here!
Related
I have two functions. One that creates a multiplication table of a given number and the other function prints the array out. Below is my code:
Here's the error (Line 18):
expression must be a pointer to a complete object type
How do I fix this error and print the array? Also, I don't know how to print a new line after every row.
#include "multiplication.h"
#include <stdio.h>
int arr[][];
void mulitpication(int num){
/* initialize array and build*/
int arr[num][num];
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int arr[][]){
/* print the arr*/
int i;
for(i=0;i<sizeof(arr);i++){
for(int j=0;j<sizeof(arr);j++){
printf("%d ",arr[i][j])**(line 18)**;
}
}
}
If using C99 or later with VLA support, pass into the print function the dimensions needed.
// void print(int arr[][]){
void print(size_t rows, size_t cols, int arr[row][col]){
size_t r,c;
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < cols; c++) {
printf("%d ",arr[r][c]);
}
printf("\n");
}
}
You need to declare the array in main(), so it can be passed to both functions.
When an array is passed as a function parameter, it just passes a pointer. You need to pass the array dimensions, they can't be determined using sizeof.
To get each row of the table on a new line, put printf("\n"); after the loop that prints a row.
#include <stdio.h>
void multiplication(int num, arr[num][num]){
/* initialize array and build*/
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int num, int arr[num][num]){
/* print the arr*/
int i;
for(i=0;i<num;i++){
for(int j=0;j<num;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main(void) {
int size;
printf("How big is the multiplication table? ");
scanf("%d", &size);
int arr[size][size];
multiplication(size, arr);
print(size, arr);
return 0;
}
i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main() {
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
declaration(m,n);\\HERE IS THE PROBLEM
printing(matrix,m,n);
getch();
}
void declaration(int a,int b) {
srand(time(NULL));
int i,j,matrix[a][b];
for(i=0;i<a;i++){
for(j=0;j<b;j++){
matrix[i][j]=1+rand()%7;
}
}
}
void printing(int c[100][100],int a,int b) {
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",c[i][j]);
}
printf("\n");
}
}
Define it like:
typedef struct {
int rows;
int cols;
int *data;
} int_matrix_entity, *int_matrix;
int_matrix int_matrix_create(int rows, int cols, bool rand)
{
int_matrix mt;
int i;
if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
{
return NULL;
}
if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
{
free(mt);
return NULL;
}
if (rand)
{
srand(time(NULL));
for (i = 0; i < cols * rows; i++)
{
mt->data[i] = 1 + rand() % 7;
}
}
else
{
memset(mt->data, 0, sizeof(int) * cols * rows);
}
return mt;
}
void int_matrix_printf(int_matrix mt)
{
int i;
int j;
for (i = 0; i < mt->rows; i++)
{
for (j = 0; j < mt->cols; j++)
{
printf("%5d ", mt[i * cols + j]);
}
printf("\n");
}
}
You have a few points that require a bit more attention;
1 ) read warning and error messages given by your compiler
2 ) again, read warning messages given by your compiler
3 ) use indentation to make your code more readable.
4 ) Always return from main(), that's a good practice
The code below does what you want to achieve; have a look at it and keep on reading...
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// You either have to declare your functions
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b, int m[a][b]);
int main(){ // always return from main()
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
int matrix[m][n];
declaration(m, n, matrix);
printing(m, n, matrix);
return 0;
}
void declaration(int a,int b, int m[a][b]){
srand(time(NULL));
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
m[i][j]=1+rand()%7;
}
}
}
void printing(int a,int b, int m[a][b]){
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",m[i][j]);
}
printf("\n");
}
}
You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below
declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */
remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.
Look into your header files (i.e. .h files) and you will see lots of function declarations.
Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.
Some extras;
Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.
For further reading, you may want to look at
variable scope
global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
declaration vs definition in C
Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.
#include <stdio.h>
#include <stdlib.h>
int** create_matrix(int rows, int cols) {
int **matrix = malloc(rows*(sizeof(int *)));
for(int i = 0; i < rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = 1 + rand()%7;
}
}
return matrix;
}
void printing(int** matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
int rows, cols;
rows = 3, cols = 3;
int** matrix = create_matrix(rows, cols);
printing(matrix, rows, cols);
free(matrix);
return 0;
}
I'm making a randomized n*n matrix, but I do not know the value of n until the program is already running.
I'm capable of creating the n*n matrices in main() like so:
double (*m1)[n];
m1 = malloc(sizeof *m1 * n);
double (*m2)[n];
m2 = malloc(sizeof *m2 * n);
But now I must use these matrices outside of main and need them to be global, but I'm completely clueless how to make them global. I intend to read these two matrices with multiple threads and need them easily accessible. I know I can make a struct to pass multiple parameters, but I would need to define a struct with variable length arrays globally so the problem rearises. Thank you for your time.
Just declare as double pointer and allocate memory in main. For more details refer this
#include <stdio.h>
int **a;
void fun(int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
printf("%d ",a[i][j]);
}
}
int main(void)
{
int n;
scanf("%d",&n);
a = (int **)malloc(n * sizeof(int *));
for(int i=0;i<5;i++)
{
a[i]=(int*)malloc(n*sizeof(int));
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
fun(n);
return 0;
}
Working Code : https://ideone.com/OMQ4qA
The correct, proper solution is to not use globals. Allocate the array in the thread which will persist throughout the execution of the whole program.
If that, for reasons unknown, is not an option, then you will have to come up with some solution that stores the array size globally too. You could create a simple ADT for this, such as a struct with a "mangled" 2D array in the form of a flexible array member. Example:
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
size_t x;
size_t y;
double array [];
} double_array2d_t;
double_array2d_t* array2d = NULL;
int main()
{
/* let x and y be some manner of run-time values: */
int x = 3;
int y = 2;
array2d = malloc(sizeof(*array2d) + sizeof(double[x][y]));
array2d->x = x;
array2d->y = y;
double count = 0.0;
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
array2d->array[i*y + j] = count++;
printf("%f ", array2d->array[i*y + j]);
}
printf("\n");
}
...
free(array2d);
}
And of course if you access this from multiple threads, you have to protect the data with a mutex as always.
I can acheive it by passing (c,a,b) to add_mat function, where result of a,b is stored in c like,
void add_mat(int c[][3], int a[][3], int b[][3], int m, int n)
What should be the return type of add_mat if I want to construct the funtion in this way
?? add_mat(int a[][3], int b[][3], int m, int n)
Below is a sample code
#include<stdio.h>
void read_mat(int a[][3], int m, int n){
//scan data
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("[%d][%d] : ",i,j);
a[i][j]=i+j;
}
}
}
int* add_mat(int a[][3], int b[][3], int m, int n){
int c[3][3];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
return c;
}
int main(){
int a[3][3];
int m=2,n=2; //mxn of a matrix
read_mat(a, m, n);
//add
int b[3][3];
read_mat(b, m, n);
int* c[3][3];
c = add_mat(a,b,m,n);
return 0;
}
Like passing or pointing the calculated value c inside add_mat function to variable in main function.
You cannot do that, since the memory of the c matrix will be gone when the function terminates.
You need to dynamically allocate it with malloc(), in order for the memory not to be free'd, unless you call free(). I have some examples for that in 2D dynamic array (C), if you want to take a look.
With your previous function, you would create the matrix c outside the function (in main()), that's why dynamic memory allocation was not required.
PS: You should compile with warnings enabled:
prog.c: In function 'add_mat':
prog.c:19:12: warning: returning 'int (*)[3]' from a function with incompatible return type 'int *' [-Wincompatible-pointer-types]
return c;
^
prog.c:19:12: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c: In function 'main':
prog.c:32:7: error: assignment to expression with array type
c = add_mat(a,b,m,n);
^
prog.c:31:10: warning: variable 'c' set but not used [-Wunused-but-set-variable]
int* c[3][3];
^
Here is a working example, which is just for demonstrative purposes:
#include <stdio.h>
#include <stdlib.h>
void read_mat(int a[][2], int n, int m){
//scan data
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
a[i][j] = i + j ;
}
}
}
int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i, **table;
table = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
table[i] = malloc( M*sizeof(int) );
return table;
}
void print(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}
void free2Darray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
int** add_mat(int a[][2], int b[][2], int m, int n){
int** c = get(n, m);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
return c;
}
int main(){
int n = 2, m = 2; //nxm of a matrix
int a[n][m];
read_mat(a, n, m);
//add
int b[n][m];
read_mat(b, n, m);
int** c;
c = add_mat(a, b, n, m);
print(c, n, m);
free2Darray(c ,n);
return 0;
}
Output:
array[0][0] = 0
array[0][1] = 2
array[1][0] = 2
array[1][1] = 4
PPS: If you really want to use static arrays, then I recommend using int c[3][3]; add_mat(c, a, b, m, n);
Oh yes, the pointers ...
In C, locally declared arrays usually are allocated on the stack (not on the heap) which means they are not valid outside the respective scope.
In other words, in your function add_mat(), c technically can be returned, but the address it points to will only contain meaningful matrix data as long as the function is executed.
After having returned from the function, the return value of the function is a pointer which still contains (points to) the address where the matrix was stored during execution of the function, but the contents of that location, i.e. the matrix data, can contain arbitrary garbage now.
So what you are doing is technically possible (i.e. throws no compile errors), but is definitely not what you want.
Secondly, your line int* c[3][3]; probably is not what you intend it to be. You are declaring c to be a two-dimensional array (matrix) of pointer to int here. This is not correct since you want to process int values, but not pointers to int values (which are addresses).
To solve both problems, just write int c[3][3]; instead of int* c[3][3]; and change your add_mat() function as follows:
void add_mat(int a[][3], int b[][3], int c[][3], int m, int n){
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
}
Then call that function like that:
add_mat(a,b,c,m,n);
In your code add_mat returns the pointer to the variable which lives while the function executes only. You can accomplish your goal with dynamically allocated matrix.
In C it is common to pass a dynamically allocated matrix via pointer to pointers to the rows of matrix as a first parameter:
void foo(int** matrix, size_t m, size_t n);
Here matrix is a pointer to the array of pointers. Every pointer in this array points to the row of matrix. So, matrix[0] points to the array containing the first row of matrix.
And it is common to use size_t type for any sizes of arrays.
If you need create matrix dynamically, be careful with memory. Every dynamically allocated block should be released. Otherwise you will get memory leaks, that may cause the program crash. Therefore, when you allocate rows of matrix, you should check if allocation of current row was successfull. If not, you should release all previously allocated rows.
There is a working code for your question:
#include <stdio.h>
#include <stddef.h>
#include <malloc.h>
void free_mat(int** a, size_t m) {
if (!a) {
return;
}
for (size_t i = 0; i < m; ++i) {
free(a[i]);
}
free(a);
}
int** create_mat(size_t m, size_t n) {
int** rows = calloc(m, sizeof(int*));
if (!rows) {
return NULL;
}
for (size_t i = 0; i < m; i++) {
rows[i] = malloc(n * sizeof(int));
if (!rows[i]) {
free_mat(rows, m);
return NULL;
}
}
return rows;
}
void read_mat(int** a, size_t m, size_t n) {
for (size_t i = 0; i < m; i++) {
for (size_t j = 0; j < n; j++) {
printf("[%d][%d]: ", i, j);
scanf("%d", a[i] + j);
}
}
}
void print_mat(const int* const* a, size_t m, size_t n) {
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
printf("[%d][%d]: %d\n", i, j, a[i][j]);
}
}
}
int** add_mat(const int* const* a, const int* const* b, size_t m, size_t n) {
int** c = create_mat(m, n);
if (c) {
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
return c;
}
int main() {
size_t m = 3;
size_t n = 3;
int** a = create_mat(m, n);
int** b = create_mat(m, n);
if (!a || !b) {
printf("error when allocating matrix\n");
}
else {
read_mat(a, m, n);
read_mat(b, m, n);
int** c = add_mat(a, b, m, n);
if (!c) {
printf("error when allocating matrix\n");
}
else {
print_mat(c, m, n);
free_mat(c, m);
}
}
free_mat(a, m);
free_mat(b, m);
return 0;
}
But you would be more flexible, if add_mat didn't create a new matrix.
It is common to pass a pointer to the result matrix as a function parameter:
void add_mat(int** c, const int* const* a, const int* const* b, size_t m, size_t n);
I have created a contiguous array with random values (0-9), but I don't know how to pass it to a function that would print it.
Right now I'm simply iterating through the array inside the main function, whenever I need to print it. But I want to have a function for that, it would make my code cleaner.
A minimal code example is below:
#include <stdio.h>
#include <stdlib.h>
int **alloc_2d_int(int rows, int cols);
int main(int argc, char** argv){
int **matrix;
int i,j,size=3;
matrix = alloc_2d_int(size,size);
// generate the array
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
matrix[i][j] = rand() % 10;
}
}
// how I print the array right now
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
printf("[%d][%d]=%d\n", i,j,matrix[i][j]);
}
}
// this is how I want the printing to happen
print_arr(matrix,3);
}
int **alloc_2d_int(int rows, int cols) {
int *data = (int *)malloc(rows*cols*sizeof(int));
int **array= (int **)malloc(rows*sizeof(int*));
int i;
for (i=0; i<rows; i++)
array[i] = &(data[cols*i]);
return array;
}
void print_arr(int **array,int size)
{
int i,j;
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
printf("matrix[%d][%d]=%d\n", i,j,array[i][j]);
}
}
}
Error message:
conflicting types for ‘print_arr’ [enabled by default]
You forgot to add the declaration(prototype) of your function. In C language, by-default, the return type of a function is of int type. But for your print_arr() function, the return type is void. So, you get the conflict type error.
So, Declare the print_arr() function above the main() function. Like:
void print_arr(int **array,int size);
You haven't declared print_arr function before the main so it is not able to find it. Rest works smoothly.