Question

In: Computer Science

int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; Create two int arrays called negative...

int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71};

  1. Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive.
  1. Write code to find the smallest value in the given array and print the value out.
  1. Write code to find the largest value in the given array and print it out.
  1. Create an int array called flipped and reverse the sign of every number in the given array and place it in the flipped array.

Solutions

Expert Solution

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:


Related Solutions

Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called...
Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive. 2)     Write code to find the smallest value in the given array and print the value out. 3)     Write code to find the largest value in the given array and print it out. 4)     Create an int...
25 49 66 44 60 36 51 78 41 54 32 54 80 56 48 41...
25 49 66 44 60 36 51 78 41 54 32 54 80 56 48 41 65 64 62 53 47 72 39 69 44 1) Average 2) Medium 3) Q1 4) Q3 5) P63 6) P93 7) Range 8) Variance 9) Standard Deviation 10) Construct a box-mustache graph for the above data.
46 42 37 52 58 36 75 72 90 65 78 80 56 40 48 60...
46 42 37 52 58 36 75 72 90 65 78 80 56 40 48 60 58 76 82 75 38 48 86 88 75 65 45 80 68 47 62 75 56 85 44 70 75 64 67 72 For the data set distribution of Salaries of the employees                                               Arrange in an array (ascending order) Evaluate the mean, the median, the mode, the skew, the average deviation the standard deviation and the interquartile range. Group, on a new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT