Calculate Reverse Matrix? - c

Why should i get crash with this , where did i wrong !? :(
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
const volatile max=15;
int read(float[][max],float[][max]);
void compute1(float[][max],float[][max],float[][max],int,int,int,float);
float compute2(float[][max],int);
void display(float[][max],float[][max],int,float);
int main(){
float num[max][max],g[max][max],v[max][max],a[max][max];
int dn,u;
float det;
int register i,j;
dn=read(num,a);
det=compute2(num,dn);
for(i=0;i<dn;i++)
for(j=0;j<dn;j++){
compute1(a,g,v,dn,i,j,det);
}
display(a,v,dn,det);
getch();
return 0;
}
//****************************************************************************
int read(float num[][max],float a[][max]){
int dn;
clrscr();
int register i,j;
printf("\nenter degree of matrix:");
scanf("%d",&dn);
clrscr();
for(i=0;i<dn;i++){
printf("\n\n\nenter arguments of row[%d]:\n\n",i);
for(j=0;j<dn;j++){
scanf("%f",*(num+i)+j);
*(*(a+i)+j)=*(*(num+i)+j);
}
}
return dn;
}
//****************************************************************************
void display(float c[][max],float inv[][max],int dn,float det){
int register i,j;
clrscr();
printf("\n\n\n\n\n\t\t\t --ORIGINAL MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.3f",c[i][j]);
}
printf("\n\n\n\t\t\t --INVERSE MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.6f ",inv[i][j]);
}
printf("\n\n\n\t\tعؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤ؟");
printf("\n\t\t³ determinan of matrix= %19.7f ³ ",det);
printf("\n\t\tہؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤظ");
}
//****************************************************************************
void compute1(float g[][max],float v[][max],float inv1[][max],int dn,int r,int c,float e){
int col=0,row=0,add=r+c,y;
float y1=1;
if(add%2)
y1=-1;
int register i,j;
for(i=0;i<dn-1;i++){
if(i==r)
row=1;
col=0;
for(j=0;j<dn-1;j++){
if(j==c)
col=1;
v[i][j]=g[i+row][j+col];
}
}
inv1[c][r]=y1*compute2(v,dn-1)/e;
}
//****************************************************************************
float compute2(float c[][max],int s){
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
int register i,j;
while(y>0){
for(i=1;i<k;i++){
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
}
y--;
k--;
for(i=0;i<k;i++)
for(j=s-1;j>=0;j--)
c[y-i][j]=c[y-i][j]-h[i]*c[y+1][j];
}
for(i=0;i<s;i++)
h1*=c[i][i];
return h1;
}

There's quite a number of problems with your code. In compute2 you're allocating memory for.. a row?
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
You're allocating space for just one float though. That could be a possible source of crashes.
You never actually deallocate the memory either, too.

You need to use debugging and logging tools rather than eyeballing the code. We don't even know what your crash is.
However here is one suspicious place
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
In both branches of the if...else you are dividing by c[y][y]. You know when it enters the else that is zero. Unless the for loop changes it you will have divide by zero. So I suggest you test it.

Related

How can I save a vector entered by a user using a subprogram?

Just started learning C, and it would be great if you could help me with the following:
I just wrote a program that saves a 4-component vector entered by the user (using function called save_vector), and prints it (using function called print_vector).
However, I am not sure how to make the function save_vector work. The other function print_vector seems to be working just fine.
Looking forward to reading any suggestions to improve this piece of code! Thank you! :-)
#include <stdio.h>
void print_vector(int,float *);
void save_vector(n,v+i);
int main(void)
{
const int n=4;
int i;
float v[4];
puts("Enter the 4 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
return 0;
}
void save_vector(int N, float v+i)
{
int i;
for(i=0;i<n;i++)
scanf("%f",v+i);
}
void print_vector(int N, float V[N])
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
You have to change the declare to
void save_vector(int ,float *);
or with the name of the arguments, if you want.
void save_vector(int N, float * v)
Because, n and v+i are not declared, so you can not use them in the declaration of save_vector function.
And in the implementation of save_vector:
void save_vector(int N, float * v)
{
int i;
for(i=0;i<N;i++)
scanf("%f",v+i);
}

Can't find error in my c code

can't find an error please help, this is a C code to find minimum number of possible quadrangles on co-ordinate plane
#include <stdio.h>
int quadrangle(int *,int);
int min(int,int);
int main(){
int t,i,j,n,p[n][n];
printf("\nEnter the number of test cases");
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
for(j=0;j<n;j++){
scanf("%d %d",&p[j][0],&p[j][1]);
}
printf("%d",quadrangle(&p[0][0],n));
}
return 0;
}
int quadrangle(int *p,int len){
int f=0,s=0,t=0,fo=0,i;
for(i=0;i<len;i++){
if(*(p+i*len)>0&&*(p+i*len+1)>0)
f++;
if(*(p+i*len)>0&&*(p+i*len+1)<0)
s++;
if(*(p+i*len)<0&&*(p+i*len+1)<0)
t++;
if(*(p+i*len)<0&&*(p+i*len+1)>0)
fo++;
}
return min(min(f,s),min(t,fo));
}
int min(int a,int b){
if(a>b) return b;
else return a;}
I tested it on codeblocks software it is compiling okay but ends abruptly when I execute it . The control doesn't even enter main() .
n is uninitialized and you create an array of size n (guess what's the value of n) here:
int t,i,j,n,p[n][n];
Declare the array after n gets initialized, i.e, change
int t,i,j,n,p[n][n];
to
int t,i,j,n;
and add
int p[n][n];
after
scanf("%d",&n);

sort function doesnt work, the simulation can not go on

I'm trying to make a sort function to sort the order of float value incompatible pointer. and when u run the code, after typing the size of pointed array and input the value, then the running just stopped. I do not know where the problem is, any one can help.
I already corrected the warning, but still now result for running the code
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, float *input, float *output);
int main(void) {
int a;
float *b=&b1;
float *c=&c1;
int i, i1;
printf("input the size\n");
scanf("%d", &a);
b=(float*)malloc(sizeof(int)*a);
c=(float*)malloc(sizeof(int)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
for(i1=0; i1<a; i1++){
c[i1]=b[i1];
printf("%f\n", c[i1]);
}
sort(10, b, c);
free(b);
free(c);
return 0;
}
void sort(const int size, float *input, float *output)
{
void swap( float *element1Ptr, float *element2Ptr);
int pass;
int j;
int i0;
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(input[j]>input[j+1]){
swap(&input[j], &input[j+1]);
}
}
}
for (i0=0; i0<size; i0++){
output[i0]=input[i0];
printf("%f", output[i0]);
}
}
void swap( float *element1Ptr, float *element2Ptr)
{
float hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
There were a couple of bugs in your code.
No memory was allocated for c.
You modified the input array in sort.
One print loop looped to 10.
Also, I cleaned up the formatting a bit.
I move the forward declaration so that it is outside the sort function. Not a bug, but programmers expect forward declarations to be put outside any function.
I removed unecessary printf statements and print only the sorted array.
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, const float *input, float *output);
void swap( float *element1Ptr, float *element2Ptr);
int main(void) {
int a;
float *b;
float *c;
int i, i1;
printf("input the size\n");
scanf("%d", &a);
b = malloc(sizeof(float)*a);
c = malloc(sizeof(float)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
sort(a, b, c);
for(i1=0; i1<a; i1++){
printf("%f\n", c[i1]);
}
free(b);
free(c);
return 0;
}
void sort(const int size, float const *input, float *output)
{
int pass;
int j;
int i0;
for (i0=0; i0<size; i0++){
output[i0]=input[i0];
}
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(output[j]>output[j+1]){
swap(&output[j], &output[j+1]);
}
}
}
}
void swap( float *element1Ptr, float *element2Ptr)
{
float hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
General advice:
Turn up the warning level of the compiler. Compiler warnings are there for a reason.
Fix 1: First you are assigning two float addresses to pointers c and d-
float *b=&b1;
float *c=&c1;
Then you are allocating memory for it. It has no meaning at all. when you allocate memory the newly allocated memory address is returned to the pointer b and c.
if you want to make 0 to all allocated memory you can use calloc to allocate memory. because it will allocate the memory and clear the data in it and give it to user
float *b= (float *)calloc(a,sizeof(float));
float *c= (float *)calloc(a,sizeof(float));
Fix 2: You are having float pointer. but after allocating memory you are typecasting the memory as int *-
float *b=&b1;
but
b=(int*)malloc(sizeof(int)*a); // don't do this
Instead use-
b=malloc(sizeof(float)*a);
Fix 3: With out allocating memory for float *c you are assigning values to it-
for(i1=0; i1<a; i1++){
c[i1]=b[i1]; // note here. you have not allocated memory for c before
printf("%f\n", c[i1]);
}
Allocate the memory for float *c and do it.
c = malloc(sizeof(float)*a);
A simple program to do your work-
#include <stdio.h>
#include <stdlib.h>
void sort(const int size, float *input);
int main(void) {
int a,i;
float *b;
printf("input the size\n");
scanf("%d", &a);
b=(float*)malloc(sizeof(float)*a);
for(i=0; i<a ; i++){
scanf("%f", &b[i]);
}
sort(a, b);
for (i=0; i<a; i++)
printf("%f\n",b[i]);
free(b);
return 0;
}
void sort(const int size, float *input)
{
int pass,j,temp;
for (pass=0; pass<size-1;pass++)
{
for (j=0; j<size-1;j++){
if(input[j]>input[j+1]){
temp = input[j];
input[j]=input[j+1];
input[j+1]=temp;
}
}
}
}
Don't use unnecessary variables, Other then the important ones! If you want a copy of your input, copy it to another array and to the sorting on the output array, not on input array!
You defined variable b as having type float *
float *b=&b1;
but are trying to assign pointer to int to that variable
b=(int*)malloc(sizeof(int)*a);
First of all there is no sense in the initialization of b
float *b=&b1;
and secondly it seems you want to allocate an array of floats. So you have to write
b = ( float * )malloc( sizeof( float ) * a );
Also you did not allocate memory pointed to by c. So this code
for(i1=0; i1<a; i1++){
c[i1]=b[i1];
printf("%f\n", c[i1]);
}
sort(10, b, c);
is invalid and the program has undefined behaviour. It is also unclear why you are using magic number 10 in sort instead of variable a.
And what is the sense in defining variables b1 and c1?
float b1=0;
float c1=0;
It seems they are not used.

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

I'm trying to fill an Array with user input, I've already prompted the user now I'm trying to write a fillArray function

#include <stdio.h>
#include <stdlib.h>
#define MAX 25
int readTotalNums();
void fillArray (int nums[], int total);
void sortIt (int nums[], int total);
int findMean (int nums[], int total);
int findMedian (int nums[], int total);
int findMode (int nums[], int total);
void printResults (mean, median, mode);
int goAgain ();
int main()
{
int nums[MAX];
int total;
double mean, median, mode;
do
{
total=readTotalNums();
fillArray(total,nums);
sortIt(nums, total);
mean= findMean (nums,total);
mode=findMode(nums,total);
median=findMedian(nums, total);
printResults(mean,mode, median);
}while(goAgain());
return 0;
}
int readTotalNums()
{
int total;
do
{
printf("How many numbers would you like to enter? (1-25)\n");
scanf("%i",&total);
while(getchar()!='\n');
}while (total<1 || total>MAX);
return total;
}
void fillArray (int nums[], int total)
{
int x;
for (x=0; x<total; x++)
{
printf("enter your numbers\n");
scanf("%i", &nums[x]);
while(getchar()!='\n');
}
}
So I decided to just put up what I have already... because maybe the problem comes before my fillArray function...
I keep getting a "This program has stopped working" message, which, I know you usually get if you don't strip your carriage return. I'm very much a newbie, just trying to make it though my only programming class I have to take for my major, so any help would be appreciated!!
You should pass to scanf a pointer to the location it should store the input, not the value that is already there. scanf("%i", &nums[x]);
The second argument to scanf should be a pointer. Here you are passing an integer. Since this is homework I won't give you the exact code, but that is the problem as far as I can tell.
The scanf function expects pointers, not integers. Didn't you get a compiler warning when you compiled? Try this:
scanf("%i", nums + x);
or equivalently
scanf("%i", &nums[x]);

Resources