简体   繁体   English

为什么我的 for 循环没有循环我指定的次数?

[英]Why does my for loop not loop the number of times I specified?

I have been trying to write a program in C but I don't know if it's correct because my for loop at the start ends after only 3 loops when it should end after 10. If someone could tell me where the issue is, I would appreciate it.我一直在尝试在 C 中编写一个程序,但我不知道它是否正确,因为我for循环在开始时仅在 3 次循环后结束,而它应该在 10 次后结束。如果有人能告诉我问题出在哪里,我会欣赏它。 Here is the program.这是程序。

#include <stdio.h>
#define size 10
int main(int argc, char *argv[]){
    int array[size],sum[size],k,sum1,i,j;
    sum1=0;

    printf("give 10 integers:\n");
    for(i=0; i<size; i++){
        scanf("%d", array[i]);
        sum[i]=0;
        }
    
    for(i=1; i<size; i++)
       {
        for(j=10; j>i; j--){
            if(array[j-1]>array[j]){
                k=array[j-1];
                array[j-1]=array[j];
                array[j]=array[j-1];

            }
        }
    }
    for(i=1; i<size; i++){
            if(array[i]=array[i+1])
            sum[i]=sum[i]+1;
            }
    for(i=1; i<size; i++){
            sum1=sum1+sum[i];
            }

 printf("%d", sum1);
}

I've tried deleting the other loops but then the for loops only once.我尝试删除其他循环,但只删除一次 for 循环。

Invalid scanf 2nd argument:无效的 scanf 第二个参数:

scanf("%d", array[i]);

array[i] is an integer, scanf requires a pointer, so: array[i] 是一个 integer,scanf 需要一个指针,所以:

scanf("%d", &array[i]);
// or
scanf("%d", array + i);

Affectation in condition:做作的条件:

if (array[i] = array[i+1])

Should be应该

if (array[i] == array[i+1])

Variable k is set, but never used.设置了变量 k,但从未使用过。

Array 'sum' can be filled with 0s at declaration.数组 'sum' 可以在声明时用 0 填充。

int sum[size] = { 0 };

Iterating array from index 1, instead of 0.从索引 1 而不是 0 开始迭代数组。

Index 'j' is initialized with 10, while max index is 9.索引 'j' 初始化为 10,而最大索引为 9。

Setting a variable to itself, did you mean to swap the variables using 'k'?将变量设置为自身,您的意思是使用“k”交换变量吗?

array[j - 1] = array[j]; // both cells now have same value
array[j] = array[j - 1]; // so this means array[j] = array[j]

Improved the code a bit, but can't name other loops, I don't know what you were trying to achieve.稍微改进了代码,但无法命名其他循环,我不知道您要实现什么。 Code compiles without error, only semantic errors are left.代码编译没有错误,只留下语义错误。

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

#define NUMBERS_COUNT 10

static void promptNumbers(int numbers[], int numbersCount)
{
    int readNumbers;

    printf("Give %d integers:\n", numbersCount);

    for (readNumbers = 0; readNumbers < numbersCount; readNumbers++)
        scanf("%d", numbers + readNumbers);
}

int main(void)
{
    int numbers[NUMBERS_COUNT];
    int sum[NUMBERS_COUNT] = { 0 };
    int i, j, sum1;

    sum1 = 0;

    promptNumbers(numbers, NUMBERS_COUNT);

    for (i = 0; i < NUMBERS_COUNT; i++)
    {
        for (j = 9; j > i; j--)
        {
            if (numbers[j - 1] > numbers[j])
            {
                numbers[j - 1] = numbers[j];
                numbers[j] = numbers[j - 1];
            }
        }
    }

    for (i = 0; i < NUMBERS_COUNT - 1; i++)
    {
        if (numbers[i] == numbers[i + 1])
            sum[i] = sum[i] + 1;
    }

    for (i = 0; i < NUMBERS_COUNT; i++)
        sum1 = sum1 + sum[i];

    printf("%d", sum1);

    return EXIT_SUCCESS;
}
gcc -ansi -pedantic -Wall -Wextra -Werror temp.c -o temp
echo '1 2 3 4 5 6 7 8 9 10' | ./temp

EDIT: 2nd iteration, still unclear what you're doing with the array 'sum', counting how many duplicates you have?编辑:第二次迭代,仍然不清楚你在用数组“sum”做什么,计算你有多少重复项?

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

#define NUMBERS_COUNT 10

static void promptNumbers(int numbers[], int numbersCount)
{
    int readNumbers;

    printf("Give %d integers:\n", numbersCount);

    for (readNumbers = 0; readNumbers < numbersCount; readNumbers++)
        scanf("%d", numbers + readNumbers);
}

static void swapNumbers(int * const first, int * const second)
{
    int swapBuffer = * first;
    * first = * second;
    * second = swapBuffer;
}

static void sortNumbers(int numbers[], int numbersCount)
{
    int i, j;

    for (i = 0; i < numbersCount; i++)
    {
        for (j = 9; j > i; j--)
        {
            if (numbers[j - 1] > numbers[j])
                swapNumbers(& numbers[j], & numbers[j - 1]);
        }
    }
}

int main(void)
{
    int numbers[NUMBERS_COUNT];
    int sum[NUMBERS_COUNT] = { 0 };
    int i, sum1;

    sum1 = 0;

    promptNumbers(numbers, NUMBERS_COUNT);
    sortNumbers(numbers, NUMBERS_COUNT);

    for (i = 0; i < NUMBERS_COUNT; i++)
    {
        if (numbers[i] == numbers[i + 1])
            sum[i] = sum[i] + 1;
    }

    for (i = 0; i < NUMBERS_COUNT; i++)
        sum1 = sum1 + sum[i];

    printf("%d", sum1);

    return EXIT_SUCCESS;
}
gcc -ansi -pedantic -Wall -Wextra -Werror temp.c -o temp
echo '10 2 3 7 1 1 8 8 8 8' | ./temp
echo '10 2 3 7 4 1 9 6 5 8' | ./temp

The problem here is that your loop has to start from 0. Use:这里的问题是你的循环必须从 0 开始。使用:

for(i = 0; i < size; i++) {
    ...
}

See, the loop will begin with i = 0 and will keep running so long as i < size .看,循环将从i = 0开始,并且只要i < size就会一直运行。 When i is exactly equal to size , it will not run anymore.i正好等于size时,它将不再运行。

To get an idea of this, run the code:要了解这一点,请运行代码:

#include <stdio.h>

int main() {
    int i, size = 10;

    for(i = 0; i < size; i++) {
        printf("The value of i is: %d\n", i);
    }

    return 0;
}

This will show you that the loop never runs for i = 10 , since when i is 10 then i < size is False, so the loop condition is false.这将告诉您循环永远不会运行i = 10 ,因为当i10i < size为 False,因此循环条件为 false。

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

相关问题 为什么我的 for 循环没有为 h[i] 运行 5 次? - Why is my for loop not running 5 times for h[i]? 为什么我的“for”循环只运行了两次? - Why does my “for” loop run only two times? 为什么我扫描数字时int while循环会继续进行? - why does my int while loop keeps going when i scan for a number? 为什么我的链表程序会无限地打印出我输入的那个“第一个数字”的循环? - Why does my linked-list program print out a loop of that "first number" I entered, infinitely? 为什么此代码仅循环4次? - Why does this code only loop 4 times? For循环未执行指定的次数 - For loop not executing as many times specified 为什么这个 final.exe 文件会无限循环执行,而不是下面指定的数字? - Why does this final.exe file execute in an infinite loop instead of the specified number as below? 我的书说这个程序可以使循环无限期地运行,但是编译器只做了有限的几次 - My book says this program would have the loop run indefinitely but the compiler does it just for a limited number of times 当使用while循环遍历链接列表时,为什么我的循环迭代列表中节点数的两倍? - When using a while loop to traverse a linked list, why does my loop iterate by twice the number of nodes in the list? 为什么我的循环没有在我想要的时候终止? - Why does my loop dont terminate when I wanted to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM