Unable to find determinant using partial pivoting - c

whats the problem. It executes but doesnt show the correct answer.
It gets compiled as well.
I've used calling function in many places.
its assumed that the matrix is square and i give the input througgh terminal.
for ex=3
then a random 3x3 matrix but the value seems to be incorrect
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void row(int r1,int r2,int n,float A[n][n]){
int c;
float temp[n][n];
for(c=0;c<n;c++){
temp[r1][c]=A[r1][c];
A[r1][c]=A[r2][c];
A[r2][c]=temp[r1][c];
}
}
void maximum(int i, int n,float A[n][n]){
int j;
float max=fabs(A[i][i]);
for(j=i+1;j<n;j++){
if(fabs(A[j][i])>max){
max=fabs(A[j][i]);
row(i,j,n,A);
}
}
}
void op(int k, int n,float A[n][n]){
int i,j;
float f;f
for(i=1;i<n-1-k;i++){
f=-(A[k+i][k]/A[k][k]);
for(j=0;j<n-1-k;j++){
A[k+i][k+j]=A[k+i][k+j]+f*(A[k][k+j]);
}
}
}
int main(){
int i,j,n;
printf("Enter the order of the matrix:");
scanf("%d",&n);
float A[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
scanf("%f",&A[i][j]);
}
float det=1;
for(i=0;i<n-1;i++){
maximum(i,n,A);
op(i,n,A);
det*=A[i][i];
}
det*=A[n-1][n-1];
printf("%f\n",det);
return 0;
}

Related

Segmentation fault (core dumped) in C while working with Bucket Sort

I have written a program in C for bucket sort:
#include <stdio.h>
#include <stdlib.h>
#define M 100
void insertion_sort(int arr[], int n){
int i, j, k;
for(i=1;i<n;i++){
j=i-1;
k=arr[i];
while(j>=0 && arr[j]>k){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=k;
}
}
int max(int arr[], int n){
int max=arr[0];
for(int i=0;i<n;i++){
if(arr[i]>max)
max=arr[i];
}
return max;
}
void bucketsort(int arr[], int n){
int i, j, idx=0, *ptr, **bsort;
int len=sizeof(int *)*n+sizeof(int)*n*n;
bsort=(int **)malloc(len);
int len1=n*max(arr, n);
ptr=(int *)malloc(len1*sizeof(int));
for(i=0;i<n;i++){
*(ptr+i)=0;
}
for(i=0;i<n;i++){
int bindex=n*arr[i];
bsort[bindex][*(ptr+bindex)]=arr[i];
*(ptr+bindex)+=1;
}
for(i=0;i<n;i++){
if(*(ptr+i)>0)
insertion_sort(bsort[i], *(ptr+i));
}
for(i=0;i<n;i++){
for(j=0;j<*(ptr+i);j++)
arr[idx++]=bsort[i][j];
}
printf("The sorted array:\n");
for(i=0;i<n;i++){
printf("%d ", arr[i]);
}
}
int main()
{
int arr[M],n,i;
char op[1];
do{
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bucketsort(arr,n);
printf("\nDo you want to go again?(Y/N):");
scanf("%s", op);
}while(op[0]=='Y');
}
However, when I run the code, and give the necessary input, it gives me:
Segmentation fault (core dumped)
Why is this so? What sort of changes do I have to make in my code?
I've tried debugging on GDB, and it shows an error at the line
bsort[bindex][*(ptr+bindex)]=arr[i];
I don't understand why it should be so, since I have already written a max function for the same.

using pointer subscript inside an array

i wrote a program for counting sort using pointers
#include<stdio.h>
#include<stdlib.h>
int max(int *arr,int n)
{
int i;
int maxi=*arr;
for(i=0;i<n;i++)
{
if(*(arr+i)>maxi)
maxi=*(arr+i);
}
return maxi;
}
void count(int *arr,int n)
{
int i;
int s=max(arr,n);
printf("max %d",s);
int c[s];
memset(c,0,s*sizeof(int));
int b[n];
for(i=0;i<n;++i)
{
++c[arr[i]];
}
for(i=1;i<s;++i)
{
c[i]=c[i]+c[i-1];
}
for(i=n-1;i>=0;--i)
{
b[c[arr[i]]]=arr[i];
--c[arr[i]];
}
for(i=0;i<n;++i)
{
arr[i]=b[i];
}
}
void print(int *arr,int n)
{
int i;
for(i=0;i<n;i++)
printf("\nelement is %d ",*(arr+i));
}
int main()
{
int n,i;
int *arr=malloc(n*sizeof(int));
printf("\nenter the number of elements ");
scanf("%d ",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
count(arr,n);
print(arr,n);
return 0;
}
as i debug the program it stops at ++c[arr[i]] giving a segmentation fault. My call watches window looks like this
my inputs were like this
enter the number of elements 4
1
3
5
7
max 7
the code received segmentation fault after this also, the value of i is initialized to 0,then why is the watches window showing it's value as 4. is it fine to pass pointer subscripts to a static array or should i declare c dynamically?

How can I override the components of a vector entered by a user with new values?

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), prints it (using function called print_vector) and if any component is negative, it also prints it with all components in absolute value (positives) using the function absolute_values.
Now I would like to only work with the vector that has the absolute values. How could I save the new absolute values into the same vector and override the ones entered by the user?
Looking forward to reading any suggestions to improve this piece of code! Thank you! :-)
#include <stdio.h>
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
printf(" %.2f ", ((V[i]<0)?-V[i]:V[i]));
}
}
Just leaving here the final answer after reviewing the comment section and following #Some programmer dude advice! :-)
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
V[i]=((V[i]<0)?-V[i]:V[i]);
printf(" %f", V[i]);
}
}

Passing multidimensional arrays to a function

The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}

Selection sort on array in C

I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}

Resources