EDN Admin
Well-known member
hi,all, I need to repeat a block of c code for a specified number of time in my vc++ project, but something went wrong, the code is as follows:for(int i = 0; i < nNumOfReps; i++)
{
if(nNumOfReps >= 2 && i <= (nNumOfReps - 2))
{
correct = 0;
total = 0;
}
if(m_predict_probability)
{
if (svm_type==NU_SVR || svm_type==EPSILON_SVR){
CString strMessage;
strMessage.Format( _T("Prob. model for test data: target value = predicted value + z,rnz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%grn",svm_get_svr_probability(model)));
AfxMessageBox(strMessage);
}else{
int *labels=(int *) malloc(nr_class*sizeof(int));
svm_get_labels(model,labels);
prob_estimates = (double *) malloc(nr_class*sizeof(double));
fprintf(output,"labels");
for(j=0;j<nr_class;j++)
fprintf(output," %d",labels[j]);
fprintf(output,"n");
free(labels);
}
}
max_line_len = 1024;
line = (char *)malloc(max_line_len*sizeof(char));
x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
while(readline(input, max_line_len, line) != NULL)
{
int i = 0;
double target_label, predict_label;
char *idx, *val, *label, *endptr;
int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
char *next_token = NULL;
label = strtok_s(line, " tn", &next_token);
if(label == NULL) // empty line
exit_input_error(total+1);
target_label = strtod(label,&endptr);
if(endptr == label || *endptr != )
exit_input_error(total+1);
while(1)
{
if(i>=max_nr_attr-1) // need one more for index = -1
{
max_nr_attr *= 2;
x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
}
idx = strtok_s(NULL, ":", &next_token);
val = strtok_s(NULL, " t", &next_token);
if(val == NULL)
break;
errno = 0;
x.index = (int)strtol(idx,&endptr,10);
if(endptr == idx || errno != 0 || *endptr != || x.index <= inst_max_index)
exit_input_error(total+1);
else
inst_max_index = x.index;
errno = 0;
x.value = strtod(val,&endptr);
if(endptr == val || errno != 0 || (*endptr != && !isspace(*endptr)))
exit_input_error(total+1);
++i;
}
x.index = -1;
if (m_predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
{
predict_label = svm_predict_probability(model,x,prob_estimates);
fprintf(output,"%g",predict_label);
for(j=0;j<nr_class;j++)
fprintf(output," %g",prob_estimates[j]);
fprintf(output,"n");
}else{
predict_label = svm_predict(model,x);
fprintf(output,"%gn",predict_label);
}
if(predict_label == target_label)
++correct;
++total;
}
}
The nNumOfReps indicates the number of repetitions the for loop runs; For example, if I want the for loop runs 3 times, and for the first time and second time, I want correct and total return to 0, and the last(third) time, I want correct and total retain their values. The problem is if nNumOfReps is <= 2, the code works fine (correct and total can keep the values they count after the for loops twice), but if it is > 2, correct and total are 0 after the for loop.
The function readline is as follows:char* CSVMPredictDlg::readline(FILE * input, int max_line_len, char *line)
{
int len;
if(fgets(line,max_line_len,input) == NULL)
return NULL;
while(strrchr(line,n) == NULL)
{
max_line_len *= 2;
line = (char *) realloc(line,max_line_len);
len = (int) strlen(line);
if(fgets(line+len,max_line_len-len,input) == NULL)
break;
}
return line;
}
a sample input file is like:3 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:1 23:1 24:0 25:1 26:0 27:0 28:0 29:0 30:1 31:0 32:0 33:0 34:0 35:0 36:0 37:0 38:0 39:1 40:0 41:1 42:0 43:0 44:0 45:0 46:0 47:0 48:1 49:0 50:0 51:0
7 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:1 23:0 24:0 25:1 26:0 27:0 28:0 29:0 30:1 31:0 32:0 33:0 34:0 35:0 36:0 37:0 38:0 39:1 40:0 41:1 42:0 43:0 44:0 45:0 46:0 47:0 48:0 49:0 50:0 51:1
the first number indicates the target_label.
View the full article
{
if(nNumOfReps >= 2 && i <= (nNumOfReps - 2))
{
correct = 0;
total = 0;
}
if(m_predict_probability)
{
if (svm_type==NU_SVR || svm_type==EPSILON_SVR){
CString strMessage;
strMessage.Format( _T("Prob. model for test data: target value = predicted value + z,rnz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%grn",svm_get_svr_probability(model)));
AfxMessageBox(strMessage);
}else{
int *labels=(int *) malloc(nr_class*sizeof(int));
svm_get_labels(model,labels);
prob_estimates = (double *) malloc(nr_class*sizeof(double));
fprintf(output,"labels");
for(j=0;j<nr_class;j++)
fprintf(output," %d",labels[j]);
fprintf(output,"n");
free(labels);
}
}
max_line_len = 1024;
line = (char *)malloc(max_line_len*sizeof(char));
x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
while(readline(input, max_line_len, line) != NULL)
{
int i = 0;
double target_label, predict_label;
char *idx, *val, *label, *endptr;
int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
char *next_token = NULL;
label = strtok_s(line, " tn", &next_token);
if(label == NULL) // empty line
exit_input_error(total+1);
target_label = strtod(label,&endptr);
if(endptr == label || *endptr != )
exit_input_error(total+1);
while(1)
{
if(i>=max_nr_attr-1) // need one more for index = -1
{
max_nr_attr *= 2;
x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
}
idx = strtok_s(NULL, ":", &next_token);
val = strtok_s(NULL, " t", &next_token);
if(val == NULL)
break;
errno = 0;
x.index = (int)strtol(idx,&endptr,10);
if(endptr == idx || errno != 0 || *endptr != || x.index <= inst_max_index)
exit_input_error(total+1);
else
inst_max_index = x.index;
errno = 0;
x.value = strtod(val,&endptr);
if(endptr == val || errno != 0 || (*endptr != && !isspace(*endptr)))
exit_input_error(total+1);
++i;
}
x.index = -1;
if (m_predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
{
predict_label = svm_predict_probability(model,x,prob_estimates);
fprintf(output,"%g",predict_label);
for(j=0;j<nr_class;j++)
fprintf(output," %g",prob_estimates[j]);
fprintf(output,"n");
}else{
predict_label = svm_predict(model,x);
fprintf(output,"%gn",predict_label);
}
if(predict_label == target_label)
++correct;
++total;
}
}
The nNumOfReps indicates the number of repetitions the for loop runs; For example, if I want the for loop runs 3 times, and for the first time and second time, I want correct and total return to 0, and the last(third) time, I want correct and total retain their values. The problem is if nNumOfReps is <= 2, the code works fine (correct and total can keep the values they count after the for loops twice), but if it is > 2, correct and total are 0 after the for loop.
The function readline is as follows:char* CSVMPredictDlg::readline(FILE * input, int max_line_len, char *line)
{
int len;
if(fgets(line,max_line_len,input) == NULL)
return NULL;
while(strrchr(line,n) == NULL)
{
max_line_len *= 2;
line = (char *) realloc(line,max_line_len);
len = (int) strlen(line);
if(fgets(line+len,max_line_len-len,input) == NULL)
break;
}
return line;
}
a sample input file is like:3 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:1 23:1 24:0 25:1 26:0 27:0 28:0 29:0 30:1 31:0 32:0 33:0 34:0 35:0 36:0 37:0 38:0 39:1 40:0 41:1 42:0 43:0 44:0 45:0 46:0 47:0 48:1 49:0 50:0 51:0
7 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:1 23:0 24:0 25:1 26:0 27:0 28:0 29:0 30:1 31:0 32:0 33:0 34:0 35:0 36:0 37:0 38:0 39:1 40:0 41:1 42:0 43:0 44:0 45:0 46:0 47:0 48:0 49:0 50:0 51:1
the first number indicates the target_label.
View the full article