Tag: 循环

C – getchar()在循环中?

我如何在循环中使用getchar()? 我现在有… for (p=0; p<n_players; p++) { … fflush(stdin); getchar(); } 但它不起作用……如果n_players为3,它只在最后执行getchar 2次… for (p=0; p<n_players; p++) { blank_start(); ascii_art_title(); printf("%s, tocca a te…\n",player_info[p].player_name); srand(time(NULL)); random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED; move_wheel_pointer(random_speed, &pointer); if (player_points(&wheel[pointer]) == 0){ player_info[p].points = wheel[pointer]; } else { player_info[p].points = 0; } printf("\nGuadagni %d punti…\n",player_info[p].points); if (p<(n_players-1)) { printf("\nOra tocca a […]

查找字符串中的子字符串数

下面的代码产生了一个荒谬的输出32674,用于’aaa’中计数’aa’的测试输入。 我该如何纠正这个? #include #include int main() { int i,j,len ,k ,count, num ; char str[100],sub[100],comp[100] ; // sub is the sub string . printf(“Please enter the string”) ; fgets(str,sizeof(str),stdin) ; printf(“Enter the substring to be searched for”) ; fgets(sub,sizeof(str),stdin) ; len=strlen(sub) ; for ( i=0 ; i < (strlen(str) – len ); i++ ) /*Goes […]

下标超出了数组中的范围

这里数组的下标超出范围。 int a[10], i; for (i = 1; i <= 10; i++) a[i] = 0; printf("India"); 输出是一个infinte循环,printf语句没有执行。 这是用KN King写的: 当我达到10时,程序将0存储到[10]中。 但是[10]并不存在。 所以0在[9]后立即进入记忆。 如果变量i碰巧跟随内存中的[9] – 可能就是这种情况 – 那么我将被重置为O.导致循环重新开始。 有谁能解释一下?

用偏置硬币模拟抛硬币的程序

我没有任何代码,主要是因为我还没有开始处理这个特定的问题。 这是我的C编程课的作业。 我的教授希望我们创建一个抛硬币(头或尾)10,000次的程序。 但是,磁头元件有55%的可能性发生。 我们必须使用具有用户提供的seed值的随机数生成器。 从概念上讲,我知道如何处理这个问题; 编码方面,我不知道。 我知道如何制作抛硬币计划,但不是偏见。 任何和所有的帮助将不胜感激。 我附上了我的硬币计划程序的代码。 我打算用这个作为这个新的偏向抛硬币计划的基础。 // CoinTosser_Homework4.cpp : Coin tossing program // nxt3 #include “stdafx.h” #include #include #define HEADS 0 #define TAILS 1 int rand_int(int a, int b) { return rand() % ((b – a + 1) + a); } int main() { int tosses = 0, min = […]

feof()和fscanf()在将字节1b扫描为char后停止工作。 是因为它在ascii中是’ESC’吗? 我能做什么?

我正在编写一个处理PPM文件的程序(P6类型,而不是P3)问题是一些图像的字节为0x1b,根据ascii表称为’ESC’ 以下几乎是我的代码: //所有包括那里,,, … int main(void) { FILE *finput; int number[7]; char r, g, b; finput = fopen(“my.ppm”, “r”); /* The code where I read the P6 numRows numColumns 255\n …Lets assume that part works well */ while(!feof(finput)) { num[0] = fscanf(finput, “%c”, &r); // the “num[] = ” part is for debugging num[1] = […]

现金更改计划使用循环和if / else

编写一个程序,允许用户输入高达$ 200.00的现金金额,然后计算并打印以下面额(20,10,5,1,.25,.10,.05,。01)的价值。 我想我已经找到了如何获得面额(除法/模数)的基础知识,但它是do / while和if / else的结构给了我麻烦。 我一直得到一个错误,我需要一个while语句,即使我已经输入它及其条件(见下文),但我也有点不知道在哪里放置范围提示(如果用户输入的东西)负数或高于200)。 任何建议/指导将不胜感激! double amt_ent; int twenty, ten, five, one, quarter, dime, nickel, penny, remainder; printf (“Enter a dollar amount up to $200.00:”); scanf (“%lf”, &amt_ent); do { printf (“Name – Assignment 2 – Change-O-Matic\n”); printf (“Amount entered: $%.2lf\n”, ((amt_ent*100)/100)); printf (“Change breakdown:\n”); { /*Change in twenties*/ twenty= (int) […]

scanf在C中导致无限循环

我对C比较陌生,但我已经编程了几年了。 我正在为一个大学课写一个程序,我很困惑为什么没有调用下面的scanf函数,导致无限循环。 我已经尝试将我的scanf放在函数外部,调用它两次,一次从内部,一次从外面,以及其他几种方式。 我在网上看到fflush可能有所帮助,但事实并非如此 有什么建议? // store starting variables int players; // print title printf(“*————————————*\n”); printf(“| |\n”); printf(“| Wheel |\n”); printf(“| of |\n”); printf(“| Fortune |\n”); printf(“| |\n”); printf(“*————————————*\n”); printf(“\n\nHow many players are there?: “); while(scanf(“%d”, &players) != 1 && players >= 0) { printf(“That isn’t a valid number of players. Try again: “); fflush(stdin); […]

int LA = {1,2,3,4,5}内存分配混乱c

我观察到为数组分配的内存似乎是动态的。 以下是我在本教程中找到的示例代码: #include main() { int LA[] = {1,3,5,7,8}; int item = 10, k = 3, n = 5; int i = 0, j = n; printf(“The original array elements are :\n”); for(i = 0; i= k){ LA[j+1] = LA[j]; j = j – 1; } LA[k] = item; printf(“The array elements after insertion :\n”); […]

如何找出哪个嵌套for循环更好?

有人问我一个问题:在以下两种情况中,哪一项是最快的: 情况1:假设int count = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { count++; } } 情况2:假设int count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { count++; } } […]

循环在第一次之后跳过scanf语句

这是main()的代码: int main (void) { float acres[20]; float bushels[20]; float cost = 0; float pricePerBushel = 0; float totalAcres = 0; char choice; int counter = 0; for(counter = 0; counter < 20; counter++) { printf("would you like to enter another farm? "); scanf("%c", &choice); if (choice == 'n') { printf("in break "); break; } […]