I'm trying to make a simple console application in C which will calculate the determinant of a Matrix using the Gauss partial pivoting elimination method. The 2 problems that I have are :
- someone told me that there are certain matrix-es that don't work with this method ( mathematically speaking ), after reading articles on google, i could not find what is that special case
- after a lot of tests I found out that my program is not working for some matrix-es, after 2 days of "wasting" time editing and undoing, i could not find the problem.
Any type of improvements are more than welcomed. I'm just starting with C.
#include<stdio.h>
#include<cstdlib>
#include<math.h>
#include<conio.h>
#include<windows.h>
// calculate biggest element on column
int indice_max(int dim, int col, float coloana[20][20]) {
float max = 0;
int indice;
for(int i = 1; i <= dim; i++)
if(fabs(max) < fabs(coloana[i][col])) {
max = coloana[i][col];
indice = i;
}
return indice;
}
// permute 2 lines
void permutare_linie(int linie1, int linie2, int dim, float matrice[20][20]) {
float aux;
for(int i = 1; i <= dim; i++) {
aux = matrice[linie1][i];
matrice[linie1][i] = matrice[linie2][i];
matrice[linie2][i] = aux;
}
}
// print matrix
void afisare_matrice(int dimensiune, float matrice[20][20], int lpiv) {
for(int i = 1; i<= dimensiune; i++) {
for(int j = 1; j <= dimensiune; j++) {
if(i == lpiv)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
printf("%4.2f ", matrice[i][j]);
}
printf("\n");
}
}
void main(void) {
float matrice[20][20];
int dimensiune ;
float rezultat = 1;
float pivot;
int lpiv;
int cpiv;
int optiune;
while(1) {
// MENU
printf("ALEGET OPTIUNEA:\n");
printf("1) Calculate matrix determinant\n");
printf("2) Exit\n");
scanf("%d", &optiune);
if(optiune == 1) {
// Read determinant dimension
printf("Matrix dimension:");
scanf("%d", &dimensiune);
// Read determinant
for(int i = 1; i <= dimensiune; i++)
for(int j = 1; j <= dimensiune; j++) {
printf("M[%d][%d]=", i, j);
scanf("%f", &matrice[i][j]);
}
// pivot initial coords
lpiv = 1;
cpiv = 1;
printf("\n----- Entered Matrix -----\n\n");
afisare_matrice(dimensiune, matrice, 0);
printf("\n");
for(int pas = 1; pas <= dimensiune - 1; pas++) {
if(fabs(matrice[lpiv][cpiv]) > fabs(matrice[indice_max(dimensiune, cpiv, matrice)][cpiv])) {
permutare_linie(lpiv, indice_max(dimensiune, cpiv, matrice), dimensiune, matrice);
rezultat = -(rezultat);
}
pivot = matrice[lpiv][cpiv];
for(int inm = 1; inm <= dimensiune; inm++) {
matrice[lpiv][inm] = matrice[lpiv][inm] / pivot;
}
rezultat *= fabs(pivot);
// transform matrix to a superior triangular
for(int l = lpiv+1; l <= dimensiune; l++)
for(int c=cpiv+1; c <= dimensiune; c++) {
matrice[l][c] -= matrice[l][cpiv] * matrice[lpiv][c] / matrice[lpiv][cpiv];
}
for(int i = lpiv + 1; i <= dimensiune; i++)
matrice[i][cpiv] = 0;
// afisam rezultat / pas
printf("----- Step %d -----\n\n", pas);
afisare_matrice(dimensiune, matrice, lpiv);
printf("\nResult after step %d : %4.2f\n\n", pas, rezultat);
lpiv++;
cpiv++;
}
// final result
rezultat = rezultat * matrice[dimensiune][dimensiune];
printf("----- REZULTAT FINAL -----\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("Rezultat = %4.2f\nRezultat rotunjit:%4.0f\n\n", rezultat, floorf(rezultat * 100 + 0.5) / 100);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
}
else {
exit(0);
}
}
}
Your code does some division:
matrice[lpiv][inm] = matrice[lpiv][inm] / pivot;
If it happens to divide by zero, an error will occur. I guess this will happen for the zero matrix.
It seems that your code is actually trying to invert the matrix, not just calculate the determinant.
Related
I have tried many different approaches and it keeps giving me errors...I am definitely not the best coder, please help! I have tried to create a histogram in many different ways, I know the logic behind making a histogram but I do not know how to implement that into C. I need to create a histogram for the x array.
The problem:
Write a computer routine to generate 2,000 values from the given cdf F(x)=x^4/16 on 0<=x<=2. Make a histogram of the 2,000 values and compare it to the theoretical cdf.
int main()
{
int i, b, d, e, j, bins=9, n=2000, y[2000], hist[9];
double seed = 12;
double temp=0, r[2000], temp2=0, x[2000];
int a = 1093, c = 18257, m = 86436;
printf("\nThis program will calculate random variates based on the given CDF\n :x^4/16 on 0<=x<=2\n ");
y[0]=seed;
for (i=1; i<n; i=i+1){
y[i] = (a*y[i-1] + c) % m;
temp = y[i];
r[i] = temp / m;
temp2 = r[i];
x[i] = pow(16*temp2,0.25);
printf("%d %.4lf %lf\n", y[i], r[i], x[i]);
}
//all of my attempts below
/*
int *buildHist(int bins, double min, double max, int n, double *data){
double *hist=malloc(bins*sizeof(int));
if (hist == NULL) return hist;
for (int i=0; i<n; ++i){
int bin=int( (data[i]-min)/((max-min)/(bins)) );
if ( (bin>=0) && (bin<n) ) hist[bin]++;
}
return hist;
}
int max = x[0];
for (d = 1; d < n; d=d+1){
if (x[d] > max)
max = x[d];
}
printf("The max is : %lf\n", max);
int min = x[0];
for (b =1; b<n; b=b+1){
if (x[b] < min)
min = x[b];
}
printf("The min is : %lf\n", min);
//Dividing data into bins
for (b = 0; b < n; b+1){
for (j = 1; j <= bins; j+1){
float bin_max = (float)j / (float)bins;
if (x[b] <= bin_max){
hist[j]+1;
break;
}
}
}
// Plotting histogram
printf("\n\nHistogram of Float data\n");
for (d = 1; d <= bins; d+1){
count = hist[d];
printf("0.%d |", d - 1);
for (e = 0; e < count; e+1)
{
printf("%c", (char)254u);
}
printf("\n");
}
*/
return 0;
}
I guess that your problem is that malloc() returns non-initialized memory. This means that your hist already contains garbage values which you then increment with hist[bin]++.
Use calloc() to allocate hist or use the memset() C library function to clear hist before use.
there's a mistake in my code but i can't find where. it must calculate the averages after reading inputs.
it reads all the code and gets all the inputs but when it comes to calculating the average it does nothing. help please
#include <stdlib.h>
#include <stdio.h>
int main() {
int students, modules, m, n, first_student, last_student, l = 0;
float student[100][20], high = 0, low = 20, average[students], average_mark[students];
printf("Please enter the number of students:\n");
scanf("%d", &students);
printf("Please enter the number of modules:\n");
scanf("%d", &modules);
for (m = 0; m < students; ++m) {
for (n = 0; n < modules; n++) {
printf("Please enter the mark of module %d for student number %d,\n", n + 1, m + 1);
scanf("%f", &student[m][n]);
}
}
for (m = 0; m < students; ++m) {
average[m] = 0;
}
for (n = 0; n < modules; n++) {
average[m] += student[m][n];
average_mark[m] = average[m] / modules;
}
printf("student average\n");
for (m = 0; m < students; ++m) {
printf("%d %f\n", m + 1, average_mark[m]);
}
for (m = 0; m < students; ++m) {
if (average_mark[m] < low) {
average_mark[m] = low;
}
else
if (average_mark[m] == low) {
last_student = m + 1;
}
if (average_mark[m] > high) {
average_mark[m] = high;
}
else
if (average_mark[m] == high) {
first_student = m + 1;
}
}
printf("The student who had the highest mark is %d : %f\n", first_student, high);
printf("The student who had the lowest mark is %d : %f\n", last_student, low);
for (m = 0; m < students; ++m) {
if (average_mark[m] == 10 || average_mark[m] > 10) {
l++;
}
}
printf("the number of students having a mark that equals or exceeds the average is %d\n", l);
return 0;
}
There are some other problems in the code but as a start, you may take a peek below.
edit: The next (2nd) problem noted & corrected. I'm leaving you to find & correct the last (3rd) problem.
// either these values need to be predefined
// or you'll need to dynamically allocate
// arrays using malloc
int MAX_STUDENTS = 100;
int MAX_MODULES = 20;
int main() {
int students, modules, m, n, first_student, last_student, l = 0;
float student[MAX_STUDENTS][MAX_MODULES], high = 0, low = 20, average[MAX_STUDENTS], average_mark[MAX_MODULES];
...
...
...
// the 2nd & 3rd for loops needs to be nested as below
for (m = 0; m < students; ++m) { // this was 2nd
average[m] = 0;
for (n = 0; n < modules; n++) { // this was 3rd
average[m] += student[m][n];
}
// you need to move this line out of 3rd loop
average_mark[m] = average[m] / modules;
}
...
...
...
// this is how you find the lowest & highest ranking students
for (m = 0; m < students; ++m) {
if (average_mark[m] < low) {
low = average_mark[m];
last_student = m;
}
if (average_mark[m] > high) {
high = average_mark[m];
first_student = m;
}
}
printf("The student who had the highest mark is %d : %f\n", first_student + 1, high);
printf("The student who had the lowest mark is %d : %f\n", last_student + 1, low);
...
...
...
}
we are trying to achieve round robin algorithm using linked list.
But My logic has some errors.
When I try to run it for 3 process then the first process values are wrong and sometimes right for the other below processes.
Please Help me.
I tried Searching for logics
Code Link: https://pastebin.com/FkbtUEaQ
#include<stdio.h>
struct process
{
char na[20];
int at, bt, ft, tat, rem;
//float ntat;
} Q[5], temp;
void roundRobin()
{
int rr[20], q, x, k;
int f, r, n, i, j, tt = 0, qt, t, flag, wt = 0;
float awt = 0, antat = 0, atat = 0;
printf("Enter the no. of jobs:");
scanf("%d", &n);
for (r = 0; r < n; r++)// aceppting arrival; and burst time
{
printf("Enter process name,arrival time and burst time:\n");
scanf("%s%d%d", Q[r].na, &Q[r].at, &Q[r].bt);
}
printf("Enter quantum:\n");
scanf("%d", &qt);
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (Q[i].at < Q[j].at) {
temp = Q[i];
Q[i] = Q[j];
Q[j] = temp;
}
}
}
for (i = 0; i < n; i++)
{
Q[i].rem = Q[i].bt;
Q[i].ft = 0;
}
tt = 0;
q = 0;
rr[q] = 0;
do
{
for (j = 0; j < n; j++)
if (tt >= Q[j].at)
{
x = 0;
for (k = 0; k <= q; k++)
if (rr[k] == j)
x++;
if (x == 0)
{
q++;
rr[q] = j;
}
}
if (q == 0)
i = 0;
if (Q[i].rem == 0)
i++;
if (i > q)
i = (i - 1) % q;
if (i <= q)
{
if (Q[i].rem > 0)
{
if (Q[i].rem < qt)
{
tt += Q[i].rem;
Q[i].rem = 0;
} else
{
tt += qt;
Q[i].rem -= qt;
}
Q[i].ft = tt;
}
i++;
}
flag = 0;
for (j = 0; j < n; j++)
if (Q[j].rem > 0)
flag++;
} while (flag != 0);
printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish time tat wt ntat");
for (f = 0; f < n; f++) {
wt = Q[f].ft - Q[f].bt - Q[f].at;
Q[f].tat = Q[f].ft - Q[f].at;
Q[f].ntat = (float) Q[f].tat / Q[f].bt;
antat += Q[f].ntat;
atat += Q[f].tat;
awt += wt;
printf("\n\t%s\t%d\t%d\t%d\t%d\t%d %f", Q[f].na, Q[f].at, Q[f].bt,
Q[f].ft, Q[f].tat, wt, Q[f].ntat);
}
antat /= n;
atat /= n;
awt /= n;
printf("\nAverage tat is %f", atat);
printf("\nAverage normalised tat is %f", antat);
printf("\n average waiting time is %f", awt);
}
void main()
{
roundRobin();
getch();
clrscr();
}
The First Process Gives Wrong Values
processes | ArrivalTime | BurstTime | FinishTime | Tat | WaitTime
a 0 10 60 60 50
b 0 20 30 30 10
c 0 30 50 50 20
In the do while loop you use i to index the rr array with valid indexes 0 to q as well as the Q array with valid indexes 0 to n − 1, of which only the former is correct. So, you have to change every occurrence of Q[i] in this loop to Q[rr[i]].
After that, still the order of the statements
if (Q[rr[i]].rem == 0)
i++;
if (i > q)
i = (i - 1) % q;
is wrong - the test for Q[rr[i]].rem == 0 is to be done each time a new i is chosen, e. g.:
while (Q[rr[i %= q+1]].rem == 0) i++;
I have a binary matrix (zeros and ones) D[][] of dimension nxn where n is large (approximately around 1500 - 2000). I want to find the inverse of this matrix in C.
Since I'm new to C, I started with a 3 x 3 matrix and working around to generalize it to N x N. This works for int values, however since I'm working with binary 1's and 0's. In this implementation, I need unsigned int values.
I could find many solutions for int values but I didn't come across any solution for unsigned int. I'd like to find the inverse of a N x N binary matrix without using any external libraries like blas/lapack. It'd be great if anyone could provide a lead on M x N matrix.
Please note that I need inverse of a matrix, not the pseudo-inverse.
/* To find the inverse of a matrix using LU decomposition */
/* standard Headers */
#include<math.h>
#include<stdio.h>
int main() {
/* Variable declarations */
int i,j;
unsigned int n,m;
unsigned int rows,cols;
unsigned int D[3][3], d[3], C[3][3];
unsigned int x, s[3][3];
unsigned int y[3];
void LU();
n = 2;
rows=3;cols=3;
/* the matrix to be inverted */
D[0][0] = 1;
D[0][1] = 1;
D[0][2] = 0;
D[1][0] = 0;
D[1][1] = 1;
D[1][2] = 0;
D[2][0] = 1;
D[2][1] = 1;
D[2][2] = 1;
/* Store the matrix value for camparison later.
this is just to check the results, we don't need this
array for the program to work */
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++) {
C[m][j] = D[m][j];
}
}
/* Call a sub-function to calculate the LU decomposed matrix. Note that
we pass the two dimensional array [D] to the function and get it back */
LU(D, n);
printf(" \n");
printf("The matrix LU decomposed \n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", D[m][j]);
}
printf("\n");
}
/* TO FIND THE INVERSE */
/* to find the inverse we solve [D][y]=[d] with only one element in
the [d] array put equal to one at a time */
for (m = 0; m <= rows-1; m++) {
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[m] = 1;
for (i = 0; i <= n; i++) {
x = 0;
for (j = 0; j <= i - 1; j++){
x = x + D[i][j] * y[j];
}
y[i] = (d[i] - x);
}
for (i = n; i >= 0; i--) {
x = 0;
for (j = i + 1; j <= n; j++) {
x = x + D[i][j] * s[j][m];
}
s[i][m] = (y[i] - x) / D[i][i];
}
}
/* Print the inverse matrix */
printf("The Inverse Matrix\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", s[m][j]);
}
printf("\n");
}
/* check that the product of the matrix with its iverse results
is indeed a unit matrix */
printf("The product\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
x = 0;
for (i = 0; i <= 2; i++) {
x = x + C[m][i] * s[i][j];
}
//printf(" %d %d %f \n", m, j, x);
printf("%d \t",x);
}
printf("\n");
}
return 0;
}
/* The function that calcualtes the LU deomposed matrix.
Note that it receives the matrix as a two dimensional array
of pointers. Any change made to [D] here will also change its
value in the main function. So there is no need of an explicit
"return" statement and the function is of type "void". */
void LU(int (*D)[3][3], int n) {
int i, j, k;
int x;
printf("The matrix \n");
for (j = 0; j <= 2; j++) {
printf(" %d %d %d \n", (*D)[j][0], (*D)[j][1], (*D)[j][2]);
}
for (k = 0; k <= n - 1; k++) {
for (j = k + 1; j <= n; j++) {
x = (*D)[j][k] / (*D)[k][k];
for (i = k; i <= n; i++) {
(*D)[j][i] = (*D)[j][i] - x * (*D)[k][i];
}
(*D)[j][k] = x;
}
}
}
This is just a sample example that I tried and I have -1 values in the inverse matrix which is my main concern. I have 1000 x 1000 matrix of binary values and the inverse should also be in binary.
The matrix:
1 1 0
0 1 0
1 1 1
The matrix LU decomposed:
1 1 0
0 1 0
1 0 1
The Inverse Matrix:
1 -1 0
0 1 0
-1 0 1
The product:
1 0 0
0 1 0
0 0 1
I'm having issues getting a function to work which should find the determinant of an upper triangular matrix. My code seems to return clearly incorrect values, usually zero and I'm pretty certain that this is caused by me defining the function incorrectly some how. I suspect it is a basic error on my part but after staring at it for sometime I havent managed to figure it out. Here is the function and printing code:
int Determinant(int mat[20][20],int N)
{
int X=0,Det=0;
if (N==2){
Det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
return(Det);
}
else {
for(X = 0; X < N; X++){
Det *= mat[X][X];
}
}
return (Det);
}
and the print function :
determinant=Determinant(matrix,n);
printf("Determinant = %d",determinant);
I'll include the full code that I've written so far to provide more detail. It's basic application at the moment is to define and n by n matrix (2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int determinant(int mat[20][20],int N);
int Determinant(int mat[20][20],int N)
{
int X=0,Det=0;
if (N==2){
Det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
return(Det);
}
else {
for(X = 0; X < N; X++){
Det *= mat[X][X];
}
}
return (Det);
}
int main()
{
int n=0,i=1;
printf("Please enter a number (n) between 2 and 4 to determine the dimensions of an (nxn) matrix \n");
scanf("%d",&n);
while(n<2||n>4){
printf("The value %d does not lie within the required range of 2-4, please re-enter \n",n);
scanf("%d",&n);
i++;
if (i>=3){
printf("\nYou have entered invalid values 3 times. The programme has been terminated");
exit(0);
}
}
printf("\n(%dx%d) matrix selected\n",n,n);
int matrix[n][n];
int f,g=0;
printf("Please enter matrix elements\n");
for(f=0;f<n;f++){
for(g=0;g<n;g++){
printf("Element[%d][%d] = ",f,g);
scanf("%d",&matrix[f][g]);
}
}
int k,j;
printf("\nThe matrix is\n");
for(k=0;k<n;k++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",matrix[k][j]);
}
}
int temp=0,c=0,determinant=0;
float factor=0;
k=0;
/* Transform matrix into upper triangular */
for(i = 0; i < n - 1; i++)
{
/* Elementary Row Operation I */
if(matrix[i][i] == 0)
{
for(k = i; k < n; k++)
{
if(matrix[k][i] != 0)
{
for(j = 0; j < n; j++)
{
temp = matrix[i][j];
matrix[i][j] = matrix[k][j];
matrix[k][j] = temp;
}
k = n;
}
}
c++;
}
/* Elementary Row Operation III */
if(matrix[i][i] != 0)
{
for(k = i + 1; k < n; k++)
{
factor = -1.0 * matrix[k][i] / matrix[i][i];
for(j = i; j < n; j++)
{
matrix[k][j] = matrix[k][j] + (factor * matrix[i][j]);
}
}
}
}
printf("\nThe Upper triangular is\n");
for(k=0;k<n;k++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",matrix[k][j]);
}
}
determinant=Determinant(matrix,n);
printf("Determinant = %d",determinant);
/*
*/
return 0;
}
The problem is basically the way you pass the matrix as a parameter. To see what I mean, change the definition of the function to read:
int Determinant(int mat[5][5],int N);
and instruct the function body to print the full 5x5 matrix passed:
int Determinant(int mat[5][5],int N)
{
printf("\n");
int a,b;
for(a = 0; a < 5; a++)
{
for(b = 0; b < 5; b++)
{
printf("%d\t", mat[a][b]);
}
printf("\n");
}
int X=0,Det=0;
Det = 1; // Add this too!
for(X = 0; X < N; X++) {
Det *= mat[X][X];
}
return (Det);
}
Now enter n=3 for the matrix dimension and pass the already upper triangular matrix
1 2 3
0 4 5
0 0 6
Observe the printout of the matrix passed in the Determinant() function, it will be something like this:
1 2 3 0 4
5 0 0 6 0
4196432 0 -163754450 0 -1253168992
32764 3 0 0 0
3 0 0 0 3
This means that your array has been "reshaped", and your actual data are stored in consecutive places in memory, unlike the original array.
TLDR: Although I am not very proficient with C, I think that you should define your 2d array as a dynamic one (for example using a double pointer).
PS: Don't forget to initialize Det variable to 1 instead of 0 in the function body, otherwise the product will always equal 0.