简体   繁体   中英

array operations facing problem with change in size of array after insertion and after performing one operation loop exit

***header files***
#include <stdio.h> #include <stdlib.h> #include <conio.h>

display

why no of array elements not getting change while traversing array elements all initial no of elements get printed only

 int display_array(int arr[], int n) { int i; for (i = 0; i < n; i++) { printf("arr[%d] = %d\\n", i, arr[i]); } return 0; }

insertion

 int insert_position(int arr[], int n) { int i = 0, pos, num; printf("Enter the number to be inserted:\\n"); scanf("%d", &num); printf("enter the pposition at which the number is to be inserted:\\n"); scanf("%d", &pos); for (i = n - 1; i >= pos; i++) { arr[i + 1] = arr[i]; } arr[pos] = num; n = n + 1; return 0; }

deletion

 int delete_position(int arr[], int n) { int i = 0, pos; printf("Enter the position from which the number has to be deleted:\\n"); scanf("%d", &pos); for (i = pos; i < n - 1; i++) { arr[i] = arr[i + 1]; } n = n - 1; return 0; }

driver program

I think you are passing those parameters in these functions as pass by value, not pass by reference . That's why initial values are printing every time you are calling them. You can use pointers, or you can use global variables to get the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM