Tag: 循环

使用哪个无限循环

可能重复: “for(;;)”比“while(TRUE)”快吗? 如果没有,为什么人们会使用它? 我一直在阅读作者for(;;)而不是while(true)的编程书(java while(true) 。 这件事真的开始让我烦恼所以我认为我应该检查哪个是最好用的,如果有任何差异。 以下是可能的无限循环: for(;;){ } while(true){ } do{ }while(true) 哪个更喜欢使用? 在表现中是否有任何差异(比如你必须测试在情况下是否为真)或者它们是否编译成同一个东西?

如何使用嵌套for循环添加两个2d(音调)数组?

我是cuda的新手。 我想将两个2d数组加到第三个数组中。 我使用以下代码: cudaMallocPitch((void**)&device_a, &pitch, 2*sizeof(int),2); cudaMallocPitch((void**)&device_b, &pitch, 2*sizeof(int),2); cudaMallocPitch((void**)&device_c, &pitch, 2*sizeof(int),2); 现在我的问题是,我不想在我的内核代码中使用这些数组作为扁平的二维数组我希望di使用两个for循环并将结果放在第三个数组中 __global__ void add(int *dev_a ,int *dev_b,int* dec_c) { for i=0;i<2;i++) { for j=0;j<2;j++) { dev_c[i][j]=dev_a[i][j]+dev_b[i][j]; } } } 我怎么能在CUDA做到这一点? 请告诉我如何以这种方式使用二维arrays? 使用2d-array的内核调用应该是什么? 如果可能,请使用代码示例进行说明。

在C上使用OpenMP并行一段时间

我想在一段时间内做一个平行,像这样的somothing: while(!End){ for(…;…;…) // the parallel for … // serial code } for循环是while循环的唯一并行部分。 如果我这样做,我会有很多开销: cycles = 0; while(!End){ // 1k Million iterations aprox #pragma omp parallel for for(i=0;i<N;i++) // the parallel for with 256 iteration aprox if(time[i] == cycles){ if (wbusy[i]){ wbusy[i] = 0; wfinished[i] = 1; } } // serial code ++cycles; } for循环的每次迭代都是彼此独立的。 […]

如何使用Escape键在C中结束循环

我只想在C中结束do while循环,如下所示: #include #include main() { char exit; do{ printf(“PLEASE INSERT OPTION:”); exit = getchar(); }while(exit != ‘\027’); }//main 我认为这有点像这样。

数组中的唯一数字

与我之前的问题类似,这次我试图打印不会出现两次或两次以上的数字。 这是我的代码: #include int main() { int i; int a[10]={2,2,2,4,6,6,9,10,10,11}; for(i=0; i 0 && a[i] != a[i-1]) printf(“%d “,a[i]); } if(i==9 && a[i]!=a[i-1]) printf(“%d”, a[i]); } printf(“\n”); return 0; } 输出结果是正确的,即4,9,11,但我需要分别考虑i的最终值。 有没有更好的办法?

C getchar()不等待输入/条件循环不int

我正在尝试向我的C控制台应用程序计算器添加一个function,提示用户决定是否要使用: y或n执行另一个计算,但在测试中, getchar()拒绝等待输入并且程序继续进行,就像它收到了有效的输入。 以下是该function的最小示例: main() { char newCalculation; do{ lengthFormula(); /* main calculation formula */ printf(“Would you like to do another calculation? (Y/N)”); newCalculation = getchar(); }while(tolower( newCalculation ) == ‘y’); if(tolower(newCalculation) == ‘n’){ exitProgram(); /* exit the program */ } while(tolower(newCalculation) != ‘n’ && tolower(newCalculation) != ‘y’){ printf(“This is not a valid response.\n Please […]

运行时检查失败变量周围的堆栈已损坏

#include main() { int num[9], i = 0, count = 0; while (i<10) { scanf("%d", &num[i]); if (num[i] % 2 == 0) { count++; } i++; } printf("we have %d double numbers\n", count); } 运行时检查失败#2 – 变量周围的堆栈已损坏 我该怎么办?

scanf()调用之后的指令被调用两次

以下程序: #include #include char input; void * dpy(void *args) { printf(“\n[input] = %c\n”, input); } void * read(void *args) { pthread_t child; printf(“Write whatever – press ‘F’ to end\n”); do { scanf(“%c”, &input); printf(“begin\n”); pthread_create(&child, NULL, dpy, NULL); pthread_join(child, NULL); printf(“end\n”); } while (input!=’F’); printf(“done\n”); } void main () { pthread_t parent; pthread_create(&parent, NULL, read, […]

反转用户输入的数字

我试图反转用户输入的数字(即用户输入的数字存储在数组中,只要他输入正数)。 但是,当我输入123 4569 752 896 -1时,输出为321 9653 257 698正如您所看到的,第二个数字不是9654.我无法修复它。 #include #include // finding the number of digits int bsm(int a){ int i=0; while(a!=0){ i++; a=a/10; } return i; } // reversing the number int rev(int m,int a){ int s=0,sum=0; while(a!=0){ s=a%10; sum+=s*pow(10,m)/10; m–; a=a/10; } return sum; } int main() { int i=0,k,a[10],p,r; scanf(“%d”,&a[i]); while(a[i]>0){ […]

如何从头开始重复ac程序并清理屏幕和第一个输入值?

我是编程新手。 我写了一个简单的程序。 我想一次又一次地重复该程序,它只能在用户想要退出时退出。 这是我的计划 #include #include main() { char ch; int num1, num2, a, m, s, choice; float d; printf(“\nEnter The First Number: “); scanf(“%d”, &num1); printf(“\nEnter The Second Number: “); scanf(“%d”, &num2); a=num1+num2; m=num1*num2; s=num1-num2; d=(float)(num1/num2); printf(“\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type […]