In: Computer Science
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71};
C code:
#include <stdio.h>
#include<string.h>
int main()
{
//aray initialize with values
int arr[]={-40,-60,78,-51,65,-95,77,-48,-66,71};
//length of array
int len=10;
//--------------------PART 1-----------------------
//positive and negative array declare
int positive[10], negative[10];
int p=0,n=0; //index for positive and negative arrays
for(int i=0;i<len;i++) //loop to iterate through arr elements
{
if(arr[i]<0)
{
negative[n]=arr[i]; //put negative value in negative array
n++; //increment negative index
}
else
{
positive[p]=arr[i]; //put positive value in positive array
p++; //increment positive index
}
}
//print arrays
printf("Positive array: ");
for(int i=0;i<p;i++)
printf("%d ",positive[i]);
printf("\nNegative array: ");
for(int i=0;i<n;i++)
printf("%d ",negative[i]);
//-------------------PART 2-------------------------
int largest=arr[0],smallest=arr[0]; //let largest and smallest be the first element of arr
for(int i=0;i<len;i++)
{
if(arr[i]>largest)
largest=arr[i]; //update largest
if(arr[i]<smallest)
smallest=arr[i]; //update smallest
}
//print values
printf("\n\nSmallest = %d\nLargest = %d\n",smallest,largest);
//-------------------PART 3-------------------------
printf("\nFlipped array: "); //find flipped value
for(int i=0;i<len;i++)
{
arr[i]=arr[i]*-1; //multiplying every element by -1 to change its sign
printf("%d ",arr[i]); //print element
}
printf("\n\n");
return 0;
}
output: