c - 在c中的一维数组中的特定位置插入元素

标签 c

所以我想要这个输出:

Position you wanted to insert
2

Value You wanted to insert
34

Array after insertion:
10 5 34 46 2 100 97 

但是它不能显示输出,我该怎么办请帮助我,并修复它..

#include <stdio.h>
#include <conio.h>

int main(){
    int arr[100] = {10, 5, 46, 2, 100, 97};
    int n, max, i;
    n = 0;

    clrscr();
    if(i > n){
        printf("postion you wanted to insert");
        scanf("%d", &n);
    }

    printf("Value for position: \n");
    scanf("%d", &max);

    for(i=7; i>n-1; i--){
        arr[i+1] = arr[i];
        arr[n] = max;
    }

    printf("Array after insertion: \n");
    for(i = 0; i < 7; i++){
        printf("%d\t", arr[i]);
    }

    getch();
}

最佳答案

您没有为 int i 分配任何值,因此编译器将垃圾值分配给变量 i,因此您没有得到输出。

你需要做这样的事情

void main()
{
 int n,val,i;
 int arr[100] = {10,20,30,40,50};
 printf("enter the position where you want to insert the new element \n");
 scanf("%d",&n);
 print("Enter the value you want to insert");
 scanf("%d",&val);

 for(i=5,i>=n,i--)
  {
   arr[i+1]=arr[i];
   }
arr[n]=max;
/*then print your array*/
getch();
}

关于c - 在c中的一维数组中的特定位置插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148160/

相关文章:

c - 如何将堆栈变量对齐到 16 字节边界

c - 如何检查整数是否已排序(两侧)

c - 通过函数指针传递附加参数是否合法/在 C 中定义?

c - 打印文件代码

c - 一起使用 C 文件和 Cocoa 类时出现问题

打印USB内容的C代码

c - 了解 cairo_scale()

c++ - 如何使用 DSP 加速 OMAP 上的代码?

c - 关于PIC18F25k50中的内存

c - 我怎么知道 gcc 是否同意某些东西是易变的?