loop not iterating in c - c

I have recently started coding and was trying to write a program to find the inverse of a matrix using Gauss Jordan method. I try to transform the given matrix to an identity matrix by using ro transformations and apply the same to corresponding elements of identity matrix.
#include <stdio.h>
void main()
{
float a[5][5],b[5][5]={};
int n,i,j,k,m,o,p,q;
printf("Enter order n of square matrix : ");
scanf("%d",&n);
printf("Enter %d elements : ",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10f",a[i][j]);
printf("\n");
}
if(n>=3)
{
for(i=0;i<n;i++)
b[i][i]=1;
for(i=0;i<n-1;i++)
{
if(a[i][i]==0)
for(m=i+1;m<n;m++)
if(a[m][i]!=0)
{
for(o=0;o<n;o++)
{
a[m][o]+=a[m][o]+a[i][o];
a[i][o]=a[m][o]-a[i][o];
a[m][o]=a[m][o]-a[i][o];
b[m][o]+=b[m][o]+b[i][o];
b[i][o]=b[m][o]-b[i][o];
b[m][o]=b[m][o]-b[i][o];
}
break;
}
for(j=i+1;j<n;j++)
{if(a[j][i]!=0)
{
for(k=0;k<n;k++)
{
b[j][k]=b[j][k]*a[i][i]-a[j][i]*b[i][k];
a[j][k]=a[j][k]*a[i][i]-a[j][i]*a[i][k];
}
}
for(p=0;p<n;p++)
{
for(q=0;q<n;q++)
printf("%10f",a[p][q]);
printf("\t\t");
for(q=0;q<n;q++)
printf("%10f",b[p][q]);
printf("\n");
}}
}
for(i=n-1;i>0;i--)
for(j=i-1;j>=0;j--)
if(a[j][i]!=0)
{
for(k=0;k<n;k++)
{
b[j][k]=b[j][k]*a[i][i]-a[j][i]*b[i][k];
a[j][k]=a[j][k]*a[i][i]-a[j][i]*a[i][k];
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
b[i][j]=(b[i][j])/(a[i][i]);
printf("\n\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10f",b[i][j]);
printf("\n");
}
}
}
It is not giving the required output(correct inverse) and so i tried to print the two matrices after each row transformation(for the beginning part). Then i found that one of the loops was not iterating(the k variableloop) and loop was only running for k=0.I can't seem to find what is wrong.
the input which i was using was 3 , 1 2 3 4 5 6 7 8 8

Related

Designing and utilizing variable matrix in C

I am having some trouble designing a matrix in my project. How would you define a variable matrix? I am using an LED Matrix for a connect four game using HCS12 Dragon 12 Light, and I want to define a matrix that allows me to light up the board. How do I define a variable matrix [8]x[8] with rows x columns? Furthermore, how might I call certain points of this matrix in other functions like if else statements? Thanks for any responses
To clarify further, I am just wondering about syntax. I want to define an 8x8 matrix 2D array that is variable based on the game results. Furthermore, I am wondering how I would be able to call certain entries in the matrix in functions and statements
#include <stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
printf("Enter the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the second matrix\n");
scanf("%d%d",&r,&s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\n Enetr the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}

How to sort 2d array column wise and row wise for given columns and rows by users

guys, this is my first time in stackoverflow
I want to sort array of random numbers row wise and column wise
after this I have to search for a given key but I am stocked in
the first part.
the problem is user should choose dimension of array between (2-5000) and elements in array is between (4-25 milion) and each element can be from 0 until int MAX and number of column and row is equal to each other
I found good code but I did not understand how to change it in order to satisfy the limit of my programm I tried to #define my 2d array like this
define R 5000
define C 5000
but it is wrong
this is the code I am trying to write
#include<stdio.h>
#define R 500
#define C 500
void sort_rows(int *arr,int n)
{
int i;
for(i=1;i<n;i++)
{
int key=arr[i];
int j=i-1;
while(j>=0 && key<arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
void sort_column(int arr[500][500],int c)
{
int key,k;
int i,j;
for(i=0;i<c;i++)
{
for(j=1;j<c;j++)
{
key=arr[j][i];
k=j-1;
while(k>=0 && arr[k][i]>key)
{
arr[k+1][i]=arr[k][i];
k--;
}
arr[k+1][i]=key;
}
}
}
int main()
{
int arr[R][C];
int r,c;
int i,j,size_row,size_column;
printf("Enter Number of rows and columns(2-5000)");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
arr[i][j]=rand()%(r*c)+1;
}
printf("Original array:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
size_row=((sizeof(arr)/sizeof(arr[0][0])/(sizeof(arr[0])/sizeof(arr[0][0]))));
printf("%d",size_row);
for(i=0;i<size_row;i++)
{
sort_rows(arr[i],sizeof(arr[i])/sizeof(arr[i][0]));
}
size_column=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<size_column;i++)
{
sort_column(arr,size_column);
}
printf("Sorted Array:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
any help would be great
You have declared your array with R and C that you defined with #define, but you read new values, r and c, from user and work on it.
You should define your array dimensions with the values you will use.
Also, you have passed an integer to sort_column which considered as call by value, but you should pass array pointer to be able to edit your array, and that is call by reference.
Hope that helps.
You usually can't. A 50000x50000 2D array has 50000x50000x(int size) bits.
This is (with int size equals 4 bytes) 10 GB of memory, more than your system usually allows.
The are ways to change OS limits, but it is easier in this case to make your array global.
This code has a lot of awkward lines that look either unnecessary or wrong, so I've shortened and changed it a little to make it work like intended to.
Keep in mind that these functions you got are running insertion sort, a pretty inefficient sorting algorithm (O(n^2)). The program will take A LOT of time to run with big instances. You might want to look for better algorithms, like merge sort and quick sort.
Modified algorithm:
#include <stdio.h>
#include <stdlib.h>
#define R 50000
#define C 50000
int arr[R][C];
void sort_rows(int *arr,int n)
{
int i;
for(i=1;i<n;i++)
{
int key=arr[i];
int j=i-1;
while(j>=0 && key<arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
void sort_column(int arr[][C],int r,int c)
{
int key,k;
int i,j;
for(i=0;i<r;i++)
{
for(j=1;j<c;j++)
{
key=arr[j][i];
k=j-1;
while(k>=0 && arr[k][i]>key)
{
arr[k+1][i]=arr[k][i];
k--;
}
arr[k+1][i]=key;
}
}
}
int main()
{
int r,c;
int i,j,size_row,size_column;
printf("Enter Number of rows and columns(2-%d,2-%d)",R,C);
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
arr[i][j]=rand()%(r*c)+1;
}
printf("Original array:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
sort_rows(arr[i],c);
}
sort_column(arr,r,c);
printf("Sorted Array:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}

Odd-Even Sort using cuda Programming

I'm trying to implement odd-even sort program in cuda-c language. But, whenever I give a 0 as one of the elements in the input array, the resulted array is not properly sorted.In other cases, however, it is working for other input.I don't understand what is the problem with the code.Here is my code:
#include<stdio.h>
#include<cuda.h>
#define N 5
__global__ void sort(int *c,int *count)
{
int l;
if(*count%2==0)
l=*count/2;
else
l=(*count/2)+1;
for(int i=0;i<l;i++)
{
if(threadIdx.x%2==0) //even phase
{
if(c[threadIdx.x]>c[threadIdx.x+1])
{
int temp=c[threadIdx.x];
c[threadIdx.x]=c[threadIdx.x+1];
c[threadIdx.x+1]=temp;
}
__syncthreads();
}
else //odd phase
{
if(c[threadIdx.x]>c[threadIdx.x+1])
{
int temp=c[threadIdx.x];
c[threadIdx.x]=c[threadIdx.x+1];
c[threadIdx.x+1]=temp;
}
__syncthreads();
}
}//for
}
int main()
{int a[N],b[N],n;
printf("enter size of array");
scanf("%d",&n);
print("enter the elements of array");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("ORIGINAL ARRAY : \n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
int *c,*count;
cudaMalloc((void**)&c,sizeof(int)*N);
cudaMalloc((void**)&count,sizeof(int));
cudaMemcpy(c,&a,sizeof(int)*N,cudaMemcpyHostToDevice);
cudaMemcpy(count,&n,sizeof(int),cudaMemcpyHostToDevice);
sort<<< 1,n >>>(c,count);
cudaMemcpy(&b,c,sizeof(int)*N,cudaMemcpyDeviceToHost);
printf("\nSORTED ARRAY : \n");
for(int i=1;i<=n;i++)
{
printf("%d ",b[i]);
}
}
Your kernel code had two main errors that I could see:
On the odd phase (for even length array, or even phase for odd length array), your last thread will index out of bounds at c[threadIdx.x+1]. For example, for 4 threads, they are numbered 0,1,2,3. Thread 3 is odd, but if you access c[3+1], that is not a defined element in your array. We can fix this by restricting each phase to work on all threads but the last one.
You were using __syncthreads() inside a conditional statement that would not allow all threads to reach the barrier. This is a coding error. Read the documentation. We can fix this by adjusting what code is inside the conditional regions.
In the main code, your final printout statements were indexing incorrectly:
for(int i=1;i<=n;i++)
that should be:
for(int i=0;i<n;i++)
You also have typo here:
print("enter the elements of array");
I assume that should be printf.
The following code has the above errors fixed, and seems to run correctly for me for arrays up to length 5 (your hardcoded limit on N). Even if you increased N, I'm not sure this would work beyond the size of a warp and certainly would not work beyond the threadblock size, but hopefully you are aware of that already(if not, read the doc link about __syncthreads()).
"Fixed" code:
#include<stdio.h>
#include<cuda.h>
#define N 5
#define intswap(A,B) {int temp=A;A=B;B=temp;}
__global__ void sort(int *c,int *count)
{
int l;
if(*count%2==0)
l=*count/2;
else
l=(*count/2)+1;
for(int i=0;i<l;i++)
{
if((!(threadIdx.x&1)) && (threadIdx.x<(*count-1))) //even phase
{
if(c[threadIdx.x]>c[threadIdx.x+1])
intswap(c[threadIdx.x], c[threadIdx.x+1]);
}
__syncthreads();
if((threadIdx.x&1) && (threadIdx.x<(*count-1))) //odd phase
{
if(c[threadIdx.x]>c[threadIdx.x+1])
intswap(c[threadIdx.x], c[threadIdx.x+1]);
}
__syncthreads();
}//for
}
int main()
{int a[N],b[N],n;
printf("enter size of array");
scanf("%d",&n);
if (n > N) {printf("too large!\n"); return 1;}
printf("enter the elements of array");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("ORIGINAL ARRAY : \n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
int *c,*count;
cudaMalloc((void**)&c,sizeof(int)*N);
cudaMalloc((void**)&count,sizeof(int));
cudaMemcpy(c,&a,sizeof(int)*N,cudaMemcpyHostToDevice);
cudaMemcpy(count,&n,sizeof(int),cudaMemcpyHostToDevice);
sort<<< 1,n >>>(c,count);
cudaMemcpy(&b,c,sizeof(int)*N,cudaMemcpyDeviceToHost);
printf("\nSORTED ARRAY : \n");
for(int i=0;i<n;i++)
{
printf("%d ",b[i]);
}
printf("\n");
}
The usual recital about proper cuda error checking belongs here.

Matrix multiplication

I can't understand where is the mistake. Help me correct it please. The output is coming as all the elements of resultant matrix being zero.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i=0,j=0,row1,col1,row2,col2,row3,col3,s=0,k=0,l=0;
printf("Enter no. of rows and no. of columns of first matrix:\n");
scanf("%d %d",&row1,&col1);
printf("Enter no. of rows and no. of columns of second matrix:\n");
scanf("%d %d",&row2,&col2);
if(col1==row2)
{
row3=row1;
col3=col2;
}
else
{
printf("Not possible!");
exit(1);
}
printf("Enter elements of first matrix:\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf("%d",&b[i][j]);
}
}
i=0;
j=0;
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
while(i<row3 || j<col3)
{
//printf("Hi");
s=s+a[i][j++]*b[i++][j];
//printf("%d\n",s);
}
}
printf("%d\n",s);
c[k][l]=s;
s=0;
}
printf("Sum matrix is:\n");
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
printf("%d ",c[k][l]);
}
printf("\n");
}
getch();
}
I have included comments of printing in the while loop so as to debug but it's not helping.
You are setting the result outside of your column loop, so you only set one result per row. Change the code to this by moving those 3 lines inside the brace:
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
i = 0; j = 0;
while(i<row3 || j<col3)
{
//printf("Hi");
s=s+a[k][j++]*b[i++][l];
//printf("%d\n",s);
}
// THIS CODE HAS MOVED:
printf("%d\n",s);
c[k][l]=s;
s=0;
}
}
Also, your addition needs to use the k and l indices, so that you move along a row of a[][] given by k and a column of b[][] given by l:
s=s+a[k][j++]*b[i++][l];
You forgot to initialize i and j inside the loop where you add the two matrices.
According to your code add.
i=0; j=0 inside the double for loop for addition.
Hope this helps

program for direct diagonalization of a matrix

I need a c program for direct diagonalization of a matrix.
i tried many ways in solving it but didn't get the desired output. so please help me out.
my code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k;
float a[10][10],x[10],u,m;
printf("Enter the number of equations :");
scanf("%d",&n);
printf("\nEnter the co-efficients of equations\n");
for(i=1;i<=n;i++)
for(j=1;j<=(n+1);j++)
scanf("%f",&a[i][j]);
printf("\nEntered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(i != k)
{
u=a[i][k]/a[k][k];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]-(u*a[k][j]);
}
}
}
for(i=1;i<=n;i++)
{
m=a[i][i];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]/m;
}
}
printf("\nDiagonalised matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
printf("\nsolution vector is\n");
for(i=1;i<=n;i++)
{j=(n+1);
printf("x[%d]=%.2f\n",i,a[i][j]);
}
}
output should be like this, if i enter any coefficient ....lower and upper triangular elements should be zero and i should also need to check whether the determinants value remains same or not.

Resources