简体   繁体   English

sqrt() 函数中 c 中的 domain_error

[英]domain_error in c in sqrt() function

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

typedef struct freqm {
  int dfreq[100];
  char word[10];
} freqm;

freqm fm[10];
int mat[10][10];
float sim[10][10];
int dsize[20];

int search(freqm fm[10], int t_cnt, char term[10]);
void print_cnt(freqm fm[10], int d_cnt, int t_cnt);
int read_files(int d_cnt);
void build_sim(int d_cnt, int t_cnt);
void print(int d_cnt);

void main() {
  int d_cnt, c_cnt, t_cnt;
  clrscr();
  printf("\n\tenter no of documents");
  scanf("%d", &d_cnt);
  t_cnt = read_files(d_cnt);
  print_cnt(fm, d_cnt, t_cnt);
  build_sim(d_cnt, t_cnt);
  print(d_cnt);
  getch();
}

int read_files(int d_cnt) {
  int flag = 0, i, j, t_cnt = 0, count;
  char str[10], str1[10], input[10], term[10];
  FILE *fp;
  printf("\nenter ip prefix");
  scanf("%s", input);
  for (i = 1; i <= d_cnt; i++) {
    strcpy(str1, input);
    sprintf(str, "%d", i);
    strcat(str1, str);
    strcat(str1, ".txt");
    fp = fopen(str1, "r");
    while (fscanf(fp, "%s %d", term, &count) != EOF) {
      flag = 0;
      //fflush(stdin);

      dsize[i] = dsize[i] + count;
      if (i > 1)
        flag = search(fm, t_cnt, term);
      if (i == 1 || flag == -1) {
        fm[t_cnt].dfreq[i] = count;
        strcpy(fm[t_cnt].word, term);
        t_cnt++;
      } else
        fm[flag].dfreq[i] = count;
    }
  }
  return t_cnt;
}

void print_cnt(freqm fm[10], int d_cnt, int t_cnt) {
  int i, j;
  for (i = 0; i < t_cnt; i++) {
    printf("\n%s", fm[i].word);
    for (j = 1; j <= d_cnt; j++)
      printf("\t%d", fm[i].dfreq[j]);
  }
}

int search(freqm fm[10], int t_cnt, char term[10]) {
  int i;
  for (i = 0; i < t_cnt; i++)
    if (strcmp(fm[i].word, term) == 0)
      return i;
  return -1;
}

void build_sim(int d_cnt, int t_cnt) {
  int i, j, sum = 0, k;
  float res = 0, res1, temp, temp1;
  for (i = 1; i <= d_cnt; i++) {
    temp = dsize[i];
    for (j = i + 1; j <= d_cnt; j++) {
      sum = 0;
      temp1 = dsize[j];
      for (k = 0; k < t_cnt; k++)
        sum = sum + min(fm[k].dfreq[i], fm[k].dfreq[j]);
      printf("sum=== %d", sum);
      printf("t1 %d %d", temp, temp1);
      //res1=(float)(sqrt(temp)*sqrt(temp1));
      printf("\n%f", res1);
      res = sum / sqrt(temp1) * sqrt(temp);
      sim[i][j] = res;
    }
  }
}

void print(int d_cnt) {
  int i, j;
  for (i = 1; i <= d_cnt; i++) {
    printf("\n");
    for (j = 1; j <= d_cnt; j++)
      printf("\t%f", sim[i][j]);
  }
}



//input1.txt
T1 0
T2 3
T3 5
T4 1
//input2.txt
T1 5
T2 0
T3 0
T4 1

I was writing code for single pass clustering algorithm.我正在为单通道聚类算法编写代码。 But in function build_sim I get domain_error even though values of temp and temp1 are positive ie 9 and 6. How to resolve this problem?但是在函数build_sim即使temptemp1值为正值,即 9 和 6,我也会得到build_sim 。如何解决这个问题?

I'm using turboc.我正在使用涡轮增压。 Is it the problem related to turbo c?是不是turbo c的问题?

Major issue:主要问题:

1) Wrong format specifiers 1) 错误的格式说明符

float res = 0, res1, temp, temp1;
...
// printf("t1 %d %d", temp, temp1);
printf("t1 %f %f", temp, temp1);

2) Missing #include <math.h> for sqrt() . 2) 缺少#include <math.h>用于sqrt() This likely explains the problem as without a prototype, sqrt assumed to return int .这可能解释了这个问题,因为没有原型, sqrt假设返回int This results in undefined behavior.这会导致未定义的行为。

Number of minor problems:小问题数:

3) poor main() declaration 3) 糟糕的main()声明

// void main()
int main(void)

4) c_cnt never used. 4) c_cnt从未使用过。 j never used in read_files() j从未在read_files()

5) Also not certain if your include files contain min() . 5)也不确定您的包含文件是否包含min() Double check that.仔细检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM