Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_LENGTH 100
- #define SIZE 15
- #define FOR(i, n) for(i = 0; i < n; i++)
- /**
- * So sorry, my keyboard does not have the backslash key.
- */
- void displayMessage(char* message);
- int getPositiveInt();
- int getInt();
- int compareInDescOrder(const void *a, const void *b);
- void getElement(int *array, int size);
- int getTheSecondLargestNumber(int *array, int size, int *secondOne);
- int main() {
- int array[SIZE + 1];
- int secondLargestNum, isFound;
- getElement(array, SIZE);
- if(!getTheSecondLargestNumber(array, SIZE, &secondLargestNum)) {
- displayMessage("All elements in this array are the same.");
- return 1;
- }
- printf("The second biggest element is %d\n", secondLargestNum);
- return 0;
- }
- int getTheSecondLargestNumber(int *array, int size, int *secondOne) {
- int i;
- qsort(array, SIZE, sizeof(int), compareInDescOrder);
- FOR(i, SIZE) {
- if(array[i] != array[0]) {
- *secondOne = array[i];
- return 1;
- }
- }
- return 0;
- }
- void getElement(int *array, int size) {
- int i;
- if(size < 2) {
- displayMessage("It's stupid if you want to find a second largest element in the array like this.");
- return;
- }
- displayMessage("Please enter an array:");
- FOR(i, size) {
- printf("Element #%d: ", i + 1);
- array[i] = getPositiveInt();
- }
- }
- int getPositiveInt() {
- int res;
- while((res = getInt()) < 0) {
- displayMessage("Please enter a positive integer!");
- }
- return res;
- }
- int getInt() {
- int res;
- while(scanf("%d", &res) != 1) {
- displayMessage("Please enter an integer!");
- while(getchar() != '\n');
- }
- while(getchar() != '\n');
- return res;
- }
- int compareInDescOrder(const void *a, const void *b) {
- int *x = (int*)a;
- int *y = (int*)b;
- return *y - *x;
- }
- void displayMessage(char* message) {
- if(NULL == message) return;
- printf("%s\n", message);
- }
Advertisement
Add Comment
Please, Sign In to add comment