Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么每当我改变变量初始化的位置时,这个c程序的输出会给出不同的结果?_Arrays_C - Fatal编程技术网

Arrays 为什么每当我改变变量初始化的位置时,这个c程序的输出会给出不同的结果?

Arrays 为什么每当我改变变量初始化的位置时,这个c程序的输出会给出不同的结果?,arrays,c,Arrays,C,看看这个程序,当我在程序开始时初始化count变量时,它给出了不正确的结果,但在第一个循环中声明时给出了正确的结果。(如图所示),为什么会发生这种情况 此源代码主要用于计算数组中元素的频率 #include <stdio.h> int main() { int arr[100], freq[100]; int size, i, j, count; /* Input size of array */ printf("Enter si

看看这个程序,当我在程序开始时初始化
count
变量时,它给出了不正确的结果,但在第一个循环中声明时给出了正确的结果。(如图所示),为什么会发生这种情况

此源代码主要用于计算数组中元素的频率

#include <stdio.h>

int main()
{
    int arr[100], freq[100];
    int size, i, j, count;
    
    /* Input size of array */
    printf("Enter size of array: ");
    scanf("%d", &size);
    
    /* Input elements in array */
    printf("Enter elements in array: ");
    for (i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
        /* Initially initialize frequencies to -1 */
        freq[i] = -1;
    }
    
    for (i = 0; i < size; i++) {
        count = 1;
        for (j = i + 1; j < size; j++) {
            /* If duplicate element is found */
            if (arr[i] == arr[j]) {
                count++;
                /* Make sure not to count frequency of same element again */
                freq[j] = 0;
            }
        }
    
        /* If frequency of current element is not counted */
        if (freq[i] != 0) {
            freq[i] = count;
        }
    }
    
    /* Print frequency of each element */
    printf("\nFrequency of all elements of array : \n");
    for (i = 0; i < size; i++) {
        if (freq[i] != 0) {
            printf("%d occurs %d times\n", arr[i], freq[i]);
        }
    }
    
    return 0;
}
#包括
int main()
{
int-arr[100],freq[100];
整数大小,i,j,计数;
/*数组的输入大小*/
printf(“输入数组的大小:”);
scanf(“%d”,大小(&S);
/*数组中的输入元素*/
printf(“在数组中输入元素:”);
对于(i=0;i
案例1:当我在开始时将count的值初始化为

  #include <stdio.h>

 int main()
 {
 int arr[100], freq[100];
 int size, i, j, count;
 count=1; //***LOOK HERE I INITIALIZE ITS VALUE***

 /* Input size of array */
#包括
int main()
{
int-arr[100],freq[100];
整数大小,i,j,计数;
count=1;//***看这里,我初始化了它的值***
/*数组的输入大小*/
。 .

案例2:当我在第一个for循环中初始化count的值时

   .
   .   
    for (i = 0; i < size; i++) {
    count = 1; //***HERE I INITIALIZE ITS VALUE INSIDE FIRST FOR LOOP***

    for (j = i + 1; j < size; j++) {
        /* If duplicate element is found */
        if (arr[i] == arr[j]) {

 .
 .
。
.   
对于(i=0;i

因为您应该分别计算每个值。如果在for循环外部初始化,则会累加不同值的总和

假设您有一个数组:
{1,3,5,6,1,8,9,1,3,2,1}

开始循环,计算
1的
,在第一轮循环后,变量中有
4

然后在第二轮中,您将要计算
3的频率
,开始时
计数器设置为
4
,而不是像它应该的那样
0

>当我在程序开始时初始化count变量时,它给出了不正确的结果

因为对于数组中的每个元素,您需要将
计数
重置为
1
,否则
计数
将具有上一个元素计数值(数组中上一个元素的出现次数),而当前元素计数值将从该值开始,这将导致当前元素的计数值错误

>但在第一个循环内声明时给出正确的值(如图所示)

您应该在代码中进行一次优化-不要再次处理重复的元素。检查该元素的
freq
,如果它不是
-1
(即已处理),则不要处理它:

for (i = 0; i < size; i++)
{
    int count = 1;
    for (j = i + 1; (j < size) && (freq[i] == -1); j++)  // added freq[i] check
    {
        /* If duplicate element is found */
        if (arr[i] == arr[j])
        {
            count++;

            /* Make sure not to count frequency of same element again */
            freq[j] = 0;
        }
    }

    /* If frequency of current element is not counted */
    if (freq[i] == -1)
    {
        freq[i] = count;
    }
}
for(i=0;i
请显示不起作用的版本。另外,请给出准确的输入、预期结果和实际结果。我没有看到在循环中声明的
count
变量
for(j=I+1;j
,如果
I==size-1
for(I=0;Ifor (i = 0; i < size; i++) { int count = 1; // initialising the count with value 1
for (i = 0; i < size; i++)
{
    int count = 1;
    for (j = i + 1; (j < size) && (freq[i] == -1); j++)  // added freq[i] check
    {
        /* If duplicate element is found */
        if (arr[i] == arr[j])
        {
            count++;

            /* Make sure not to count frequency of same element again */
            freq[j] = 0;
        }
    }

    /* If frequency of current element is not counted */
    if (freq[i] == -1)
    {
        freq[i] = count;
    }
}