This is the full code of my program,which I need to write for a homework assignment. It worked without the "free()" function, but I wanted to optimize it. Adding the "free()" function led to crashes while it calculates the result, and I couldn't find the reason why.The problematic fraction is on lines 91 to 94.
Any help would be appreciated. -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <math.h>
-
#define MAXDIM 200
-
-
/*This program lets the user do some mathematical operations with matrixes.
-
It is in early stage, so only one operation is available - calculating a determinant*/
-
-
/*This program is made just for a project I have been assigned to make.
-
I'm in early stage of studying Computer Science, so some of my code may seem unusual.
-
I'm open for advices of how to improve my code writting.*/
-
-
/*Each function is described before it's implementation*/
-
void read(int**,int,int);
-
void print(int**,int,int);
-
int** allocateMatrix(int,int);
-
void applyOperation(int**,int,int,void (*)(int**,int,int));
-
void enterDim(int*,int*);
-
int adjugateMatrix(int**,int,int,int);
-
int** subMatrix(int**,int,int,int,int);
-
int det(int**,int,int);
-
-
int main()
-
{
-
int **matrix,n,m;
-
enterDim(&n,&m);
-
matrix = allocateMatrix(n,m);
-
read(matrix,n,m);
-
printf("The determinant is: %d ",det(matrix,n,m));
-
return 0;
-
}
-
-
/*This function lets the user to enter the matrix*/
-
void read(int **matrix,int dim1,int dim2){
-
printf("Enter the matrix with %d rows and %d columns:\n",dim1,dim2);
-
printf("(Enter %d elements separated by space on %d lines)\n",dim2,dim1);
-
int **i,*j;
-
for(i = matrix; i < matrix + dim1; i++)
-
for(j = *i; j < *i + dim2; j++){
-
scanf("%d",j);
-
}
-
}
-
-
/*This function prints a matrix on the screen*/
-
void print(int **matrix,int dim1,int dim2){
-
int **i,*j;
-
for(i = matrix; i < matrix + dim1; i++){
-
for(j = *i; j < *i + dim2; j++){
-
printf("%-5d",*j);
-
}
-
putchar('\n');
-
}
-
}
-
-
/*This function allocates space for a matrix with dim1 rows and dim2 columns and returns a pointer-to-pointer-to-int*/
-
int** allocateMatrix(int dim1,int dim2){
-
int i,**matrix = (int **)malloc(dim1*sizeof(int *));
-
for(i = 0; i < dim2;i++){
-
*(matrix + i) = (int *)malloc(dim2*sizeof(int));
-
if((*matrix + i) == NULL){
-
puts("Unable to allocate memory!!!");
-
exit(EXIT_FAILURE);
-
}
-
}
-
return matrix;
-
}
-
-
/*This function will be used later for easier operation selection*/
-
void applyOperation(int** matrix,int dim1,int dim2,void (*oper)(int**,int,int)){
-
(*oper)(matrix,dim1,dim2);
-
}
-
-
/*This function lets the user define the dimensions of the matrix*/
-
void enterDim(int *n,int *m){
-
puts("Enter the two dimensions:");
-
do{
-
scanf("%d",n);
-
if((*n<1)||(*n>MAXDIM))puts("Invalid dimension.Try again!!!");
-
}while((*n<1)||(*n>MAXDIM));
-
do{
-
scanf("%d",m);
-
if((*m<1)||(*m>MAXDIM))puts("Invalid dimension.Try again!!!");
-
}while((*m<1)||(*m>MAXDIM));
-
}
-
-
/*Calculates the adjoint matrix of the element on the n-th row and m-th column*/
-
int adjugateMatrix(int **matrix,int dim1,int n,int m){
-
int i,d,**newMatrix = subMatrix(matrix,dim1,dim1,n,m);
-
d = ((int)pow(-1.0,(double)(n+m)))*det(newMatrix,dim1-1,dim1-1);
-
-
/*for(i = 0; i < dim1-1; i++){
-
free(*(newMatrix + i));
-
}
-
free(newMatrix);*/
-
/*The above comment is the problematic fraction.
-
I've tested the program for up to six rowed matrix, and it worked without the above section.
-
But I want to make the program work for big "n"s.
-
So I tried to free the allocated memory, when unneeded.
-
But that led to a crash for bigger than 3x3 matrix.*/
-
-
return d;
-
}
-
-
/*This function makes a new matrix with the n-th row and m-th column removed*/
-
int** subMatrix(int **matrix,int dim1,int dim2,int n,int m){
-
int i,j,k,l,**newMatrix = allocateMatrix(dim1 - 1,dim2 -1);
-
for(i = 0,k =0; i < dim1; i++){
-
if(i == n)continue;
-
for(j = 0, l = 0; j < dim2; j++){
-
*(*(newMatrix + k) + l) = *(*(matrix + i) + j);
-
if(j == m)continue;
-
l++;
-
}
-
k++;
-
}
-
return newMatrix;
-
}
-
-
/*This function calculates the determinant using recursion*/
-
int det(int **matrix,int dim1,int dim2){
-
int d = 0,i;
-
if(dim1 != dim2){
-
puts("Determinant is defined only for square matrix!!!");
-
exit(EXIT_FAILURE);
-
}
-
else{
-
if(dim1 <= 2){
-
return (**matrix)*(*(*(matrix + 1) + 1)) - (**(matrix + 1))*(*(*matrix + 1));
-
}
-
else{
-
for(i = 0; i < dim1; i++){
-
d += (*(*matrix + i))*adjugateMatrix(matrix,dim1,0,i);
-
}
-
}
-
}
-
return d;
-
}
-
1 1728
A 3x3 matrix with:
123
456
789
is laid out in memory as:
123456789
With this in mind, if the elements of your array are int* and these int* point to the row elements, you will not be able have rows of different sizes nor can you have new arrays with different dimesions since there is no data for either the number of rows nor the number of elements in each row.
You might research how to use a chain link table. I know that Robert Sedgewick has examples in his books.
Other than that, you will need to be sure that your vision of the array is the same as malloc's.
you might also read http://bytes.com/topic/c/insights/77...rrays-revealed Sign in to post your reply or Sign up for a free account.
Similar topics
by: TCMA |
last post by:
I am looking for some tools to help me understand source code of a
program written in C++ by someone else.
Are there any non-commercial, open source C or C++ tools to reverse
engineer C or C++...
|
by: Fernando Barsoba |
last post by:
Dear all,
I have been posting about a problem trying to encrypt certain data using
HMAC-SHA1 functions. I posted that my problem was solved, but
unfortunately, I was being overly optimistic. I...
|
by: asif929 |
last post by:
I need immediate help in writing a function program. I have to write a
program in functions and use array to store them. I am not familiar
with functions and i tried to create it but i fails to...
|
by: sparkid |
last post by:
I need immediate help in writing a function program. I have to write a
program in functions and use array to store them. I am not familiar
with functions and i tried to create it but i fails to...
|
by: Bikini Browser |
last post by:
Folks:
I am not a programmer so I need some help here... I need a small
program
written that tells me how many days old my daughter is since she was
born.
She was born September 26th, 2000. ...
|
by: Manikandan |
last post by:
Hi,
I have a program written in .Net Framework 1.1 using Visual studio
enterprise edition 2003.
I tried compiling the same program in visual c# express edition 2005.
I'm getting following...
|
by: Alenik1989 |
last post by:
I wrote this code, but I'm not able to test it because im getting 3 errors. So I'm not sure that this code will work properly or not!!!
the code in
#include <stdio.h>
#include <stdlib.h>
int...
|
by: hirsh.dan |
last post by:
i have a functions that writes information to a file.
inside that function i have a line in which i call another function.
if this line is executed, nothing is written to the file, but if i
remark...
|
by: lacole |
last post by:
I have an .exe program written in C# that will not run on a specific PC that has Windows XP on it. That same .exe program will run on other Windows XP PCs though. So it's something specific to that...
|
by: kirubagari |
last post by:
Dear expert,
i had written the code in my form
Public Function HoldType(ByRef sType As String) As String
If bOkClicked Then
sType = "Rehold "
End If
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |