In: Computer Science
C
Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If the input is invalid the function should return 1. Otherwise, the function should return 0. Note- a pointer to the array size is passed to the function, NOT the actual size.
#include<stdio.h>
#define MAX 100
int sort(int* arr, int n)
{
int i, j;
double temp;
if(n<=0){
return 1;
}
for (i = 0; i < n; i++) {
arr[i] = arr[i]*-1;
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return 0;
}
int main(void) {
int n, i;
int arr[MAX];
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d integers: \n",n);
for(i = 0;i<n;i++){
scanf("%d",&arr[i]);
}
sort(arr,n);
printf("\nSorted array: ");
for(i = 0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}


int sort(int* arr, int n)
{
int i, j;
double temp;
if(n<=0){
return 1;
}
for (i = 0; i < n; i++) {
arr[i] = arr[i]*-1;
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return 0;
}