-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyArray.c
56 lines (50 loc) · 1.25 KB
/
MyArray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<stdlib.h>
typedef struct myArray{
int init_size;
int len;
int *array;
} myarr;
void set(myarr* arr,int val,int pos){
if(pos==0){
arr->array= (int*)malloc(arr->init_size * sizeof(int));
*(arr->array)=val;
}
else if( pos > arr->init_size && arr->len +1 > arr->init_size){
printf("Array is filled");
}else{
*(arr->array+pos)=val;
arr->len= arr->len+1;
}
}
int get(myarr* arr,int ind){
if(ind > arr->len){
return -2700;
}else{
return *(arr->array + ind);
}
return 0;
}
int main(){
myarr arr[5];
int size,value;
int n;
printf("Enter the number of Array you want\n");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("Enter the size of %d array\n",i+1);
scanf("%d", &arr[i].init_size );
printf("length of array is %d\n",arr[i].init_size);
for(int j=0;j<=arr[i].init_size-1;j++){
printf("Enter value of %d array at postion %d \n",i+1,j+1);
scanf("%d",&value);
set(&arr[i],value,j);
}
}
for(int j=0;j<n;j++){
for(int i=0;i<arr[j].init_size;i++)
printf("%d ",get(&arr[j],i));
printf("\n");
}
return 0;
}