// Problem 8 //a. Determine the value x^n, where x is a real number and n is a natural number, by using multiplication and squared operations. //b. Given a vector of numbers, find the longest contiguous subsequence such that any two consecutive elements have contrary signs. //Problem 2 for A23 #include #include #include int read_vector(int **v); void print_vector(int *v,int size); void print_vector_with_interval(int *v,int start,int end); void ui(); float power(float x, int n); int* longest_contiguous_subsequence(int* v,int size_of_v); int read_vector(int **v){ /* :Params: v: pointer to the pointer of the vector :Returns: size: size of the vector :Description: Reads a vector from the user and returns the size of the vector and the vector itself */ int size; printf("What is the size of the vector?\n"); scanf("%d",&size); if (size<=0){ return 0; } free(*v); int *p = (int*)malloc(sizeof(int)*size); for(int i=0;i<=size-1;i++){ scanf("%d",p+i); } *v=p; return size; } void print_vector(int *v,int size){ /* :Params: v: pointer to the vector size: size of the vector :Returns: None :Description: Prints the vector */ for(int i=0;i<=size-1;i++){ printf("%d ",*(v+i)); } printf("\n"); } void print_vector_with_interval(int *v,int start,int end){ /* :Params: v: pointer to the vector start: start index end: end index :Returns: None :Description: Prints the vector from start to end */ for(int i=start;i<=end;i++){ printf("%d ",*(v+i)); } printf("\n"); } void ui(){ /* :Params: None :Returns: None :Description: User interface */ int *v=NULL; int size=0; while(true){ printf("\ 1.Read a vector\n\ 2.Determine the value x^n, where x is a real number and n is a natural number, by using multiplication and squared operations.\n\ 3.Given a vector of numbers, find the longest contiguous subsequence such that any two consecutive elements have contrary signs.\n\ 0.Exit\n\ \n\ "); int option=-1; scanf("%d",&option); switch (option) { case 1: { int new_size=read_vector(&v); if (new_size==0){ printf("Invalid size\n"); break; } size=new_size; print_vector(v,size); break; } case 2: { float x; int n; printf("What is the value of x?\n"); scanf("%f",&x); printf("What is the value of n?\n"); scanf("%d",&n); float result=power(x,n); if (result==0){ printf("Invalid power\n"); break; } printf("%f\n",result); break; } case 3: { int *p=longest_contiguous_subsequence(v,size); if (p==NULL){ printf("Invalid vector\n"); break; } print_vector_with_interval(v,*p,*(p+1)); free(p); break; } case 0: goto exit; default: printf("Invalid option\n"); break; } } exit: free(v); } float power(float x, int n){ /* :Params: x: real number n: natural number :Returns: x^n :Description: Calculates x^n by using multiplication and squared operations */ if (n<0){ return 0; } if (n==0){ return 1; } if (n==1){ return x; } if (n%2==0){ return power(x*x,n/2); } return x*power(x*x,(n-1)/2); } int* longest_contiguous_subsequence(int* v,int size_of_v){ /* :Params: v: pointer to the vector :Returns: p: pointer to the longest contiguous subsequence :Description: Finds the longest contiguous subsequence such that any two consecutive elements have contrary signs */ if (size_of_v==0){ return NULL; } int start=0; int end=0; int max_size=0; int size=0; for(int i=0;imax_size){ max_size=size; start=i-size; end=i; } size=0; } } if (size>max_size){ start=size_of_v-1-size; end=size_of_v-1; } int *p = malloc(sizeof(int)*2); *(p)=start; *(p+1)=end; return p; } int main(){ ui(); return 0; }