Struct and file handling - c

The program I am making must store the values in the text file on their respective variables. The problem seems to arise in the inner loop. I have the respective structs already defined. The program runs properly for the outer loop but when it proceeds in the inner loop, the file pointer does not read the proper values from the text file and outputs "0.0" and does not proceed with the processes from the outer loop altogether.
my input file looks something like this:
GENERAL
1 4
PANELS
1 2.1 3.1 4.1 5.1 6.1 7.1 1
2 2.2 3.2 4.2 5.2 6.2 7.2 2
1 2.21 3.21 4.21 5.21
2 2.22 3.22 4.22 5.22
3 2.3 3.3 4.3 5.3 6.3 7.3 0
4 2.4 3.4 4.4 5.4 6.4 7.4 4
1 2.41 3.41 4.41 5.41
2 2.42 3.42 4.42 5.42
3 2.43 3.43 4.43 5.43
4 2.44 3.44 4.44 5.44
MATERIAL
1 1000.0 2000.0 3000.0 4000.0
2 1010.0 2020.0 3030.0
3 1010.1 2020.2
CHB
1 10 20 30 2
2 11 22 33 1
these are just placeholders to see if they are stored. single digits are integers while floats have decimal points.
here is the code
typedef struct _open
{
int id;
double length;
double height;
double origX;
double origY;
int frames;
double thickness;
double E;
double v;
}CHBOpening;
typedef struct _panels
{
int id;
double length;
double height;
double origX;
double origY;
double origZ;
double angle;
int nOpenings;
int nReinforcement;
double *xReinf;
double sx;
double xReinf0;
CHBUnit *chb;
CHBOpening *openings[];
}CHBPanel;
typedef struct _chb
{
int nStories;
int nModes;
int nIter;
int nPanels;
CHBPanel *panels[];
}CHBStructure;
int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
int i,j;
S->panels = malloc(S->nPanels*sizeof(CHBStructure));
for (i=0; i< S->nPanels; i++)
{
fscanf(fp,"%d",&S->panels[i].id);
fscanf(fp,"%lf",&S->panels[i].length);
fscanf(fp,"%lf",&S->panels[i].height);
fscanf(fp,"%lf",&S->panels[i].angle);
fscanf(fp,"%lf",&S->panels[i].origX);
fscanf(fp,"%lf",&S->panels[i].origY);
fscanf(fp,"%lf",&S->panels[i].origZ);
fscanf(fp,"%d",&S->panels[i].nOpenings);
if (S->panels[i].nOpenings > 0)
{
S->panels[i].openings = malloc(sizeof(CHBOpening)*S->panels[i].nOpenings);
for (j=0; j<S->panels[i].nOpenings;j++)
{
fscanf(fp,"%d",&S->panels[i].openings[j].id);
fscanf(fp,"%lf",&S->panels[i].openings[j].length);
fscanf(fp,"%lf",&S->panels[i].openings[j].height);
fscanf(fp,"%lf",&S->panels[i].openings[j].origX);
fscanf(fp,"%lf",&S->panels[i].openings[j].origY);
}
}
}
return 1;
}

When you allocate pannels you do
S->panels = malloc(S->nPanels*sizeof(CHBStructure));
The problem here is that you use sizeof(CHBStructure) instead of sizeof(CHBPanel). The problem with this is that sizeof(CHBStructure) < sizeof(CHBPanel), so you don't allocate enough memory for the data you read.
That will lead to writing out of bounds, and undefined behavior.
This is in addition to the "typo" in the input file, which will lead you to enter the inner loop when there's nothing to read.

Related

C program is ending by itself during nested loop

My C program is ending on it's own and I can't figure out the issue. I have added a comment above the loop where it is stopping.
I am creating a program to match dna samples in arrays with each other. The dna samples are floats and I am reading them from a file.
This is the file I am reading from:
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
5
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
1.3 0.3 9.5 8.7 5.8 4.1 3.2 2.3 6.2 6.9
6.3 9.3 4.3 6.4 7.5 2.9 3.0 4.1 5.3 6.5
6.1 9.4 4.5 6.6 7.4 2.8 3.2 4.4 5.0 6.0
2.3 3.3 4.5 6.6 7.8 2.2 3.2 4.3 5.2 6.5
The expected output is all the values will be printed (5 lines for the criminal samples) and the first line will be matched and the rest will not. However this is my output:
Reading chromosomes of the suspect.
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
Reading chromosomes of the criminals.
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
1.3 0.3 9.5 8.7 5.8 4.1 3.2 2.3 6.2
I have attempted debugging it and at one point it was randomly assigning my sizeR variable to like 10000 or something during the loop and I assumed that was the problem somewhere but now it's not I'm struggling to understand why it is still stopping for me.
Code:
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
int main(){
fp = fopen("dna_input.txt", "r");
int sizeR = 0, sizeC = 10; // declare size variables
float suspect[sizeC]; // declaring suspect array
float criminal[sizeR][sizeC]; // declaring criminal array
// reads 10 input values from first line of the file
printf("Reading chromosomes of the suspect. \n");
for (int i = 0; i < sizeC; i++){
fscanf(fp, " %f", &suspect[i]);
printf("%.1f ", suspect[i]);
}
printf("\n");
// reads integer from 2nd line of file for the amount of lines to read for next loop
fscanf(fp, " %d", &sizeR);
printf("Reading chromosomes of the criminals. \n");
// read 10 input values into 5 criminal arrays
// THIS LOOP IS WHERE MY PROGRAM IS STOPPING <---------------------------------------------
for (int i = 0; i < sizeR; i++){
for (int j = 0; j < sizeC; j++){
fscanf(fp, " %f", &criminal[i][j]);
printf("%.1f ", criminal[i][j]);
}
printf("\n");
}
// check for match
bool match = true;
for (int i = 0; i < sizeR; i++){
for (int j = 0; j < sizeC; j++){
if (suspect[j] != criminal[i][j]){
match = false;
}
}
// display matching result
if (match)
printf("The two profiles match! \n");
else
printf("The two profiles don't match! \n");
}
fclose(fp);
return 0;
}
You need to dynamically allocate the criminals array.
//Here sizeR is 0 so you are declaring criminal[0][10]
float criminal[sizeR][sizeC]; // declaring criminal array
Typical buffer overflow problem.
Thank you for the comments, I missed that completely. I've moved my criminal array to be declared after the fscanf
// reads integer from file for the amount of lines to read
fscanf(fp, " %d", &sizeR);
// declare criminal aray
float criminal[sizeR][sizeC];

C bitmap: rotation and zoom

I'm recently checking out C for a friend having problems with it for school. As I only have learned java and C#, though it would be easy. But currently stuck on this.
I have a project reading a small bmp (512x512) image. I've managed to change some colors on it and have it rotated (both horizontal as vertical). Though I'm stuck with the -90° rotation.
1. ROTATION (512x512)
Currently I have this code (both getPixel and setPixel are my own functions):
typedef struct _bitmap {
char file_path[PATH_MAX+1];
char magic_number[3];
unsigned int size;
unsigned char application[5];
unsigned int start_offset;
unsigned int bitmapHeaderSize;
unsigned int width;
unsigned int height;
unsigned short int depth;
unsigned char* header;
PIXEL* raster;
} BITMAP;
void rotate(BITMAP* bmp) {
int i;
int j;
PIXEL* originalPixel;
BITMAP* originalBmp;
deepCopyBitmap(bmp, originalBmp);
for(j=1; j <= bmp->height; j++) {
for(i=1; i <= bmp->width; i++) {
originalPixel=getPixel(originalBmp->raster, bmp->width, bmp->height, j, i);
setPixel(bmp->raster, bmp->width, bmp->height, (bmp->width + 1 - i), j, originalPixel);
}
}
}
void deepCopyBitmap(BITMAP* bmp, BITMAP* copy) {
*copy = *bmp;
if (copy->raster) {
copy->raster = malloc(copy->height * sizeof(*copy->raster));
for (int i = 0; i < copy->height; i++) {
copy->raster[i] = malloc(copy->width * sizeof(*copy->raster[i]));
memcpy(copy->raster[i], bmp->raster[i], copy->width * sizeof(*copy->raster[i]));
}
}
}
indirection requires pointer operand ('PIXEL' (aka 'struct _pixel') invalid)
copy->raster[i] = malloc(copy->width * sizeof(*copy->raster[i]));
^~~~~~~~~~~~~~~~
indirection requires pointer operand ('PIXEL' (aka 'struct _pixel') invalid)
memcpy(copy->raster[i], bmp->raster[i], copy->width * sizeof(*copy->raster[i]));
^~~~~~~~~~~~~~~~
expanded from macro 'memcpy' __builtin___memcpy_chk (dest, src, len, __darwin_obsz0 (dest))
This correctly rotates the first diagonal part of the image, but the second part is totally wrong (having two times a part of the first diagonal).
I think the problem is, swapping pixels around and halfway I'm starting to swap already swapped pixels. So I tried to duplicate my bmp, to a original bitmap (originalBmp) and one rotated (rotatedBmp). Though I think it just copies the reference. Anyone has an idea how I create a duplicate bmp?
As example (sorry for the flue img): I want the vertical lines (left), to turn -90deg, so it becomes horizontal lines (right). Though the left diagonal part is correct. But the right part of the diagonal is incorrect copying a piece of the left diagonal. I think because it swaps pixels that are already swapped in the bmp file.
2. ROTATION (512x1024)
What happens if the height or width is the double of the other? Anyone knows how to start on this?
3. ZOOM (200%)
Anyone know how to do this? Get the center pixels of the bitmap, and make them twice at the start of the image, or is there a better/cleaner solution?
1 2 3 4 5 6 7 8 3 3 4 4 5 5 6 6
2 2 3 4 5 6 7 8 3 3 4 4 5 5 6 6
3 3 3 4 5 6 7 8 4 4 4 4 5 5 6 6
4 4 4 4 5 6 7 8 4 4 4 4 5 5 6 6
5 5 5 5 5 6 7 8 5 5 5 5 5 5 6 6
6 6 6 6 6 6 7 8 5 5 5 5 5 5 6 6
7 7 7 7 7 7 7 8 6 6 6 6 6 6 6 6
8 8 8 8 8 8 8 8 6 6 6 6 6 6 6 6
From your code it seems clear that both originalBmpand bmp are pointers to some BMP-type. So when you do originalBmp=bmp;, you just get two pointers pointing to the same BMP, i.e. they operate on the same data.
I assume you have something like
struct BMP
{
// ....
};
If that is the case you can make a copy like this:
struct BMP originalBmp = *bmp;
When using originalBmp you must use the . notation, e.g. originalBmp.raster
EDIT An alternative approach
Instead of making a copy of the original bmp you could do the rotation directly on the original. Each rotation will involve 4 locations. You can copy the 4 locations into temp variables first and then write them to their final location.
For a simple matrix it could be something like this:
#include <stdio.h>
#define WIDTH 4
// display function
void d(int t[WIDTH][WIDTH])
{
int i, j;
for (i=0; i<WIDTH;i++)
{
for (j=0; j<WIDTH; j++)
{
printf("%d ", t[i][j]);
}
printf("\n");
}
}
int main(void) {
int org[WIDTH][WIDTH];
int i, j;
// Just initialize the matrix
for (i=0; i<WIDTH;i++)
{
for (j=0; j<WIDTH; j++)
{
org[i][j] = 10 + i*5 + j;
}
}
printf("Original\n");
d(org);
// Rotate the matrix
for (j=0; j < (WIDTH/2); j++)
{
for (i=0; i < ((WIDTH+1)/2); i++)
{
int t1 = org[j][i];
int t2 = org[i][WIDTH-1-j];
int t3 = org[WIDTH-1-j][WIDTH-1-i];
int t4 = org[WIDTH-1-i][j];
org[j][i] = t2;
org[i][WIDTH-1-j] = t3;
org[WIDTH-1-j][WIDTH-1-i] = t4;
org[WIDTH-1-i][j] = t1;
}
}
printf("Rotated\n");
d(org);
return 0;
}
This will output:
Original
10 11 12 13
15 16 17 18
20 21 22 23
25 26 27 28
Rotated
13 18 23 28
12 17 22 27
11 16 21 26
10 15 20 25
Change to #define WIDTH 5 and it will output:
Original
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
Rotated
14 19 24 29 34
13 18 23 28 33
12 17 22 27 32
11 16 21 26 31
10 15 20 25 30

fscanf() causing segmentation fault

I am writing some code in C to read some file data in arrays and keep getting a segmentation fault compiling with gcc. It reads the file up to 11th line of data then gives the fault. Been through some other similar questions on here but can't find a solution.
Thanks
code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int ainb(char a[],char b[])//returns 0 if str b contains a returns 1 otherwise
{
int i=0,j0=-1,j1=0,count=0;
if(strlen(b)<strlen(a)) {return 1;}
for(i=0;i<strlen(b);i++) {
if((b[i]==a[j1])&&(j1==j0+1)){
j0=j1;j1++;
} else {
j0=-1;j1=0;
}
if((j1+1)==strlen(a)) {break;}
}
if((j1+1)==strlen(a)){
return 0;
} else {
return 1;
}
}
void read_pdb(FILE* fp,char **atm,int *atnum,char **name,char **res,char *chain,int *resnum,double *x,double *y,double *z,double *occ,double *bfac,char **elem,double ac[2][3]) //reads file lines and stores in arrays
{
printf("\nReading pdb data\n");
int i=0,j=0;
char buff[7];
fpos_t position;
while(!feof(fp))
{
fgetpos(fp,&position);fgets(buff,sizeof buff,fp);
if((ainb("ATOM",buff)==0)||(ainb("HETATM",buff)==0))
{
fsetpos(fp,&position);printf("\ngetting position %d\n",i+1);
fscanf(fp,"%6s%5d %4s %3s %1s%4d %8lf%8lf%8lf%6lf%6lf %2s \n",atm[i],&atnum[i],name[i],res[i],&chain[i],&resnum[i],&x[i],&y[i],&z[i],&occ[i],&bfac[i],elem[i]);
printf("\nnode %d data found\n",i+1);
printf("\n%6s%5d %4s %3s %1s%4d %8.3lf%8.3lf%8.3lf%6.2lf%6.2lf %2s \n",atm[i],atnum[i],name[i],res[i],&chain[i],resnum[i],x[i],y[i],z[i],occ[i],bfac[i],elem[i]);
if(ainb("HETATM",atm[i])==0){
ac[j][0]=x[i];ac[j][1]=y[i];ac[j][2]=z[i];j++;
}
i++;
}
}
printf("\n%d Atoms read\n",i);
}
void main()
{
double ac[2][3];
int N,k;
double *x,*y,*z,*occ,*bfac;
char **atm,**name,**res,**elem,*chain;
int *atnum,*resnum;
FILE *out;
out=fopen("OUT.pdb","r");//something to check for file
N=66;
//make dynamic arrays
x=(double*)malloc(N*sizeof(double));
y=(double*)malloc(N*sizeof(double));
z=(double*)malloc(N*sizeof(double));
occ=(double*)malloc(N*sizeof(double));
bfac=(double*)malloc(N*sizeof(double));
atnum=(int*)malloc(N*sizeof(int));
resnum=(int*)malloc(N*sizeof(int));
atm=(char**)malloc(N*sizeof(char));
name=(char**)malloc(N*sizeof(char));
res=(char**)malloc(N*sizeof(char));
elem=(char**)malloc(N*sizeof(char));
chain=(char*)malloc(N*sizeof(char));
for(k=0;k<N;k++)
{
atm[k]=(char*)malloc(7*sizeof(char));
name[k]=(char*)malloc(5*sizeof(char));
res[k]=(char*)malloc(4*sizeof(char));
elem[k]=(char*)malloc(3*sizeof(char));
}
//read in data
read_pdb(out,atm,atnum,name,res,chain,resnum,x,y,z,occ,bfac,elem,ac);
fclose(out);
printf("\n-------------------------------------------\nTest Complete\n");
free(x);
free(y);
free(z);
free(occ);
free(bfac);
free(elem);
free(name);
free(atm);
free(res);
free(resnum);
free(atnum);
free(chain);
}
The output is:
Reading pdb data
getting position 1
node 1 data found
ATOM 1 CA PRO A 1 4.612 0.903 5.089 1.00 24.97 C
getting position 2
node 2 data found
ATOM 2 CA SER A 2 3.526 0.341 3.809 1.00 59.99 C
getting position 3
node 3 data found
ATOM 3 CA ARG A 3 6.208 1.550 6.551 1.00 20.40 C
getting position 4
node 4 data found
ATOM 4 CA TRP A 4 5.912 2.348 4.388 1.00 50.28 C
getting position 5
node 5 data found
ATOM 5 CA GLE A 5 4.087 4.359 6.884 1.00 54.04 C
getting position 6
node 6 data found
ATOM 6 CA THR A 6 4.405 1.292 2.566 1.00 62.06 C
getting position 7
node 7 data found
ATOM 7 CA TYR A 7 3.327 3.041 5.205 1.00 50.46 C
getting position 8
node 8 data found
ATOM 8 CA VAL A 8 5.276 0.109 0.387 1.00 58.00 C
getting position 9
node 9 data found
ATOM 9 CA LEU A 9 2.992 3.190 3.084 1.00 41.48 C
getting position 10
node 10 data found
ATOM 10 CA CYS A 10 3.565 0.287 0.721 1.00 47.65 C
getting position 11
Segmentation fault (core dumped)
Lets consider this code:
name=(char**)malloc(N*sizeof(char));
for(k=0;k<N;k++)
{
name[k]=(char*)malloc(5*sizeof(char));
}
You allocate n*sizeof(char) array and try to store N pointers to char in it. But size of pointer to the char is greater than sizeof(char), so you get buffer overflow and undefined behavior even on the initialization stage. You are lucky and your program is not crashing at this stage, but it will fail on the array usage. To prevent this error you should use sizeof(char*) in your allocation code.
Rather than hard code the type, and get it wrong for name, let the compiler figure it out. Less code, easier to read and easier to code & maintain.
//bfac=(double*)malloc(N*sizeof(double));
//resnum=(int*)malloc(N*sizeof(int));
//name=(char**)malloc(N*sizeof(char)); OP was looking for `sizeof (char*)`
bfac = malloc(N * sizeof *bfac);
resnum = malloc(N * sizeof *resnum);
name = malloc(N * sizeof *name);
Also in C, no need to cast the result of malloc().

CBLAS: Issue with *gbmv

I am trying to use the BLAS function dgbmv to multiply a vector by a band matrix. My code is in C and I use cblas to call the function.
Here is an example:
int main(int argc, char **argv)
{
/*Band representation of A = {1.0 1.0 1.0 0.0
2.0 2.0 2.0 2.0
3.0 3.0 3.0 3.0
4.0 4.0 4.0 4.0
0.0 5.0 5.0 5.0 }*/
double A[24]={0.,0.,1.,2.,
0.,1.,2.,3.,
1.,2.,3.,4.,
2.,3.,4.,5.,
3.,4.,5.,0.,
4.,5.,0.,0. };
/* Vectors x and y */
double X[4]={1,2,3,4};
double Y[10]={1,0,2,0,3,0,4,0,5,0};
double alpha=2.;
double beta=10.;
/* Use BLAS to compute y=beta*y+alpha*A*x */
cblas_dgbmv(CblasRowMajor,CblasNoTrans,5,4,3,2,alpha,A,6,X,1,beta,Y,2);
}
This code gives y={20.,0.,80.,0.,106.,0.,114.,0.,58.,0.} which is wrong.
However, if I use the transpose of A and change the first parameter of cblas_dgbmv to "CblasColMajor" I get the correct result:
int main(int argc, char **argv)
{
....
....
double B[24];
/* Set B=transpose(A) */
int l,m;
for (l=0;l<6;l++)
{
for(m=0;m<4;m++)
{
B[6*m+l]=A[l*4+m];
}
}
...
...
cblas_dgbmv(CblasColMajor,CblasNoTrans,5,4,3,2,alpha,B,6,X,1,beta,Y,2);
}
This code gives y={22.,0,60.,0.,90.,0.,120.,0.,140.,0.}, which is correct.
I think that both codes should produce the same result, since changing the matrix storage format from row-major to column-major and transposing the matrix should lead to no net effect.
I need to handle large matrices and I can not afford to transpose all of them. Moreover I would like to understand why the two examples give different results.

Recursive Block Matrix Multiplcation

Trying to implement block matrix multiplication recursively. It works fine for matrices of 2x2 but increase to sizes such as 4x4 and the answers differ vastly
Result of 3 for loops
1.53 0.89 0.53 1.33
1.75 1.09 0.72 1.17
1.78 1.43 0.57 1.69
1.73 1.04 0.62 1.51
Result of recursion
1.34 1.49 0.30 1.45
2.02 1.93 0.79 1.30
2.70 2.75 0.87 2.21
1.81 1.84 0.59 1.47
If the amount of blocks within the matrix is greater than 4 I divide blocks into four larger ones and take the square root to get the new dimension like so then make the 8 recursive calls.
void myRecMat(float** MatrixA, float** MatrixB, float** MatrixC, int srA, int scA, int srB, int scB, int srC, int scC, int blocks,int dim){
if(blocks > 4)
{ blocks=blocks/4;
int newDim = dim/2;
myRecMat(MatrixA,MatrixB,MatrixC, srA,scA,srB,scB,srC,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA,scA+newDim,srB+newDim,scB,srC,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA,scA,srB,scB+newDim,srC,scC+newDim,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA,scA+newDim,srB+newDim,scB,srC+newDim,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA+newDim,scA,srB,scB,srC+newDim,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA+newDim,scA+newDim,srB+newDim,scB,srC+newDim,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA+newDim,scA+newDim,srB,scB+newDim,srC+newDim,scC+newDim,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC, srA+newDim,scA+newDim,srB+newDim,scB+newDim,srC+newDim,scC+newDim,blocks,newDim); }
else
{
int i,j,k,endR,endC;
endR=srC+dim;
endC=scC+dim;
for(i=srC; i< endR; i++)
for(j=scC;j< endC;j++)
for(k=0; k<newDim; k++)
c[i][j] += a[i][k]*b[k][j];
}
}
The sr and sc are for starting row and col. The spacing should be right so I'm honestly out of leads here. Thanks in advanced.
I've compiled and carefully debugged your code. If you only intend to use this function on matrices of 2^k*2^k, these 2 modifications will help.
First:
for(i=srC; i< endR; i++) {
for(j=scC;j< endC;j++) {
for(k=0; k<newDim; k++)
/*c[i][j] += a[i][k]*b[k][j];*/
c[i][j] += a[i][scA+k] * b[srB+k][j];
}
}
Second:
myRecMat(MatrixA,MatrixB,MatrixC,srA,scA,srB,scB,srC,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC,srA,scA+newDim,srB+newDim,scB,srC,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC,srA,scA,srB,scB+newDim,srC,scC+newDim,blocks,newDim);
/*myRecMat(MatrixA,MatrixB,MatrixC,srA,scA+newDim,srB+newDim,scB,srC+newDim, scC,blocks,newDim);*/
myRecMat(MatrixA,MatrixB,MatrixC,srA,scA+newDim,srB+newDim,scB+newDim,srC, scC+newDim,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC,srA+newDim,scA,srB,scB,srC+newDim,scC,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC,srA+newDim,scA+newDim,srB+newDim,scB,srC+newDim,scC,blocks,newDim);
/*myRecMat(MatrixA,MatrixB,MatrixC,srA+newDim,scA+newDim,srB,scB+newDim,srC+newDim,scC+newDim,blocks,newDim);*/
myRecMat(MatrixA,MatrixB,MatrixC,srA+newDim,scA,srB,scB+newDim,srC+newDim,scC+newDim,blocks,newDim);
myRecMat(MatrixA,MatrixB,MatrixC,srA+newDim,scA+newDim,srB+newDim,scB+newDim,srC+newDim,scC+newDim,blocks,newDim);
I believe your problem here is not as much on the implementation of your method, but on the loss of precision of floating-point operations. Sometimes one may think this imprecision is neglectable, but when we do intense operations over a floating point variable, like your triple nested loop, these imprecisions become significant.
One way to go around this is to scale your floating point numbers so they "lose" their decimal part. That is, for example, if you know your matrix won't have numbers with more than two decimal digits, then multiply them all by 100 and get their integer representation. Then perform the arithmetics on integers (which are precise), and in the end get the floating point representation of the result and divide it by 100.
Hope this helps.

Resources