Elaboriază un program care inlocueste numerele pare cu 1 si cele inpare cu 0
Răspunsuri la întrebare
Răspuns:
#include <stdio.h>
/** funtion : readArray()
input : arr ( array of integer ), size
to read ONE-D integer array from standard input device (keyboard).
**/
void readArray(int arr[], int size)
{
int i =0;
printf("\nEnter elements : \n");
for(i=0; i < size; i++)
{
printf("Enter arr[%d] : ",i);
scanf("%d",&arr[i]);
}
}
/** funtion : printArray()
input : arr ( array of integer ), size
to display ONE-D integer array on standard output device (moniter).
**/
void printArray(int arr[], int size)
{
int i =0;
printf("\nElements are : ");
for(i=0; i < size; i++)
{
printf("\n\tarr[%d] : %d",i,arr[i]);
}
printf("\n");
}
/** funtion : replaceEvenOdd()
input : arr ( array of integer ), size
to replace EVEN elements by 0 and ODD elements by 1.
**/
void replaceEvenOdd(int arr[], int size)
{
int i=0;
for(i=0; i < size; i++)
{
if( arr[i] % 2 == 0 )
arr[i] = 0 ;
else
arr[i] = 1 ;
}
}
int main()
{
int arr[10];
readArray(arr,10);
printf("\nBefore replacement : ");
printArray(arr,10);
replaceEvenOdd(arr,10);
printf("\nAfter replacement : ");
printArray(arr,10);
return 0;
}
[acest script inlocuieste numerele pare cu 0 iar cele impare cu 1 ]