我有一个我写的程序,列出了100,000个素数.它适用于10个数字,但在这么多数字之后它们变成负值.我将int更改为long int并且没有改变任何内容,然后我将它们更改为double,我得到标题中列出的错误.我的变量应该是什么?请记住,我仍然是编程的新手.我还看了一些以前的帖子,但没有看到答案.
int is_prime(double x,char array[]){
//doesnt use array but I put it in there
double j=2;//divider
for(j=2;j<=pow(x,0.5);j++){
if((x%j==0)){
return(0);
} //isnt prime
}
return(1);// because it is prime.
}
Run Code Online (Sandbox Code Playgroud) 该代码应该跳过空格并一次返回一个单词.关于这段代码的几个问题:当代码到达*word ++ = c时; 我得到一个核心转储.我是否正确写过这一行?并返回正确.我需要以某种方式分配内存来存储这个词吗?
//get_word
int get_word(char *word,int lim){
int i=0;
int c;
int quotes=0;
int inword = 1;
while(
inword &&
(i < (lim-1)) &&
((c=getchar()) != EOF)
){
if(c==('\"')){//this is so i can get a "string"
if (quotes) {
inword = 0;
}
quotes = ! quotes;
}
else if(quotes){ //if in a string keep storing til the end of the string
*word++=c;//pointer word gets c and increments the pointer
i++;
}
else if(!isspace(c)) {//if not in …
Run Code Online (Sandbox Code Playgroud) c中是否有一个函数可以让我查看数组中的下一个char?另外我在哪里可以找到这些信息,我尝试了谷歌并在这个网站上寻找现有的线程.
我试图从一行中提取数字,并存储这些数字.所以我想做点什么
if(c =数字和c"下一个字符"不是数字){value is = value*10 + c-'0',商店号码}
我的代码有问题,好消息是我确实查明了问题,坏消息是我不明白为什么这是一个问题.这应该是返回还是退出?这是我的getNums()函数,...到目前为止.首先我的代码调用getLine()获取行并返回其字符长度.然后给出nums,行,行的长度,以及将数字放入的空数组.并且假设返回它刚刚放入的数字的数量.
int getNums(char s[], int endMarker, int numarray[])
{
char c;
double value;
int counter =0;
int i,j;
for(i=0;i<endMarker;i++) {
while ((c=s[i]!='\n')&&(c!=' ')) {
//errors
if ( (c<'0') || (c>'9') ) {
return(-1); //was exit testing return, **this always kicks me out**
if( counter > 6){
return(-2);
} //was exit testing return
s[i]=c;
i++;
value = value*10+'c'-'0';
}else
numarray[j]=value;
j++;
counter++;
}
if ((c=getchar())==' ') {
i++;
}
}
return (counter);
printf("%c,%c:",counter,value); // for testing
}
Run Code Online (Sandbox Code Playgroud) 当我使用调试器时,我可以告诉退出没有退出该功能.我使用退出功能错了吗?(我必须)我该如何解决这个问题?
int is_prime(int x,char array[]){
int divider = (x-1);
float test;
while(x>-1){
test = isdigit((x % divider)); //isdigit returns !0 if digit
if(divider == '1'){
return(1); //if divider reaches 1 then the number is prime
exit;
}
if(test == '0'){
return (0);//not prime
exit;
}
divider--;
}
Run Code Online (Sandbox Code Playgroud)
}
我昨天工作了大约5个小时,并使用此站点的帮助获得了代码,但我认为我这样做的方式是骗子,我使用了scanf命令.无论如何,我想以正确的方式解决这个问题.多谢你们!哦,代码编译但是吐出的平均值是错误的.我想在概念上理解我做错了什么以及完成作业.
#include <stdio.h>
#include <stdlib.h>
double get_number(int num);
main () {
double n1,n2,n3;
double average;
printf("\nCompute the average of 3 integers\n");
printf("--------------------------------\n");
n1 = get_number(1);
n2 = get_number(2);
n3 = get_number(3);
average = (n1 + n2 + n3)/3;
printf("The average is %0.2f\n",average);
}
double get_number(int num) {
double value = 0;
int c;
printf("Please input number %i: ", num);
while ((c = getchar()) != '\n') {
if ( (c<'0') || (c>'9') ) {
printf("Incorrect character entered as a number - …
Run Code Online (Sandbox Code Playgroud)