123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- #include <stdio.h>
- #include <regex.h>
- #include <stdlib.h>
- #include <malloc.h>
- int match(const char *string, char *pattern)
- {
- int status;
- regex_t re;
- if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
- return(0); /* report error */
- }
- status = regexec(&re, string, (size_t) 0, NULL, 0);
- regfree(&re);
- if (status != 0) {
- return(0); /* report error */
- }
- return(1);
- }
- typedef struct
- {
- int value;
- int loser[200];
- int numLoser;
- }loserList;
- int main(int argc, char* argv[])
- {
- int i = 1, j = 0, k=0;
- char* pattern = "^[+]?[0-9]+$";
- if(argc > 1)
- {
- loserList* l;
- l = (loserList*) malloc(sizeof(loserList)*(argc-1));
- while(argv[i] != NULL)
- {
- if(match(argv[i],pattern))
- {
- l[k].value = atoi(argv[i]);
- //l[k].loser = (int*)malloc(sizeof(int)*(argc-1));
- l[k].numLoser = 0;
- }
- else
- {
- printf("\n%s is not a valid number. Will be considered as zero.\n", argv[i]);
- }
- i++;
- k++;
- }
- /*printf("\nEntered numbers are:\n");
- k=0;
- while(j != (argc-1))
- {
- printf("%d ", l[j].value);
- j++;
- }*/
- int indexOfMax=0, m;
- int max = l[indexOfMax].value;
- k=0;
- while(j != (argc-1))
- {
- if(l[j].value >= max)
- {
- max = l[j].value;
- indexOfMax = j;
- k=0;
- }
- else
- {
- if(indexOfMax <= j)
- {
- //check for all other elements
- for(m=0;m<(argc-1); m++)
- {
- if((l[indexOfMax].value > l[m].value) && ((m < indexOfMax)))
- {
- l[indexOfMax].numLoser++;
- l[indexOfMax].loser[k] = l[m].value;
- k++;
- }
- }
- }
- }
- j++;
- }
- printf("\nMax element is %d\n", l[indexOfMax].value);
- printf("\nIndex of max: %d\n", indexOfMax);
- //second max element
- // traverse the losers list of max element
- printf("\nSize of losers list %d\n",l[indexOfMax].numLoser);
- printf("\nLosers list of max element\n");
- for(i=0; i<k ; i++)
- {
- printf(" %d ", l[indexOfMax].loser[i]);
- }
- printf("\n");
- }
- else
- printf("\nUsage: ./cs1713-day2-prog2.o ele1 ele2 ...\n");
- return 0;
- }
|