Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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
打印数组中的元素,直到C编程中指定的键为止_C_Arrays - Fatal编程技术网

打印数组中的元素,直到C编程中指定的键为止

打印数组中的元素,直到C编程中指定的键为止,c,arrays,C,Arrays,我一直在努力提高我的编码技能,我已经在这里开始了我的旅程。因为我被困在一个地方,试图找出我的错误所在。代码如下所示: #include<stdio.h> int main(){ int a[10],i,j,arr_size; printf("Enter the size of the array"); scanf("%d",&arr_size); printf("Enter the array:"); for(i=0;i<arr_siz

我一直在努力提高我的编码技能,我已经在这里开始了我的旅程。因为我被困在一个地方,试图找出我的错误所在。代码如下所示:

#include<stdio.h>
int main(){
   int a[10],i,j,arr_size;

   printf("Enter the size of the array");
   scanf("%d",&arr_size);

   printf("Enter the array:");
   for(i=0;i<arr_size;i++)
   scanf("%d",&a[i]);

   //here key is 42
   //so we'll find the key and print the elements up to that
   for(j=0;j<arr_size;j++){
      if(j==42)
        break;

      //loop for the array up to the key
      for(i=0;i<j;i++)
      printf(" %d",a[i]);  
   }

   return 0; 
}
#包括
int main(){
int a[10],i,j,arr_尺寸;
printf(“输入数组的大小”);
scanf(“%d”和arr_大小);
printf(“输入数组:”);
对于(i=0;i

输出显示循环上升到键,即42,但以不同的方式打印1 2 42。现在这很奇怪

所需输出的格式必须为:1 2仅当输入为1 2 42 33

时,如果不检查
42
的数组值,则检查索引

循环打印现在打印的内容的原因是嵌套循环。可以多次打印(部分)数组

代替嵌套循环,您可以做一些简单的事情,如

for (int j = 0; j < arr_size && a[j] != 42; ++j)
{
    printf(" %d", a[j]);
}
for(int j=0;j

我还建议您添加一项检查,以确保
arr\u size
不超出阵列的范围。

括号放错了位置。这就是您要查找的内容:

#include<stdio.h>
int main(){
   int a[10],i,j,arr_size;

   printf("Enter the size of the array");
   scanf("%d",&arr_size);

   printf("Enter the array:");
   for(i=0;i<arr_size;i++)
   scanf("%d",&a[i]);

   //here key is 42
   //so we'll find the key and print the elements up to that
   for(j=0;j<arr_size;j++){
        if(a[j]==42) 
          break;
    }
   //loop for the array up to the key
   for(i=0;i<j;i++)
    printf(" %d",a[i]);  


   return 0; 
}
#包括
int main(){
int a[10],i,j,arr_尺寸;
printf(“输入数组的大小”);
scanf(“%d”和arr_大小);
printf(“输入数组:”);
对于(i=0;i试试这个.)

#include<stdio.h>
int main(){
   int a[10],i,j,arr_size;

   printf("Enter the size of the array");
   scanf("%d",&arr_size);

   printf("Enter the array:");
   for(i=0;i<arr_size;i++)
   scanf("%d",&a[i]);

   //here key is 42
   //so we'll find the key and print the elements up to that
   for(j=0;j<arr_size;j++){
        if(a[j]==42) 
          break;
    }
   //loop for the array up to the key
   for(i=0;i<j;i++)
    printf(" %d",a[i]);  


   return 0; 
}
#包括
int main(){
int a[10],i,j,arr_尺寸;
printf(“输入数组的大小”);
scanf(“%d”和arr_大小);
printf(“输入数组:”);

对于(i=0;iYou似乎是在用42代替值来测试数组索引。学习如何使用调试器。然后你可以逐行检查代码,看看可能是什么错误。感谢@doynax,因为我在代码中做了编辑,并用if(a[j]==42)代替if(j==42),然后中断;对于(i=0;i@Ajay是吗?@一些程序员,你的代码非常高效,但是我想问一个简单的问题,尽管我对它了解不多。因为人们一直在代码中使用j++来增加,所以++j是如何在循环中工作的。如果你能提供一些细节,我将不胜感激。@AlokKumar
j++ 与
++j
相同,但是
j++
需要返回
j
的旧值,这意味着有更多的操作,可能还有一个临时的(编译器生成的)在这种情况下,如果结果被忽略,一个好的编译器应该能够将两者优化到相同的效果。使用
++j
只是我的一个习惯。