Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <math.h>
- double gt (int n)
- {
- double giaithua = 1;
- for( int i = 1; i <= n; i++)
- {
- giaithua=giaithua*i;
- }
- return giaithua;
- }
- double fa(double x, double c)
- {// write statements from here to compelete the function
- double s = 0,t;
- int i;
- while(1)
- { t = pow(x,2*i) / gt(2*i);
- if(t <= c) break;
- s = s + pow(-1,i)*t ;
- i++;
- }
- return(s); //This statement must be changed
- }
- //note: int &n means that the variable n is passed to the function, not it's copy.
- int findMax (int a[],int n)
- { int max=a[0];
- int i,imax;
- for ( i = 1 ; i < n; i++)
- if(a[i]> max) {max = a[i];imax=i;}
- return imax;
- }
- int findMin (int a[],int n)
- { int min=a[0];
- int i,imin;
- for ( int i = 1 ; i < n; i++)
- if(a[i]< min){ min = a[i]; imin=i;}
- return imin;
- }
- void swap ( int &x, int &y)
- { int temp = x;
- x = y;
- y = temp;
- }
- void removeAt( int i , int a[], int &n)
- { for( int j = i ; j< n-1 ; j++)
- a[j] = a[j+1];
- n--;
- }
- void fb(int a[], int &n)
- {// write statements from here to compelete the function
- int imax,imin;
- if(n % 2 !=0 ) {removeAt(0,a,n);removeAt(0,a,n);}
- else { imax = findMax(a,n);
- imin = findMin(a,n) ;
- swap(a[imax],a[imin]);}
- }
- //---------------------------------------------
- void removeAt (int i , char a[][50] , int &n)
- { for( int j = i; j <n-1; j++) strcpy(a[j],a[j+1]);
- n--;
- }
- void split ( char s[] , char t[][50] , int &n)
- { int len = strlen(s);
- int i = 0;
- n = 0;
- while(i < len)
- { while ( i < len && s[i] == ' ') i++;
- int k = 0;
- char temp[50];
- while( i < len && s[i] != ' ')
- { temp[k] = s[i];
- k++;
- i++;
- }
- temp[k] ='\0';
- strcpy(t[n], temp);
- n++;
- }
- }
- void unSplit( char t[][50],int n, char s[] )
- {
- int k = 0;
- for ( int i =0 ; i < n; i++)
- { int len = strlen(t[i]);
- for(int j =0 ; j < len; j++)
- { s[k] = t[i][j] ;
- k++;
- }
- s[k] = ' ' ;k++;
- }
- s[k-1] = '\0';
- }
- void removeLongest(char s[])
- { char t[20][50];
- int n,len=0;
- split(s,t,n);
- int imax = 0;
- // find imax
- for(int i=0;i<n;i++)if(len<strlen(t[i])){ imax=i; len=strlen(t[i]);
- }
- removeAt(imax,t,n);
- unSplit(t,n,s);
- }
- //here
- void fc(char s[])
- {// write statements from here to compelete the function
- removeLongest(s);
- }
- //and here
- void fd(char s[])
- {// write statements from here to compelete the function
- char t[20][50];
- int n;
- split(s,t,n);
- for(int i=1;i<n;i++)
- removeAt (i,t,n);
- unSplit(t,n,s);
- }
- //-------------------------------------------------------
- void display(int a[], int n) // do not edit this function
- {for(int i=0;i<n;i++) printf("%d ",a[i]);
- printf("\n");
- }
- int main()
- {system("cls");
- int choice, i;
- int a[50]; int n;
- int b[] = {3,7,4,2,6}; // n = 5;
- int c[] = {3,9,4,9,2,6}; // n = 6;
- char s[100];
- while(1)
- {printf("\nTEST Q2 (2+2+1+1 = 6 marks):\n");
- printf("1. Test fa()\n");
- printf("2. Test fb()\n");
- printf("3. Test fc()\n");
- printf("4. Test fd()\n");
- printf("0. Exit\n");
- printf("Enter your option (0 -> 4): ");
- scanf("%d",&choice);
- if(choice==0) return(0);
- //=====================================
- switch(choice)
- {case 1: printf("Test fa():\n");
- printf("fa(1.5,0.00001) = %.4f\n",fa(1.5,0.00001));
- break;
- case 2: printf("Test fb():\n");
- n = 5;
- printf("n is odd:\n");
- memcpy(a,b,n*sizeof(int));
- display(a,n);
- fb(a,n);
- display(a,n);
- n = 6;
- printf("n is even:\n");
- memcpy(a,c,n*sizeof(int));
- display(a,n);
- fb(a,n);
- display(a,n);
- break;
- case 3: printf("Test fc():\n");
- strcpy(s,"The quick brown fox");
- printf("%s\n",s);
- fc(s);
- printf("%s\n",s);
- break;
- case 4: printf("Test fd():\n");
- strcpy(s,"A09 A01 A08 A07");
- printf("%s\n",s);
- fd(s);
- printf("%s\n",s);
- break;
- default:printf("Your selection is incorrect\n");
- break;
- }
- }
- printf("\n");
- system ("pause");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement