-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateArray.c
44 lines (41 loc) · 1006 Bytes
/
UpdateArray.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
#include <stdio.h>
int main(){
int numOfElements,choice,val,newVal,newValIndex;
printf("Enter number of elements\n");
scanf("%d", &numOfElements);
int arr[numOfElements];
for(int i=0;i<numOfElements; i++)
{
printf("Enter Element No.%d\n",i);
scanf("%d", &arr[i]);
}
printf("Enter 1 to update array by value\nEnter 2 to update array by index\n");
scanf("%d",&choice);
if(choice==1){
printf("Enter the value you want to change\n");
scanf("%d",&val);
printf("Enter the new value\n");
scanf("%d",&newVal);
for(int i=0;i<10;i++){
if(arr[i]==val){
arr[i]=newVal;
}
}
}
else if(choice==2){
printf("Enter the index of value you want to change\n");
scanf("%d",&newValIndex);
printf("Enter the new value\n");
scanf("%d",&newVal);
arr[newValIndex]=newVal;
}
else{
printf("Enter Valid Choice\n");
main();
}
printf("updated Array:\n");
for(int i=0;i<numOfElements;i++){
printf("%d\n",arr[i]);
}
return 0;
}