How to make this program
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(){
clrscr();
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf("a[%d][%d] = %d \n",i,j,a[i][j]);
printf("cu pointer\n");
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf("a[%d][%d] = %d \n",i,j,*(p+i));
getch();
}
int a[4][4],b4[3][3],b1[3][3],b2[3][3],b3[3][3],n,c1,c2,c3,c4,i,j;
printf("\nIntroduceti elementele Matricei cu 4 linii si 4 coloane: \n");
int *mat;
n=4;
for (i=0; i<n; i++){
for (j=0; j<n; j++){
printf("a[%d][%d]=",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n Matri: \n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
printf("%2d ",a[i][j]);
}
printf("\n");
}
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
b1[i][j]=a[i+1][j+1];;
}
printf("\n");
}
printf("\t Matri b1 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b1[i][j]);
}
printf("\n");
}
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
b2[i][j]=a[i][j];
}
printf("\n");
}
printf("\tMatri b2: \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b2[i][j]);
}
printf("\n");
}
for (i=0; i<n-2; i++){
for (j=0; j<n-2; j++){
b3[i][j]=a[i][j];
}
b3[2][0]=a[3][0];
b3[2][1]=a[3][1];
b3[2][2]=a[3][3];
b3[1][2]=a[1][3];
b3[0][2]=a[0][3];
printf("\n");
}
printf("\tMatri b3 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b3[i][j]);
}
printf("\n");
}
for (i=0; i<n-2; i++){
for (j=0; j<n-2; j++){
b4[i+1][j+1]=a[i+2][j+2];
}
b4[0][0]=a[0][0];
b4[1][0]=a[2][0];
b4[2][0]=a[3][0];
b4[0][1]=a[0][2];
b4[0][2]=a[0][3];
printf("\n");
}
printf("\tMatri b4 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b4[i][j]);
}
printf("\n\n");
}
c1=a[0][0]*( b1[0][0]*b1[1][1]*b1[2][2] + b1[1][0]*b1[2][1]*b1[0][2] + b1[2][0]*b1[0][1]*b1[1][2] - b1[0][2]*b1[1][1]*b1[2][0] - b1[1][2]*b1[2][1]*b1[0][0] - b1[1][0]*b1[0][1]*b1[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[1][1]: %d", c1);
c2=a[1][1]*( b4[0][0]*b4[1][1]*b4[2][2] + b4[1][0]*b4[2][1]*b4[0][2] + b4[2][0]*b4[0][1]*b4[1][2] - b4[0][2]*b4[1][1]*b4[2][0] - b4[1][2]*b4[2][1]*b4[0][0] - b4[1][0]*b4[0][1]*b4[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[2][2]: %d", c2);
c3=a[2][2]*( b3[0][0]*b3[1][1]*b3[2][2] + b3[1][0]*b3[2][1]*b3[0][2] + b3[2][0]*b3[0][1]*b3[1][2] - b3[0][2]*b3[1][1]*b3[2][0] - b3[1][2]*b3[2][1]*b3[0][0] - b3[1][0]*b3[0][1]*b3[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[3][3]: %d", c3);
c4=a[3][3]*( b2[0][0]*b2[1][1]*b2[2][2] + b2[1][0]*b2[2][1]*b2[0][2] + b2[2][0]*b2[0][1]*b2[1][2] - b2[0][2]*b2[1][1]*b2[2][0] - b2[1][2]*b2[2][1]*b2[0][0] - b2[1][0]*b2[0][1]*b2[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[4][4]: %d", c4);
getch();
return 0;
}
with pointers, This is a program that calculates the algebraic complement
Sorry for my english
why do you want to do that? I see no advantage of using pointers in that program, they could actually make it more difficult to read and comprehend the algorithm.
i want just to see how the pointers will work in this program ,how to use pointers for this cind of problems
First, here's a tutorial on pointers:
/*
============================================================================
Name : PointersExplained1.c
Author : Dean M. Sands, III
Version : 0.1a
Copyright : I will set fire to the flamingos in your front yard.
Description : A code explanation of pointers
============================================================================
*/
#include <malloc.h>
#include <stdio.h>
#include <conio.h>
int *i; //Define i as a pointer. Its present value is meaningless.
int j=0; //Define j as an integer with a value of 0.
void pointerAsAReference(){
printf("j is an integer that contains: %d\n", j);
printf("i is an int pointer that contains: %d\n", i);
printf("And no one cares about i right now since we haven't given it a useful value.\n");
printf("That's right, i. No one likes you.\n");
i=&j; //Assign the location of j into i;
printf("We plugged j's location into i.\n");
printf("j still contains: %d\n", j);
printf("i now contains: %d\n", i);
printf("OK, i...\nI apologize for the hurtful things I said earlier. You're truly useful now.\n");
printf("...\n");
printf("No, you can't borrow money.\n");
*i=4; //Assign the number 4 to the space pointed to by i. j now contains 4.
printf("Hey look what we can do! We assigned a value to the space pointed to by i\n");
printf("i still contains the address: %d\n", i);
printf("j now contains: %d\n", j);
printf("You're so special, i!\n");
}
void pointerAsSingleElementArray(){
i[0]=10; //Pointers can be used as variables.
printf("Ooh! This is neat. Let's use i as a single element array!\n");
printf("j now contains: %d\n", j);
}
void pointersToAllocatedBuffers(){
int x; //loop variable
//Allocate a buffer the size of j integers and plug its pointer into i.
i=(int*)malloc(j*sizeof(int));
printf("Now we're going to allocate a buffer %d bytes long.\nThis can be used as an int array that is %d elements long.\n", j*sizeof(int), j);
printf("i now contains: %d\n", i);
printf("j still contains: %d\n", j);
printf("Feeling important yet, i?\n");
//Fancy time!
printf("Now we're doing something really super!\n");
printf("We're going to plug cube numbers into i's array - just because we can!\n");
for(x=0;x<j;x++){
i[x]=x*x*x;
printf("i[%d]=%d\n", x, i[x]);
}
//i now contains a pointer that can be used as an array.
//Everything allocated during runtime needs to be freed during runtime.
free(i);
}
int main(){
pointerAsAReference();
pointerAsSingleElementArray();
pointersToAllocatedBuffers();
return 0;
}
Now your new code:
/*
============================================================================
Name : Pointers.c
Author : dan1992
Editor : Dean M. Sands, III
Version : 0.2
Copyright : I will steal your lawn gnomes. And then the flamingos burn.
Description : Algebraic Complement in C with Pointers
============================================================================
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <malloc.h>
int **allocateMatrix(unsigned int rows, unsigned int columns){
int i, **matrix;
//Malloc only allocates X number of bytes. It doesn't care that you want X number of INT pointers.
//An INT pointer is a lot bigger than a byte.
//So we have to specify:
matrix=(int**)malloc(rows*sizeof(int*)); //Allocate pointers to rows
printf("Matrix allocated!\n");
if(matrix==NULL)
return NULL;
for(i=0;i<rows;i++){
matrix[i]=(int*)malloc(columns*sizeof(int)); //Allocate each row
printf("Row allocated!\n");
if(matrix[i]==NULL){
return NULL;
}
}
return matrix;
}
void deallocateMatrix(int **matrix, unsigned int rows){
int i;
for(i=0;i<rows;i++){
free(matrix[i]);
}
free(matrix);
}
int main(){
//int a[4][4],b4[3][3],b1[3][3],b2[3][3],b3[3][3],n,c1,c2,c3,c4,i,j;
unsigned int n;
int **a,**b4,**b1,**b2,**b3,c1,c2,c3,c4,i,j;
n=4;
//print("What size square matrix do you want? ");
//scanf("%d", &n);
a=allocateMatrix(n,n);
if(a==NULL){
printf("Could not allocate a.\n");
return 0;
}
b1=allocateMatrix(n-1,n-1);
if(b1==NULL){
printf("Could not allocate b1.\n");
return 0;
}
b2=allocateMatrix(n-1,n-1);
if(b2==NULL){
printf("Could not allocate b2.\n");
return 0;
}
b3=allocateMatrix(n-1,n-1);
if(b3==NULL){
printf("Could not allocate b3.\n");
return 0;
}
b4=allocateMatrix(n-1,n-1);
if(b4==NULL){
printf("Could not allocate b4.\n");
return 0;
}
//printf("\nIntroduceti elementele Matricei cu 4 linii si 4 coloane: \n");
printf("\nEnter the matrix elements with 4 rows and 4 columns: \n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
printf("a[%d][%d]=",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n Matrix: \n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
printf("%2d ",a[i][j]);
}
printf("\n");
}
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
b1[i][j]=a[i+1][j+1];;
}
printf("\n");
}
printf("\t Matrix b1 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b1[i][j]);
}
printf("\n");
}
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
b2[i][j]=a[i][j];
}
printf("\n");
}
printf("\tMatrix b2: \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b2[i][j]);
}
printf("\n");
}
for (i=0; i<n-2; i++){
for (j=0; j<n-2; j++){
b3[i][j]=a[i][j];
}
b3[2][0]=a[3][0];
b3[2][1]=a[3][1];
b3[2][2]=a[3][3];
b3[1][2]=a[1][3];
b3[0][2]=a[0][3];
printf("\n");
}
printf("\tMatrix b3 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b3[i][j]);
}
printf("\n");
}
for (i=0; i<n-2; i++){
for (j=0; j<n-2; j++){
b4[i+1][j+1]=a[i+2][j+2];
}
b4[0][0]=a[0][0];
b4[1][0]=a[2][0];
b4[2][0]=a[3][0];
b4[0][1]=a[0][2];
b4[0][2]=a[0][3];
printf("\n");
}
printf("\tMatrix b4 : \n");
for (i=0; i<n-1; i++){
for (j=0; j<n-1; j++){
printf("%2d ",b4[i][j]);
}
printf("\n\n");
}
c1=a[0][0]*( b1[0][0]*b1[1][1]*b1[2][2] + b1[1][0]*b1[2][1]*b1[0][2] + b1[2][0]*b1[0][1]*b1[1][2] - b1[0][2]*b1[1][1]*b1[2][0] - b1[1][2]*b1[2][1]*b1[0][0] - b1[1][0]*b1[0][1]*b1[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[1][1]: %d", c1);
c2=a[1][1]*( b4[0][0]*b4[1][1]*b4[2][2] + b4[1][0]*b4[2][1]*b4[0][2] + b4[2][0]*b4[0][1]*b4[1][2] - b4[0][2]*b4[1][1]*b4[2][0] - b4[1][2]*b4[2][1]*b4[0][0] - b4[1][0]*b4[0][1]*b4[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[2][2]: %d", c2);
c3=a[2][2]*( b3[0][0]*b3[1][1]*b3[2][2] + b3[1][0]*b3[2][1]*b3[0][2] + b3[2][0]*b3[0][1]*b3[1][2] - b3[0][2]*b3[1][1]*b3[2][0] - b3[1][2]*b3[2][1]*b3[0][0] - b3[1][0]*b3[0][1]*b3[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[3][3]: %d", c3);
c4=a[3][3]*( b2[0][0]*b2[1][1]*b2[2][2] + b2[1][0]*b2[2][1]*b2[0][2] + b2[2][0]*b2[0][1]*b2[1][2] - b2[0][2]*b2[1][1]*b2[2][0] - b2[1][2]*b2[2][1]*b2[0][0] - b2[1][0]*b2[0][1]*b2[2][2] );
printf("\nComplementul algebric fata de elementul diagonalei principala a[4][4]: %d", c4);
deallocateMatrix(a,n);
deallocateMatrix(b1,n-1);
deallocateMatrix(b2,n-1);
deallocateMatrix(b3,n-1);
deallocateMatrix(b4,n-1);
getch();
return 0;
}