Hi everyone,
Before you read my code, i want to mention that the main function was giving to me, that means that i should create all the functions presented in main.that is what the exercise requires.
No one of the option within the choice_operation function is done correctly, I mean, it list huge numbers like 418033 or something.
Code:#include <stdio.h> #include <stdlib.h> #define SIZE 3 void read_array(int []); void sort_array(int []); int search(int, int[]); void choice_operation(int []); int main(){ int ar[SIZE]; //array declaration (SIZE is constant) read_array(ar); //read array elements sort_array(ar); //sort the array choice_operation(ar); //display a list of operations system("PAUSE"); return 0; } void read_array(int x[]){ for (int i = 0 ; i < SIZE ; i++) scanf("%d",&x[i]); } void sort_array(int x[]){ int index,i,j; for (i = 1 ; i < SIZE ; i++){ x[i] = index; for(j = i - 1 ; j < 0 ; j--){ if (x[j] > x[i]) x[j+1] = x[j]; } x[j] = index; } } int search (int v,int x[]){ for (int i = 0 ; i < SIZE ; i++){ if (x[i] == v) return(i); } return(-1); } void choice_operation(int x[]){ int y,sum = 0,a = 0,v; printf("To display the max and the min of the array, enter 1\n"); printf("To know a position of a value, Enter 2\n"); printf("To find the average, Enter 3\n"); printf("To list the elements of the array, Enter 4\n"); printf("To exit, Enter any other character.\n"); scanf("%d",&y); switch(y){ case 1: printf("the max is %d, and the min is %d\n",x[0],x[SIZE-1]); break; case 2: printf("Enter a value to know its position within the array:\n"); scanf("%d",&v); a = search(v,x); if(a != -1) printf("the positions of %d is %d\n",v,a); else printf("this element does not belong to the array\n"); break; case 3: for (int i = 0 ; i < SIZE ; i++) sum += x[i]; printf("The average is %d\n",(sum / SIZE)); break; case 4: for(int i = 0 ; i < SIZE ; i++) printf("%d\n",x[i]); break; default: printf("The program ends.\n"); } }



LinkBack URL
About LinkBacks



