Advertisement
captainIBM

Untitled

Apr 3rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. // RĂ©alisation Pierre DELMER et Gauthier HUBERT
  7. // Projet Langage C : Tableaux et Pointeurs
  8. // Date : 21/03/2017
  9.  
  10.  
  11. int main(int argc,char **argv)
  12. {
  13.     double answer;
  14.     int i;
  15.     srand(time(NULL));
  16.     printf("|************************** Welcome to the exercise_2 ! *************************|\n");
  17.     printf("|                                                                                |\n");
  18.     printf("|                                                                                |\n");
  19.     printf("|                                                                                |\n");
  20.     printf("|In this program we will manipulate a dynamic array !                            |\n");
  21.     printf("|Would you like to enter in the program ? Press [1/0] ( 1 -> Enter || 0 -> Quit) |\n");
  22.     printf("|                                                                                |\n");
  23.     printf("|                                                                                |\n");
  24.     printf("|               /!\\ Any error will close the program /!\\                       |\n");
  25.     printf("|                                                                                |\n");
  26.     printf("|                                                                                |\n");
  27.     printf("|********************************************************************************|\n\n");
  28.     printf("Choice : ");
  29.     scanf("%lf", &answer);
  30.     while((int)answer<0 || (int)answer>1)
  31.     while(answer==1){                           //ENTRY MENU
  32.         double menuloop = 1;
  33.         double answervalues,selectedvalue,sizeTab,answerminus,minusone,menuanswer;
  34.         int sequency = 0;
  35.         int errorSequency = 0;
  36.         double sum = 0;
  37.         double var1 = 0;
  38.         double var2;
  39.         int maxIndex,minIndex,maxSecondIndex,minSecondIndex,max,min;
  40.         printf("\n\nPlease, select the size of the array you want :");
  41.         do{
  42.             scanf("%lf", &sizeTab);                 // MENU SELECTION VALUE
  43.         }while(sizeTab<=0);
  44.         printf("By default, the values of the array will be randomly selected between -32767 and 32767.\n\n");
  45.         printf("If you want to change these values : press 1\n");
  46.         printf("If you want to keep these values : press 0");
  47.         printf("\nChoice : ");
  48.         scanf("%lf",&answervalues);
  49.             if(answervalues == 1){
  50.                 printf("Please select the max value : ");
  51.                 do{
  52.                     scanf("%lf", &selectedvalue);
  53.                     if(selectedvalue==0){
  54.                         printf("Error, please enter a correct value : ");
  55.                     }
  56.                 }while(selectedvalue==0);
  57.                 if(selectedvalue<0){
  58.                     selectedvalue=(int)(selectedvalue*(-1));
  59.                 }
  60.             }
  61.             if(answervalues==0){
  62.                 selectedvalue=32767;
  63.             }
  64.         int array[(int)sizeTab];
  65.         if(selectedvalue!=32767){
  66.             for(i=0;i<sizeTab;i++){
  67.                 array[i]=rand()%(((int)selectedvalue*2)+1)-(int)selectedvalue;
  68.             }
  69.         }else{
  70.             for(i=0;i<sizeTab;i++){
  71.                 array[i]=(rand()*2+1)-(int)selectedvalue;
  72.         }
  73.         }
  74.         printf("The draw's end is -1. Would you like to enter a specific index in the array for this value or adding it randomly ? [1/0]");
  75.             printf("Choice : ");
  76.             scanf("%lf", &answerminus);
  77.         while((int)answerminus<0 || (int)answerminus>1){
  78.                 printf("Error, enter a correct value : ");
  79.             scanf("%lf", &answerminus);
  80.         }
  81.         if(answerminus==1){
  82.             printf("Select the index you want for a specific -1 : ");
  83.             printf("Choice : ");
  84.             scanf("%lf",&minusone);
  85.             while((int)minusone<0 || (int)minusone>(sizeTab-1)){
  86.                 scanf("%lf", &minusone);
  87.             }
  88.             array[(int)minusone]=-1;
  89.         }else{
  90.             i=rand()%(int)sizeTab;                      // RANDOM -1
  91.             array[i]=-1;
  92.         }
  93.  
  94.         for(i=0;i<sizeTab;i++){                         //CALCULATE THE SEQUENCY
  95.             if(array[i]>=0){
  96.                 sequency++;
  97.             }
  98.             if(array[i]==(-1)){
  99.                 break;
  100.             }
  101.             if(array[i]<(-1)){
  102.                 errorSequency = 1;
  103.                 sequency=0;
  104.                 break;
  105.             }
  106.         }
  107.         int sequencyArray[(int)sequency];
  108.         if(errorSequency == 0 && sequency > 0){
  109.             for(i=0;i<sequency;i++){
  110.                 sequencyArray[i]=array[i];
  111.             }
  112.         }                           // MENU OF THE PROGRAM
  113.         while(menuloop==1){
  114.         printf("\n\n\n********** Menu **********\n");
  115.         printf("Select the option you want : \n\n");
  116.         printf("1\\ Display the array\n\n");
  117.         printf("2\\ Calculate the longest sequency\n\n");
  118.         printf("3\\ Calculate the index of the highest element (Return -1 if the sequency is empty)\n\n");
  119.         printf("4\\ Calculate the index of the lowest element (Return -1 if the sequency is empty)\n\n");
  120.         printf("5\\ Calculate the index of the second highest element and of the second lowest element (Return -1 if the sequency is empty)\n\n");
  121.         printf("6\\ Calculate the average of the array (Return 0 if the sequency is empty)\n\n");
  122.         printf("7\\ Calculate the variance and the standard deviation (Return 0 if the array is sequency)\n\n");
  123.         printf("8\\ Exit the menu");
  124.         printf("\n\nChoice :");
  125.         scanf("%lf",&menuanswer);
  126.         while((int)menuanswer<0 || (int)menuanswer>8){
  127.             printf("Error, please enter a correct value : ");
  128.             scanf("%lf",&menuanswer);
  129.         }
  130.         switch((int)menuanswer){
  131.             case 1:{                // DISPLAY THE ARRAY
  132.  
  133.                 for(i=0;i<sizeTab;i++){
  134.                     printf("| %d |",array[i]);
  135.                     if(i%11==10){
  136.                         printf("\n");
  137.                     }
  138.                 }
  139.                 printf("\n\n");
  140.                 break;
  141.             }
  142.             case 2:{
  143.                 if(sequency>0 && errorSequency == 0){
  144.                     printf("The longest sequency is : %d",sequency);            // DISPLAY THE LONGEST SEQUENCY
  145.                 }
  146.                 if(errorSequency==1){
  147.                     printf("Error, negative number in the sequency");
  148.                 }
  149.                 if(sequency == 0 && errorSequency == 0){
  150.                     printf("The sequency is empty");
  151.                 }
  152.                 break;
  153.             }
  154.             case 3:{                        // DISPLAY THE MAX INDEX
  155.                 max=sequencyArray[0];
  156.                 maxIndex=0;
  157.                 for(i=0;i<sequency;i++){
  158.                     if(sequencyArray[i]>=max){
  159.                         max=sequencyArray[i];
  160.                         maxIndex=i;
  161.                     }
  162.                 }
  163.                 if(sequency!=0){
  164.                     printf("Index of the max element is : %d",maxIndex);
  165.                 }else{
  166.                 printf("Empty sequency, returning -1");
  167.                 }
  168.                 break;
  169.             }
  170.             case 4:{                    // DISPLAY THE MIN INDEX
  171.                 min=sequencyArray[0];
  172.                 minIndex=0;
  173.                 for(i=0;i<sequency;i++){
  174.                     if(sequencyArray[i]<=min){
  175.                         min=sequencyArray[i];
  176.                         minIndex=i;
  177.                     }
  178.                 }
  179.                 if(sequency!=0){
  180.                     printf("Index of the min element is : %d",minIndex);
  181.                 }else{
  182.                 printf("Empty sequency, returning -1");
  183.                 }
  184.                 break;
  185.             }
  186.             case 5:{                        // DISPLAY SECOND MAX AND MIN
  187.                 if(sequency == 1){
  188.                     printf("Sequency length is 1, no second element.");
  189.                     break;
  190.                 }
  191.                 if(sequency == 0){
  192.                     printf("\nEmpty sequency, returning -1");
  193.                     break;
  194.                 }
  195.                 max=sequencyArray[0];
  196.                 maxIndex=0;
  197.                 for(i=0;i<sequency;i++){
  198.                     if(sequencyArray[i]>=max){
  199.                         max=sequencyArray[i];
  200.                         maxSecondIndex = maxIndex;
  201.                         maxIndex=i;
  202.                     }
  203.                 }
  204.                 if(sequency!=0){
  205.                     printf("Index of the second max element is : %d",maxSecondIndex);
  206.                 }
  207.                 min=sequencyArray[0];
  208.                 minIndex=0;
  209.                 for(i=0;i<sequency;i++){
  210.                     if(sequencyArray[i]<=min){
  211.                         min=sequencyArray[i];
  212.                         minSecondIndex=minIndex;
  213.                         minIndex=i;
  214.                     }
  215.                 }
  216.                 if(sequency!=0){
  217.                     printf("\nIndex of the second min element is : %d",minSecondIndex);
  218.                 }
  219.                 break;
  220.             }
  221.             case 6: {                   // DISPLAY THE AVERAGE
  222.                 if(sequency == 1){
  223.                     printf("Sequency length is 1, the average is : %d",sequencyArray[0]);
  224.                     break;
  225.                 }
  226.                 if(sequency == 0){
  227.                     printf("\nEmpty sequency, the average is 0");
  228.                     break;
  229.                 }
  230.                 for(i=0;i<sequency;i++){
  231.                     sum = sum + sequencyArray[i];
  232.                 }
  233.                 sum = sum / i;
  234.                 printf("The average is : %lf",sum);
  235.                 break;
  236.             }
  237.             case 7:{                // DISPLAY THE VARIANCE AND THE STANDARD DEVIATION
  238.                 if(sequency == 1){
  239.                     var1 = sequencyArray[0];
  240.                     var1 = pow(var1,2);
  241.                     var1 = (var1/1)-pow(sum,2);
  242.                     var2 = sqrt(var2);
  243.                     printf("The value of the variance is : %lf.2 and the standard deviation is : %lf",var1,var2);
  244.                     break;
  245.                 }
  246.                 if(sequency == 0){
  247.                     printf("\nEmpty sequency, returning 0");
  248.                     break;
  249.                 }
  250.                 for(i=0;i<sequency;i++){
  251.                     var1 = var1 + pow(sequencyArray[i],2);
  252.                 }
  253.                 var1 = var1 / i;
  254.                 var1 = var1 - pow(sum,2);
  255.                 var2 = sqrt(var1);
  256.                 printf("The value of the variance is : %lf.2 and the standard deviation is : %lf.2",var1,var2);
  257.                 break;
  258.             }
  259.             case 8:{            // QUITE MENU
  260.                 menuloop=0;
  261.                 break;
  262.             }
  263.         }
  264.         }
  265.     printf("\n\n\n********* Would you like to restart the program ? Press [1/0] *********\n\n");
  266.     printf("/!\\ Any error will close the program /!\\ \n\n");
  267.     printf("Choice : ");
  268.     scanf("%lf",&answer);
  269.     }
  270.  
  271.     return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement