Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ynamic Programming You own a waterpark and are deciding what days to open for th

ID: 3724364 • Letter: Y

Question

ynamic Programming You own a waterpark and are deciding what days to open for the summer. Some days the park does well and you make money and other days there are not enough visitors and you lose money. Based on data from previous years you have come up with a guess for the amount of money you will make every day from the earliest possible opening day until the last possible closing day for a total of n days. Some days are positive earnings and some are negative earnings. You want to pick and opening day and a closing day that maximizes your summer earnings. Here is a short sample input. P I-3,6,-4,10,-5,4,-3

Explanation / Answer

#include<dirent.h>
#include<errno.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int cstring_cmp(const void *a, const void *b)
{
   return strcmp((char *)a, (char *)b);

}


int main(int argc, char *argv[]){

    struct dirent *direntp;
    DIR *d;
    char path[50];
    char opt[3];
    char data[20000][50];
    char data1[20000][50];
    char temp[50];
    int i,j;
   

    strcpy(opt,"");
    strcpy(path,".");
    if (argc == 2){
       if (argv[1][0] == '-'){
          strcpy(opt,argv[1]);
         
       }
    }

    if ((d = opendir(path)) == NULL){
        perror("Failed to open directory");
        return 1;
    }
    int count = 0;
    while((direntp = readdir(d)) != NULL){
          //printf("%s %d ",direntp->d_name,count);
          strcpy(data[count],direntp->d_name);
          count++;

    }
      
    while((closedir(d) == -1) && (errno == EINTR));
   
    if (strcmp(opt,"-s") == 0){
       qsort(data, count, sizeof(char *), cstring_cmp);
       for (i = 0; i<count; i++)
           if (data[i][0] != '.')
              printf("%-15s ",data[i]);
    
    }
    else if (strcmp(opt,"-r") == 0){
       for (i = 0; i<count; i++){
          for (j=i+1; j<count; j++){
             if (strcmp(data[i],data[j]) < 0){
                strcpy(temp,data[i]);
                strcpy(data[i],data[j]);
                strcpy(data[j],temp);
             }
          }
       }
       for (i = 0; i<count; i++)
           if (data[i][0] != '.')
              printf("%s ",data[i]);

      
    }
    else {
     
       for (i = 0; i<count; i++)
           if (data[i][0] != '.')
              printf("%s ",data[i]);

    }
   
    return 0;
}