0

How do I convert tabs to spaces?

I posted code on Stack Overflow, but it seems this wasn't done correctly (I have no experience in coding so at the moment; I'm trying to improve it).

I got the command to convert my tabs to spaces. However, if I did just that using the Ctrl + H function in word there weren't any spaces.

How can I do this?

void mrqmin(float x[], float y[], float sig[], int ndata, float a[], int ia[],
int ma, float **covar, float **alpha, float *chisq,
void (*funcs)(float, float [], float *, float [], int), float *alamda)   
{
void covsrt(float **covar, int ma, int ia[], int mfit);
void gaussj(float **a, int n, float **b, int m);
void mrqcof(float x[], float y[], float sig[], int ndata, float a[],
int ia[], int ma, float **alpha, float beta[], float *chisq,
void (*funcs)(float, float [], float *, float [], int));
int j,k,l;
static int mfit;
static float ochisq,*atry,*beta,*da,**oneda;
if (*alamda < 0.0) { //Initialization.
atry=vector(1,ma);
beta=vector(1,ma);
da=vector(1,ma);
for (mfit=0,j=1;j<=ma;j++)
if (ia[j]) mfit++;
oneda=matrix(1,mfit,1,1);
*alamda=0.001;
mrqcof(x,y,sig,ndata,a,ia,ma,alpha,beta,chisq,funcs);
ochisq=(*chisq);
for (j=1;j<=ma;j++) atry[j]=a[j];
}
for (j=1;j<=mfit;j++) { //Alter linearized fitting matrix, by augmenting diagonal elements.
for (k=1;k<=mfit;k++) covar[j][k]=alpha[j][k]; 
covar[j][j]=alpha[j][j]*(1.0+(*alamda));
oneda[j][1]=beta[j];
}
gaussj(covar,mfit,oneda,1); //Matrixsolution.
for (j=1;j<=mfit;j++) da[j]=oneda[j][1];
if (*alamda == 0.0) { //Once converged, evaluate covariance matrix.
covsrt(covar,ma,ia,mfit);
covsrt(alpha,ma,ia,mfit); //Spread out alpha to its full size too.
free_matrix(oneda,1,mfit,1,1);
free_vector(da,1,ma);
free_vector(beta,1,ma);
free_vector(atry,1,ma);
return;
}
for (j=0,l=1;l<=ma;l++) //Did the trial succeed?
if (ia[l]) atry[l]=a[l]+da[++j];
mrqcof(x,y,sig,ndata,atry,ia,ma,covar,da,chisq,funcs);
if (*chisq < ochisq) { //Success, accept the new solution.
*alamda *= 0.1;
ochisq=(*chisq);
for (j=1;j<=mfit;j++) {
for (k=1;k<=mfit;k++) alpha[j][k]=covar[j][k];
beta[j]=da[j];
}
for (l=1;l<=ma;l++) a[l]=atry[l];
} else { //Failure, increase alamda and return.
*alamda *= 10.0;
*chisq=ochisq;
}
}

{
int i,j,k,l,m,mfit=0;
float ymod,wt,sig2i,dy,*dyda;
dyda=vector(1,ma);
for (j=1;j<=ma;j++)
if (ia[j]) mfit++;
for (j=1;j<=mfit;j++) { //Initialize (symmetric) alpha, beta.
for (k=1;k<=j;k++) alpha[j][k]=0.0;
beta[j]=0.0;
}
*chisq=0.0;
for (i=1;i<=ndata;i++) { //Summation loop over all data.
(*funcs)(x[i],a,&ymod,dyda,ma);
sig2i=1.0/(sig[i]*sig[i]);
dy=y[i]-ymod;
for (j=0,l=1;l<=ma;l++) {
if (ia[l]) {
wt=dyda[l]*sig2i;
for (j++,k=0,m=1;m<=l;m++)
if (ia[m]) alpha[j][++k] += wt*dyda[m];
beta[j] += dy*wt;
}
}
*chisq += dy*dy*sig2i;// And find χ2    .
}
for (j=2;j<=mfit;j++)// Fill in the symmetric side.
for (k=1;k<j;k++) alpha[k][j]=alpha[j][k];
free_vector(dyda,1,ma);
}
4
  • 11
    "in word" - what? Don't use Word for code; it'll screw up everything. Aug 22, 2013 at 22:24
  • 2
    If you are talking about this question, there does not appear to be any tabs in your code. To answer your question though, you might have better luck using a code editor such as Notepad++, rather than using MS Word.
    – chue x
    Aug 22, 2013 at 22:28
  • 6
    The simple answer is to code with spaces.
    – Cole Tobin
    Aug 22, 2013 at 22:28
  • 2
    Another simple answer is: tabs are fine too. They will be converted to spaces (based on a tab width of 4) in the preview and after posting. (Unfortunately, your browser will probably show them in the edit window with a tab width of 8, but that could be changed using CSS.)
    – Arjan
    Aug 22, 2013 at 22:43

1 Answer 1

1

I see several solutions, from optimal to less:

  1. Use a proper IDE for the language you're writing. Eclipse is a good generic one, Microsoft's Visual Studio is good for Windows applications. Those can do the intenting for you, and you can change the settings between TAB characters and spaces.
  2. Use an online tool to auto-indent your code. A quick google search can help you. I found this one in about 10 seconds.
  3. Leave it with TAB characters. The site will automatically convert the TAB characters into spaces when showing your code. So no need to worry there.
5
  • Can't add a thing!
    – tjons
    Aug 23, 2013 at 10:58
  • @TJSDesigned: What do you mean? Aug 23, 2013 at 11:03
  • Your answer is perfect! I would have said the same thing about using an IDE.
    – tjons
    Aug 23, 2013 at 11:04
  • "The site will automatically convert the TAB characters into spaces when pasted as code." — more precisely: when showing your code.
    – Arjan
    Aug 23, 2013 at 11:09
  • @Arjan: Yes, thank you. Feel free to edit next time for trivial changes like these :P Aug 23, 2013 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .