Professional Documents
Culture Documents
HW 6
HW 6
.1
B :
A
A- B-
A- B-
>#include <stdio.h
#define ARRAY_SIZE1 5
#define ARRAY_SIZE2 5
1
!N
return ans;
}
int is_value_exists(int arr[], int size, int value)
/*the function gets a value and an array and checks if the array contains
the value*/
/*if contains - returns 1. if does not contain return 0*/
{
int i, ans=0;
for(i = 0; i < size && ans==0; i++)
if (arr[i] == value)
ans=1;
return ans;
}
int is_permutation(int array1[], int size1, int array2[], int size2)
{
int i=0, ans=1;
/* the reason for ans=1 - the arrays are permutation while it hasn't proved
otherwise*/
/*cheack for the same size*/
if (size1!=size2)
ans=0;
else
{
/*cheack for double input in the first array*/
if (any_double_input( array1, size1)==1)
ans=0;
else
{
for (i=0; i<size1; i++)
if (is_value_exists(array2, size2, array1[i])==0)
{
ans=0;
break;
}
}
}
return ans;
}
int main()
{
int i=0;
int arr1[ARRAY_SIZE1]={0};
int arr2[ARRAY_SIZE2]={0};
/*get 2 arrays form the users */
printf("please insert the first %d numbers: \n",ARRAY_SIZE1);
for (i=0; i<ARRAY_SIZE1 ; i++)
scanf("%d",&arr1[i]);
printf("please insert the last %d numbers: \n", ARRAY_SIZE2);
for (i=0; i<ARRAY_SIZE2 ; i++)
scanf("%d",&arr2[i]);
/*now i have 2 arrays, each contain ARRAY_SIZE numbers which
i need to cheack for permutation */
if (is_permutation( arr1, ARRAY_SIZE1, arr2, ARRAY_SIZE2)==1)
printf("the arrays are permutative\n");
else
printf(" the arrays are not permutative \n");
return 0;
}
)
2
)
.2
-
- . ,
"
.
.
, a n [a[0],a[1], ,a[n-1 k-
,(i1i2..ik<n (k < n0 a
[a[ij ,[a[ij+1- [a[ik .[a[i1-
:
} {0,10,20,30,40 2,3,4
} ,{0,10,40,20,30 ) 20-
(2 ) 30 ,3 (3 ,4
) 40- (4 .2
} {1,2,3,4,5 0,4
}.{5,2,3,4,1
} {1,2,3 0,1,2
}.{3,1,2
:
int shift_subarray(int arr[],int size1,
;)int indices[], int size2
arr size1 indices-
.size2 -
arr " .indices
shift_subarray
:
size1 > size2
)(i1i2..ik<n0
) TRUE (0-
, ) FALSE .(0
,5
- } {1,2,4
. '.
:
:
12345
15243
* Assignment No: assignment 06
* Question No: question 02
#include <stdio.h>
#define ARRAY_SIZE_IN 3
#define ARRAY_SIZE2 5
int shift_subarray(int arr[],int size1, int indices[], int size2)
{
/* the indices array is legal while it hasn't proved otherwise. therefore:*/
int legality=1;
int temp=0, i=0;
/* now i have to confirm the legality of the indices array
according to the 3 legality conditions*/
/* for legal indices array -->"legality" gets 1. For NOT legal
indices array --> "legality" gets 0.*/
for (i=0 ; i<size2 ; i++)
if ((arr[i])>(arr[i+1]) || (arr[i])>=(size1) || arr[i]<0)
{
legality=0;
break;
}
if (legality=1)
{
}
return legality;
}
if (size2==2)
{
temp=arr[indices[2]];
arr[indices[2]]=arr[indices[1]];
arr[indices[1]]=temp;
}
else
{
temp=arr[indices[size2-1]];
for (i=(size2-1) ; i>0 ; i--)
arr[indices[i]]=arr[indices[i-1]];
arr[indices[0]]=temp;
}
.
'
int main()
{
int ans=0, i=0;
int arr_in[ARRAY_SIZE_IN]={1,2,4};
int arr2[ARRAY_SIZE2]={0};
/*get an arrays form the user */
printf("please insert the %d numbers in the original order: \n",ARRAY_SIZE2);
for (i=0; i<ARRAY_SIZE2 ; i++)
scanf("%d",&arr2[i]);
/*now i have 2 arrays, the first one is the indices array and the
second is the user's array which need to be change*/
if ( shift_subarray(arr2,ARRAY_SIZE2, arr_in, ARRAY_SIZE_IN)==1)
{
printf("the new order is:\n");
for (i=0; i<ARRAY_SIZE2 ; i++)
printf("%d ", arr2[i]);
}
else
return 0;
}