Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- // RĂ©alisation Pierre DELMER et Gauthier HUBERT
- // Projet Langage C : Tableaux et Pointeurs
- // Date : 21/03/2017
- int main(int argc,char **argv)
- {
- double answer;
- int i;
- srand(time(NULL));
- printf("|************************** Welcome to the exercise_2 ! *************************|\n");
- printf("| |\n");
- printf("| |\n");
- printf("| |\n");
- printf("|In this program we will manipulate a dynamic array ! |\n");
- printf("|Would you like to enter in the program ? Press [1/0] ( 1 -> Enter || 0 -> Quit) |\n");
- printf("| |\n");
- printf("| |\n");
- printf("| /!\\ Any error will close the program /!\\ |\n");
- printf("| |\n");
- printf("| |\n");
- printf("|********************************************************************************|\n\n");
- printf("Choice : ");
- scanf("%lf", &answer);
- while((int)answer<0 || (int)answer>1)
- while(answer==1){ //ENTRY MENU
- double menuloop = 1;
- double answervalues,selectedvalue,sizeTab,answerminus,minusone,menuanswer;
- int sequency = 0;
- int errorSequency = 0;
- double sum = 0;
- double var1 = 0;
- double var2;
- int maxIndex,minIndex,maxSecondIndex,minSecondIndex,max,min;
- printf("\n\nPlease, select the size of the array you want :");
- do{
- scanf("%lf", &sizeTab); // MENU SELECTION VALUE
- }while(sizeTab<=0);
- printf("By default, the values of the array will be randomly selected between -32767 and 32767.\n\n");
- printf("If you want to change these values : press 1\n");
- printf("If you want to keep these values : press 0");
- printf("\nChoice : ");
- scanf("%lf",&answervalues);
- if(answervalues == 1){
- printf("Please select the max value : ");
- do{
- scanf("%lf", &selectedvalue);
- if(selectedvalue==0){
- printf("Error, please enter a correct value : ");
- }
- }while(selectedvalue==0);
- if(selectedvalue<0){
- selectedvalue=(int)(selectedvalue*(-1));
- }
- }
- if(answervalues==0){
- selectedvalue=32767;
- }
- int array[(int)sizeTab];
- if(selectedvalue!=32767){
- for(i=0;i<sizeTab;i++){
- array[i]=rand()%(((int)selectedvalue*2)+1)-(int)selectedvalue;
- }
- }else{
- for(i=0;i<sizeTab;i++){
- array[i]=(rand()*2+1)-(int)selectedvalue;
- }
- }
- 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]");
- printf("Choice : ");
- scanf("%lf", &answerminus);
- while((int)answerminus<0 || (int)answerminus>1){
- printf("Error, enter a correct value : ");
- scanf("%lf", &answerminus);
- }
- if(answerminus==1){
- printf("Select the index you want for a specific -1 : ");
- printf("Choice : ");
- scanf("%lf",&minusone);
- while((int)minusone<0 || (int)minusone>(sizeTab-1)){
- scanf("%lf", &minusone);
- }
- array[(int)minusone]=-1;
- }else{
- i=rand()%(int)sizeTab; // RANDOM -1
- array[i]=-1;
- }
- for(i=0;i<sizeTab;i++){ //CALCULATE THE SEQUENCY
- if(array[i]>=0){
- sequency++;
- }
- if(array[i]==(-1)){
- break;
- }
- if(array[i]<(-1)){
- errorSequency = 1;
- sequency=0;
- break;
- }
- }
- int sequencyArray[(int)sequency];
- if(errorSequency == 0 && sequency > 0){
- for(i=0;i<sequency;i++){
- sequencyArray[i]=array[i];
- }
- } // MENU OF THE PROGRAM
- while(menuloop==1){
- printf("\n\n\n********** Menu **********\n");
- printf("Select the option you want : \n\n");
- printf("1\\ Display the array\n\n");
- printf("2\\ Calculate the longest sequency\n\n");
- printf("3\\ Calculate the index of the highest element (Return -1 if the sequency is empty)\n\n");
- printf("4\\ Calculate the index of the lowest element (Return -1 if the sequency is empty)\n\n");
- 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");
- printf("6\\ Calculate the average of the array (Return 0 if the sequency is empty)\n\n");
- printf("7\\ Calculate the variance and the standard deviation (Return 0 if the array is sequency)\n\n");
- printf("8\\ Exit the menu");
- printf("\n\nChoice :");
- scanf("%lf",&menuanswer);
- while((int)menuanswer<0 || (int)menuanswer>8){
- printf("Error, please enter a correct value : ");
- scanf("%lf",&menuanswer);
- }
- switch((int)menuanswer){
- case 1:{ // DISPLAY THE ARRAY
- for(i=0;i<sizeTab;i++){
- printf("| %d |",array[i]);
- if(i%11==10){
- printf("\n");
- }
- }
- printf("\n\n");
- break;
- }
- case 2:{
- if(sequency>0 && errorSequency == 0){
- printf("The longest sequency is : %d",sequency); // DISPLAY THE LONGEST SEQUENCY
- }
- if(errorSequency==1){
- printf("Error, negative number in the sequency");
- }
- if(sequency == 0 && errorSequency == 0){
- printf("The sequency is empty");
- }
- break;
- }
- case 3:{ // DISPLAY THE MAX INDEX
- max=sequencyArray[0];
- maxIndex=0;
- for(i=0;i<sequency;i++){
- if(sequencyArray[i]>=max){
- max=sequencyArray[i];
- maxIndex=i;
- }
- }
- if(sequency!=0){
- printf("Index of the max element is : %d",maxIndex);
- }else{
- printf("Empty sequency, returning -1");
- }
- break;
- }
- case 4:{ // DISPLAY THE MIN INDEX
- min=sequencyArray[0];
- minIndex=0;
- for(i=0;i<sequency;i++){
- if(sequencyArray[i]<=min){
- min=sequencyArray[i];
- minIndex=i;
- }
- }
- if(sequency!=0){
- printf("Index of the min element is : %d",minIndex);
- }else{
- printf("Empty sequency, returning -1");
- }
- break;
- }
- case 5:{ // DISPLAY SECOND MAX AND MIN
- if(sequency == 1){
- printf("Sequency length is 1, no second element.");
- break;
- }
- if(sequency == 0){
- printf("\nEmpty sequency, returning -1");
- break;
- }
- max=sequencyArray[0];
- maxIndex=0;
- for(i=0;i<sequency;i++){
- if(sequencyArray[i]>=max){
- max=sequencyArray[i];
- maxSecondIndex = maxIndex;
- maxIndex=i;
- }
- }
- if(sequency!=0){
- printf("Index of the second max element is : %d",maxSecondIndex);
- }
- min=sequencyArray[0];
- minIndex=0;
- for(i=0;i<sequency;i++){
- if(sequencyArray[i]<=min){
- min=sequencyArray[i];
- minSecondIndex=minIndex;
- minIndex=i;
- }
- }
- if(sequency!=0){
- printf("\nIndex of the second min element is : %d",minSecondIndex);
- }
- break;
- }
- case 6: { // DISPLAY THE AVERAGE
- if(sequency == 1){
- printf("Sequency length is 1, the average is : %d",sequencyArray[0]);
- break;
- }
- if(sequency == 0){
- printf("\nEmpty sequency, the average is 0");
- break;
- }
- for(i=0;i<sequency;i++){
- sum = sum + sequencyArray[i];
- }
- sum = sum / i;
- printf("The average is : %lf",sum);
- break;
- }
- case 7:{ // DISPLAY THE VARIANCE AND THE STANDARD DEVIATION
- if(sequency == 1){
- var1 = sequencyArray[0];
- var1 = pow(var1,2);
- var1 = (var1/1)-pow(sum,2);
- var2 = sqrt(var2);
- printf("The value of the variance is : %lf.2 and the standard deviation is : %lf",var1,var2);
- break;
- }
- if(sequency == 0){
- printf("\nEmpty sequency, returning 0");
- break;
- }
- for(i=0;i<sequency;i++){
- var1 = var1 + pow(sequencyArray[i],2);
- }
- var1 = var1 / i;
- var1 = var1 - pow(sum,2);
- var2 = sqrt(var1);
- printf("The value of the variance is : %lf.2 and the standard deviation is : %lf.2",var1,var2);
- break;
- }
- case 8:{ // QUITE MENU
- menuloop=0;
- break;
- }
- }
- }
- printf("\n\n\n********* Would you like to restart the program ? Press [1/0] *********\n\n");
- printf("/!\\ Any error will close the program /!\\ \n\n");
- printf("Choice : ");
- scanf("%lf",&answer);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement