Write a program to find the smallest and largest element in
a given array in c language

Answer Posted / hari prasad perabattula

#include<stdio.h>
#include<stdlib.h>

int main() {
int n, *arr, i, min, max, tmin, tmax, start=2;
printf("How many elements:");
scanf("%d", &n);
arr = (int *) malloc(sizeof(int) * n);

printf("Enter %d Elements:", n);
for (i=0; i<n; i++)
scanf("%d", &arr[i]);

if(arr[0] < arr[1]) { // 1 comparison
min = arr[0];
max = arr[1];
} else {
min = arr[1];
max = arr[0];
}

tmin = min;
tmax = max;

if(n%2) {
if(arr[2] < min)
min = arr[2];
if(arr[2] > max)
max = arr[2];
start = 3;
}


for(i=start; i < n; i+=2) //
(n-2)/2 elements
{
if(arr[i] < arr[i+1]) { // 1 comparison
min = arr[i];
max = arr[i+1];
} else {
min = arr[i+1];
max = arr[i];
}

if(tmin < min) // +1
min = tmin;
if(tmax > max) // +1 = 3
max = tmax; // Total
comparisons = 3(n-2)/2
}

printf("Min: %d \nMax: %d\n", min, max);

}



Note: This gives a slightly better running time.

Is This Answer Correct ?    21 Yes 34 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the basic structure of c?

558


If the size of int data type is two bytes, what is the range of signed int data type?

595


hi to every one .. how to view table pool after creating the pooled table? plz help me.. if any knows abt this ..

1468


Can we compile a program without main() function?

636


Why do we use c for the speed of light?

606






Where can I get an ansi-compatible lint?

643


my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

1122


Are negative numbers true in c?

601


Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;

612


What is f'n in math?

622


What are the advantages of the functions?

607


What is static and volatile in c?

782


what is ur strangth & weekness

1821


I need testPalindrome and removeSpace #include #define SIZE 256 /* function prototype */ /* test if the chars in the range of [left, right] of array is a palindrome */ int testPalindrome( char array[], int left, int right ); /* remove the space in the src array and copy it over to the "copy" array */ /* set the number of chars in the "copy" array to the location that cnt points t */ void removeSpace(char src[], char copy[], int *cnt); int main( void ) { char c; /* temporarily holds keyboard input */ char string[ SIZE ]; /* original string */ char copy[ SIZE ]; /* copy of string without spaces */ int count = 0; /* length of string */ int copyCount; /* length of copy */ printf( "Enter a sentence:\n" ); /* get sentence to test from user */ while ( ( c = getchar() ) != '\n' && count < SIZE ) { string[ count++ ] = c; } /* end while */ string[ count ] = '\0'; /* terminate string */ /* make a copy of string without spaces */ removeSpace(string, copy, ©Count); /* print whether or not the sentence is a palindrome */ if ( testPalindrome( copy, 0, copyCount - 1 ) ) { printf( "\"%s\" is a palindrome\n", string ); } /* end if */ else { printf( "\"%s\" is not a palindrome\n", string ); } /* end else */ return 0; /* indicate successful termination */ } /* end main */ void removeSpace(char src[], char copy[], int *cnt) { } int testPalindrome( char array[], int left, int right ) { }

2215


Explain what is the most efficient way to store flag values?

701