#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct tagNode { ElementType Data; } Node; typedef struct tagArrayStack { int Capacity; int Top; Node* Nodes; } ArrayStack; void AS_CreateStack(ArrayStack** Stack, int Capacity); void AS_DestroyStack(ArrayStack* Stack); void AS_Push(ArrayStack* Stack, ElementType Data); ElementType AS_Pop(ArrayStack* Stack); ElementType AS_Top(ArrayStack* Stack); int AS_GetSize(ArrayStack* Stack); int AS_IsEmpty(ArrayStack* Stack); void AS_CreateStack(ArrayStack** Stack, int Capacity) { (*Stack)=(ArrayStack*)malloc(sizeof(ArrayStack)); (*Stack)->Nodes=(Node*)malloc(sizeof(Node)*Capacity); (*Stack)->Capacity=Capacity; (*Stack)->Top=0; } void AS_DestroyStack(ArrayStack* Stack) { free(Stack->Nodes); free(Stack); } void AS_Push(ArrayStack* Stack, ElementType Data) { int Position = Stack->Top; Stack->Nodes[Position].Data = Data; Stack->Top++; } ElementType AS_Pop(ArrayStack* Stack) { int Position = --(Stack->Top); return Stack->Nodes[Position].Data; } ElementType AS_Top(ArrayStack* Stack) { int Position = (Stack->Top) - 1; return Stack->Nodes[Position].Data; } int AS_GetSize(ArrayStack* Stack) { return Stack->Top; } int AS_IsEmpty(ArrayStack* Stack) { return (Stack->Top == 0); } int main(void) { int i=0; ArrayStack* Stack = NULL; AS_CreateStack(&Stack, 10); AS_Push(Stack, 3); AS_Push(Stack, 37); AS_Push(Stack, 11); AS_Push(Stack, 12); printf("Capacity: %d, Size: %d, Top: %d\n\n",Stack->Capacity, AS_GetSize(Stack), AS_Top(Stack)); while(!AS_IsEmpty(Stack)) { printf("Popped: %d, ",AS_Pop(Stack)); if(!AS_IsEmpty(Stack)) printf("Current Top: %d\n",AS_Top(Stack)); } printf("Stack Is Empty.\n"); AS_DestroyStack(Stack); return 0; }
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <math.h> //declare variables double bpm, ewma, ewma_prev, average , raw, alpha, stored_bpm, D1, D2, read_pin, ewma_comp; int t_int; clock_t time1,time2; double t_old, t_new; //////SIMULATED SIGNAL READING DATA///// double signal_read[] = {1107,1263,1304,1286,2928,2042,1256,1603,1243,1152,1240,1246,1259,2676,2853,1256,1486,1339,1141,1219,1241,1250,2008,2927,1351,1415,1466,1181,1164,1224,1225,2021,2667,1211,1313,1328,1078,1105,1261,1294,2839,2190,1232,1553,1297,1119,1208,1275,1902,2926,1373,1428,1482,1145,1180,1256,1280,2927,1909,1131,1428,1129,985,1162,1233,1778,2669,1355,1362,1500,1181,1192,1295,1860,2925,1345,1421,1531,1163,1216,1298,1330,2927,1998,1246,1514,1190,1077,1221,1266,2223,2925,1293,1412,1357,1035,1096,1187,1204,2929,1701,1096,1501,1216,1124,1326,1355,2913,2004,1084,1563,1321,1139,1225,1286,2753,2790,1159,1504,1307,1068,1189,1295,1739,2939,2934,2944,2938,2938,2938 }; //////SIMULATED TIME READING DATA///// double time_read[] = {0,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3100,3200,3300,3400,3500,3600,3700,3800,3900,4000,4100,4200,4300,4400,4500,4600,4700,4800,4900,5000,5100,5200,5300,5400,5500,5600,5700,5800,5900,6000,6100,6200,6300,6400,6500,6600,6700,6800,6900,7000,7100,7200,7300,7400,7500,7600,7700,7800,7900,8000,8100,8200,8300,8400,8500,8600,8700,8800,8900,9000,9100,9200,9300,9400,9500,9600,9700,9800,9900,10000,10100,10200,10300,10400,10500,10600,10700,10800,10900,11000,11100,11200,11300,11400,11500,11600,11700,11800,11900,12000,12100,12200,12300,12400,12500,12600,12700,12800,12900}; int main() { //initialize variables value bpm =0; ewma = 1200; average = 0; raw = 0; alpha=1.5; // X 10% D1 = 0; D2 =0; t_old =0; ewma_prev = 1200; stored_bpm =0; //////////////// t_int = 0; while (t_int<= 130) // loop begins // set how many loops or iterations are desired // //POINT OF INTEREST { ewma_prev = ewma; //store previous EWMA //read analog pin //////ANALOG READING SIMULATION read_pin = signal_read[t_int]; ////////////////////////////////// raw = read_pin; /// POINT OF INTEREST D1 = raw; ///calculate EWMA ewma = (alpha*D1 + (10-alpha)*ewma)/10; ewma_comp = ((ewma-1000)/(ewma_prev-1000))*100; //raw_array[0] = D2; D2 = D1; if (ewma_comp>136) /// detect signal peaks, comparing an EWMA with another //EWMA in previous iteration (101/100) { t_new = time_read[t_int];//set a timestamp once a peak is detected if (ewma>1000 && ewma<2400) // prevent ewma misread or data misread (200,700) { ////get latest running time in ms //t_new = clock(); ////// CAUTION ///calculate bpm //bpm = 1*60000/(t_new-t_old); ///measured time in milliseconds //bpm = 1*60000/ (((double) (t_new - t_old)) / CLOCKS_PER_SEC);//measured time in milliseconds bpm = 1000*60/ (t_new-t_old); ///measured time in seconds(*1) or milliseconds(x1000) t_old = t_new;///duplicate the t_new once its only use was finished in this iteration if (bpm > 0 && bpm < 5600) ///prevent systematic bpm miscalculation(20,220) { stored_bpm = bpm; /// update bpm } else { bpm = stored_bpm; /// bpm remains the same printf("Measured BPM is outside acceptable range \n"); } } } printf("T_peak : %.2f | signal : %.2f |ewma = %.0f| ewma comparison =%.2f percent | BPM = %.2f \n",t_new, D1, ewma,ewma_comp, stored_bpm ); t_int++; } return 0; }
#include <stdio.h> void main() { int r1,r2,c1,c2,a[10][10],b[10][10],c[10][10],i,j,k; printf("Enter the no. of rows and columns of 1st matrix:\n"); scanf("%d%d",&r1,&c1); printf("Enter the no. of rows and columns of 2nd matrix:\n"); scanf("%d%d",&r2,&c2); if(c1==r2) { printf("Enter elements of 1st matrix:"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } } printf("Enter elements of 2nd matrix:"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<r1;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } printf("Product of a and b\n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%d",c[i][j]); } printf("\n"); } } else printf("Invalid input"); }
#include <stdio.h> int main(void) { int a = 0; int b = 0; FILE *fp; int array[10]; //配列を宣言(10) fp = fopen("before_sort.txt","r"); //ファイルをオープンし、戻り値を返す。 if(fp == NULL) { //もしも戻り値が何もない時にオープンエラーと表示 printf("オープンエラー"); } else { //そうでなければ printf("file open\n"); //ファイルオープンと表示 } while(a = fgetc(fp) != EOF) { //ファイルを抜けるまで処理を繰り返す a = fgetc(fp); //ファイルを一文字ずつaに格納する putchar(a); //一文字ずつ表示 array[b] = a; //配列bにaの値(ファイルの中身)一文字を格納する b++; //配列bに+1を入れる } fclose(fp); //ファイル終了 return 0; } /* for(j = 0;j < 9;j++){ printf("%d",j); for(n = 0;n < 9;n++){ printf("%d",n); } }*/ /* if(a[j] < a[n]){ b = a[j]; a[j] = a[n]; a[n] = b; } printf("%d",a[n]); return 0; }*/
// // APS105 Lab 9 // // This is a program written to maintain a personal music library, // using a linked list to hold the songs in the library. // // Author: Ron Thomas // Student Number:1005145629 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <stdbool.h> // A node in the linked list // Each string in the node is declared as a character pointer variable, // so they need to be dynamically allocated using the malloc() function, // and deallocated using the free() function after use. typedef struct node { char *artist; char *songName; char *genre; struct node *next; } Node; // Declarations of linked list functions //this is a pointer of type Node which points to a type node structure typedef struct linkedList{ Node *head; }LinkedList; // DECLARE YOUR LINKED-LIST FUNCTIONS HERE bool checkListEmpty(LinkedList *list); bool insertSongFront(LinkedList *musicList, char *artist, char *songName, char *genre); bool insertSongLast(LinkedList *musicList, char *artist,char *songName, char *genre); bool alphabeticalSongOrder(LinkedList *musicList,char *artist, char *songName, char *genre); Node *addNewSong(char *artist,char *songName,char *genre); void printLibrary(LinkedList *musicList); Node *searchThroughList(LinkedList *musicList,char *promptName); void deleteAllSong(LinkedList *musicList); void deleteFirstSong(LinkedList *musicList); bool deleteSingleSong(LinkedList *musicList,char *promptDeleteSong); // Declarations of support functions // See below the main function for descriptions of what these functions do void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; int main(void) { // Declare the head of the linked list. LinkedList *musicList=(LinkedList*)malloc(sizeof(LinkedList));; musicList-> head=NULL; // Announce the start of the program printf("Personal Music Library.\n\n"); printf("%s", "Commands are I (insert), D (delete), S (search by song name),\n" "P (print), Q (quit).\n"); char response; char input[MAX_LENGTH + 1]; do { inputStringFromUser("\nCommand", input, MAX_LENGTH); // Response is the first character entered by user. // Convert to uppercase to simplify later comparisons. response = toupper(input[0]); if (response == 'I') { // Insert a song into the linked list. // Maintain the list in alphabetical order by song name. // USE THE FOLLOWING STRINGS WHEN PROMPTING FOR DATA: char *promptName = malloc((MAX_LENGTH+1)*sizeof(char)); char *promptArtist = malloc((MAX_LENGTH+1)*sizeof(char)); char *promptGenre = malloc((MAX_LENGTH+1)*sizeof(char)); inputStringFromUser("Song Name", promptName, MAX_LENGTH); inputStringFromUser("Artist", promptArtist, MAX_LENGTH); inputStringFromUser("Genre", promptGenre, MAX_LENGTH); if(alphabeticalSongOrder(musicList,promptArtist, promptName, promptGenre)==false){ songNameDuplicate(promptName); } } else if (response == 'D') { // Delete a song from the list. char *prompt = "\nEnter the name of the song to be deleted" ; char *promptDelete=malloc((MAX_LENGTH+1)*sizeof(char)); inputStringFromUser(prompt, promptDelete, MAX_LENGTH); if(deleteSingleSong(musicList,promptDelete)){ songNameDeleted(promptDelete); } else{ songNameNotFound(promptDelete); } // ADD STATEMENT(S) HERE } else if (response == 'S') { // Search for a song by its name. char *promptSearch=malloc((MAX_LENGTH+1)*sizeof(char)); char *prompt = "\nEnter the name of the song to search for" ; inputStringFromUser(prompt, promptSearch, MAX_LENGTH); Node *temp=searchThroughList(musicList,promptSearch); if(temp!=NULL){ songNameFound(temp->songName); //print statements printf("\n%s\n",temp->songName); printf("%s\n",temp->artist); printf("%s\n",temp->genre); } else{ songNameNotFound(promptSearch); } } else if (response == 'P') { // Print the music library. if(musicList->head==NULL){ printMusicLibraryEmpty(); } else{ printMusicLibraryTitle(); printLibrary(musicList); } // ADD STATEMENT(S) HERE } else if (response == 'Q') { deleteAllSong(musicList); printMusicLibraryEmpty(); } else { // do this if no command matched ... printf ("\nInvalid command.\n"); } } while (response != 'Q'); // Delete the entire linked list. // already down in ==Q response. // Print the linked list to confirm deletion. printLibrary(musicList); return 0; } // Support Function Definitions // Prompt the user for a string safely, without buffer overflow void inputStringFromUser(char *prompt, char *s, int maxStrLength) { int i = 0; char c; printf("%s --> ", prompt); while (i < maxStrLength && (c = getchar()) != '\n') s[i++] = c; s[i] = '\0'; } // Function to call when the user is trying to insert a song name // that is already in the personal music library. void songNameDuplicate(char *songName) { printf("\nA song with the name '%s' is already in the music library.\n" "No new song entered.\n", songName); } // Function to call when a song name was found in the personal music library. void songNameFound(char *songName) { printf("\nThe song name '%s' was found in the music library.\n", songName); } // Function to call when a song name was not found in the personal music library. void songNameNotFound(char *songName) { printf("\nThe song name '%s' was not found in the music library.\n", songName); } // Function to call when a song name that is to be deleted // was found in the personal music library. void songNameDeleted(char *songName) { printf("\nDeleting a song with name '%s' from the music library.\n", songName); } // Function to call when printing an empty music library. void printMusicLibraryEmpty(void) { printf("\nThe music library is empty.\n"); } // Function to call to print a title when the entire music library is printed. void printMusicLibraryTitle(void) { printf("\nMy Personal Music Library: \n"); } // Add your functions below this line. bool checkListEmpty(LinkedList *list){ if(list->head==NULL){ return true; } return false; } //checks memory availability and creates a new Node for new added song based. Node *addNewSong(char *artist,char *songName,char *genre){ Node *newSong=(Node*)malloc(sizeof(Node)); if(newSong!=NULL){ //checking if space was avaiable in memory newSong->artist=artist; newSong->songName=songName; newSong->genre=genre; } return newSong; } bool insertSongFront(LinkedList *musicList, char *artist, char *songName, char *genre){ if(checkListEmpty(musicList)==true){ musicList->head=addNewSong(artist,songName,genre); return musicList!=NULL; } Node *temp=addNewSong(artist,songName,genre); if(temp==NULL){ return false;//unable to find space } temp->next=musicList->head;//links the new soon to be #1 node to new #2 node musicList->head=temp;//assigns musicList to new #1 node; return true; } //adding some to end of list bool insertSongLast(LinkedList *musicList, char *artist,char *songName, char *genre){ if(checkListEmpty(musicList)==true){ return insertSongFront(musicList,artist,songName,genre); } Node *temp=musicList->head; //go through list until last node reached (symbolified by pointing to null) while(temp->next!=NULL){ temp=temp->next; } temp->next=addNewSong(artist,songName,genre); if(temp->next==NULL){ return false; } return true; } bool alphabeticalSongOrder(LinkedList *musicList,char *artist, char *songName, char *genre){ if(checkListEmpty(musicList)==true){ return insertSongFront(musicList,artist,songName,genre); } if(strcmp(songName,musicList->head->songName)<0){ //new song names old first song precedes return insertSongFront(musicList,artist,songName,genre); } Node *n=musicList->head; while(n->next!=NULL&&strcmp(songName,n->songName)>0){//goes through until the song is in the write place or last node is reached n=n->next; } if(strcmp(songName,n->songName)==0){//duplicate return false; } Node *temp=addNewSong(artist,songName,genre); if(temp==NULL){ return false; } temp->next=n->next;//links what n was linked to temp so it can continued to be linked n->next=temp;//connects next song into the list in the correct order. return true; } void printLibrary(LinkedList *musicList){ Node *temp=musicList->head; while(temp!=NULL){ printf("\n\n%s\n",temp->songName); printf("%s\n",temp->artist); printf("%s\n",temp->genre); temp=temp->next; } } Node *searchThroughList(LinkedList *musicList,char *promptName){ Node *temp=musicList->head; while(temp!=NULL){ if(strcmp(promptName,temp->songName)==0){ return temp; } temp=temp->next; } return temp; } void deleteFirstSong(LinkedList *musicList){ if(checkListEmpty(musicList)==true){ return; } Node *newHead=musicList->head->next; songNameDeleted(musicList->head->songName); //Free up the memory used by the current head free(musicList->head); //update current head musicList->head=newHead; } void deleteAllSong(LinkedList *musicList){ //going through all deleting first songs untill all songs are done while(checkListEmpty(musicList)==false){ deleteFirstSong(musicList); } //list is empty (does not point to any nodes) musicList->head=NULL; } bool deleteSingleSong(LinkedList *musicList,char *promptDeleteSong){ if(checkListEmpty(musicList)==true){ return false;//nothing to delete } if(strcmp(musicList->head->songName,promptDeleteSong)==0){ //if first song is to be deleted transfer pointers to continue chain Node *temp=musicList->head->next; free(musicList->head); musicList->head=temp; return true; } //if it is not the first song, try to find where it is Node *temp2=musicList->head; while(temp2->next!=NULL&&strcmp(temp2->next->songName,promptDeleteSong)!=0){ temp2=temp2->next; } if(temp2->next!=NULL){//if song name has been found, delete it while still maintaing the list Node *temp3=temp2->next; temp2->next=temp3->next; free(temp3); return true; } return false;//means that value has not been found }
;fourier series clear all close all clc syms x f =input('Enter the function of x: '); I=input('Enter the interval of [a,b]: '); m=input('Enter the number of Harmonics required: '); a=I(1);b=I(2); L=(b-a)/2; a0=(1/L)*int(f,a,b); Fx=a0/2; for n=1:m figure; an(n)=(1/L)*int(f*cos(n*pi*x/L),a,b); bn(n)=(1/L)*int(f*sin(n*pi*x/L),a,b); Fx=Fx+an(n)*cos(n*pi*x/L)+bn(n)*sin(n*pi*x/L); Fx=vpa(Fx,4); ezplot(Fx,[a,b]); hold on ezplot(f,[a,b]); title(['Fourier Series with ',num2str( n ),'harmonics']); legend('Fourier Series', 'Function Plot'); hold off end disp(strcat('Fourier series with', num2str(n),'harmonics is:',char(Fx))) ;harmonic analysis clear all clc syms t x=input('Enter the equally spaced values of x: '); y=input('Enter the values of y=f(x): '); m=input('Enter the number of harmonics required: '); n=length(x);a=x(1);b=x(n); h=x(2)-x(1); L=(b-a+h)/2; theta=pi*x/L; a0=(2/n)*sum(y); Fx=a0/2; x1=linspace(a,b,100); for i=1:m figure an=(2/n)*sum(y.*cos(i*theta)); bn=(2/n)*sum(y.*sin(i*theta)); Fx=Fx+an*cos(i*pi*t/L)+bn*sin(i*pi*t/L) ; Fx=vpa(Fx,4); Fx1=subs(Fx,t,x1); plot(x1,Fx1); hold on plot(x,y); title(['Fourier Series with ',num2str( i ),'harmonics']) legend('Fourier Series', 'Function Plot') hold off; end disp(strcat('Fourier series with', num2str(i),'harmonics is:',char(Fx))); ;eigen value,vector clc clear A=input(’Enter the Matrix: ’); %Characteristic Equation cf=poly(A); disp(’Characteristic Equations’) disp(cf) %Eigenvalues EV=eig(A); disp(’Eigenvalues’) disp(EV) %Eigenvectors [P D]=eig(A); disp(’Eigenvectors’) disp(P) ;product determinant eigen clc clear Page 3 A=input(’Enter the Matrix: ’); %Determinant detA=det(A); disp(’Determinant of A:’) disp(detA) %Eigenvalues EV=eig(A); disp(’Eigenvalues:’) disp(EV) %Product of eigenvalues prev=prod(EV); disp(’Product of Eigenvalues:’) disp(prev) ;cayley-hamilton clc clear A=input(’Enter the Matrix: ’); %Verification of Cayley-Hamilton theorem cf=poly(A); n=length(cf); CHT=cf(1)*Aˆ (n-1); for i=2:n CHT=CHT+cf(i)*Aˆ (n-i); end disp(’R.H.S of C-H Theorem: ’) disp(round(CHT)) %To find the inverse INV=cf(1)*Aˆ (n-2); for i=2:n-1 INV=INV+cf(i)*Aˆ (n-i-1); end INV=INV/(-cf(n)); disp(’Inverse of A: ’) disp(INV) ;solving differential equation using variation of parameters clear all close all clc syms c1 c2 x m F=input('Enter the coefficients [a,b,c]: '); f=input('Enter the RHS function f(x): '); a=F(1);b=F(2);c=F(3); AE=a*m^2+b*m+c; % Auxilliary Equation m=solve(AE); m1=m(1); m2=m(2); D=b^2-4*a*c; if(D>0) % Roots are real and different y1=exp(m1*x);y2=exp(m2*x); elseif (D==0)% Roots are real and equal y1=exp(m1*x);y2=x*exp(m1*x); else % Roots are complex alfa=real(m1);beta=imag(m1); y1=exp(alfa*x)*cos(beta*x); y2=exp(alfa*x)*sin(beta*x); end yc=c1*y1+c2*y2; % Complimentary Solution %%% Particular Integral by Method of variation of parameters. fx=f/a; W=y1*diff(y2,x)-y2*diff(y1,x); %%% Wronskian%%% u=int(-y2*fx/W,x); v=int(y1*fx/W,x); yp=y1*u+y2*v; %%%Particular Integral%%% y_gen=yc+yp; %%%General Solution%%% check=input('If the problem has initial conditions then enter 1 else enter 2: '); if(check==1) cn=input('Enter the initial conditions [x0, y(x0), Dy(x0)]:'); dy_gen=diff(y_gen); eq1=(subs(y_gen,x,cn(1))-cn(2)); eq2=(subs(dy_gen,x,cn(1))-cn(3)); [c1 c2]=solve(eq1,eq2); y=simplify(subs(y_gen)); disp('The complete solution is'); disp(y); ezplot(y, [cn(1),cn(1)+2]); else y=simplify(y_gen); disp('The General Solution is '); disp(y); end ;solution of differential equation using laplace transform clear all clc syms t s y(t) Y dy(t)=diff(y(t)); d2y(t)=diff(y(t),2); F = input('Input the coefficients [a,b,c]: '); a=F(1);b=F(2);c=F(3); nh = input('Enter the non-homogenous part f(x): '); eqn=a*d2y(t)+b*dy(t)+c*y(t)-nh; LTY=laplace(eqn,t,s); IC = input('Enter the initial conditions in the form [y0,Dy(0)]: '); y0=IC(1);dy0=IC(2); LTY=subs(LTY,{'laplace(y(t), t, s)','y(0)','D(y)(0)'},{Y,y0,dy0}); eq=collect(LTY,Y); Y=simplify(solve(eq,Y)); yt=simplify(ilaplace(Y,s,t)); disp('The solution of the differential equation y(t)=') disp(yt); ; ezplot(yt,[y0,y0+2]); ;solution of first order and second order by matrix method clc clear syms t C1 C2 A=input(’Enter A: ’); [P,D]=eig(A); L1=D(1);L2=D(4); y1=C1*exp(L1*t);y2=C2*exp(L2*t); Y=[y1;y2]; X=P*Y; Cond=input(’Enter the initial conditions [t0, x10,x20]: ’); t0=Cond(1);x10=Cond(2);x20=Cond(3); eq1=subs(X(1)-x10,t0);eq2=subs(X(2)-x20,t0); [C1, C2] = solve(eq1,eq2); X=subs(X); ;z-transform and their application for solving differential equation clear all clc syms n z y(n) Y yn=y(n); yn1=y(n+1); yn2=y(n+2); F = input('Input the coefficients [a,b,c]: '); a=F(1);b=F(2);c=F(3); nh = input('Enter the non-homogenous part f(n): '); eqn=a*yn2+b*yn1+c*yn-nh; ZTY=ztrans(eqn); IC=input('Enter the initial conditions in the form [y0,y1]:'); y0=IC(1);y1=IC(2); ZTY=subs(ZTY,{'ztrans(y(n),n,z)','y(0)','y(1)'},{Y,y0,y1}); eq=collect(ZTY,Y); Y=simplify(solve(eq,Y)); yn=simplify(iztrans(Y)); disp('The solution of the difference equation yn=') disp(yn); m=0:20; y=subs(yn,n,m); stem(y) title('Difference equation'); xlabel('n'); ylabel('y(n)'); ;solution of homogenous equations clear all clc syms n k1 k2 L F = input('Input the coefficients [a,b,c]: '); a=F(1);b=F(2);c=F(3); ch_eqn=a*L^2+b*L+c; %Characteristic equation L=solve(ch_eqn); L1=L(1);L2=L(2); D=b^2-4*a*c; if(D>0) % Roots are real and different y1=L1^n; y2=L2^n; elseif (D==0)% Roots are real and equal y1=L1^n; y2=n*L1^n; else % Roots are complex rho=abs(L1); t=angle(L1); y1 = (rho^n)*cos(n*t); y2 = (rho^n)*sin(n*t); end yn = k1*y1+k2*y2; check=input('If initial conditions are known, then enter 1 else enter 0: '); if (check == 1) IC=input('Enter the initial conditions [y(0),y(1)]'); eq1=(subs(yn,n,0)-IC(1)); eq2=(subs(yn,n,1)-IC(2)); [k1,k2]=solve(eq1,eq2); yn=simplify(subs(yn)); m=0:20; y=subs(yn,n,m); stem(y) title('Difference equation'); xlabel('n'); ylabel('y(n)'); end disp('The Solution of the given Homogeneous equation is y_n= '); disp(collect(collect(yn,y1),y2)) ;power series L = 11; num = [1 2]; den = [1 0.4 −0.12]; u = [1 zeros(1,L−1)]; x = filter(num,den,u); disp('Coefficients of the power series expansion: '); disp(x); ;similarity transformation clc clear A=input(’Enter the matrix for diagonalization :’); [P D]=eig(A); disp(’Given Matrix (A) :’) disp(A) disp(’Modal Matrix (P):’) disp(P) disp(’Inverse of P :’) PI=inv(P); disp(PI) disp(’Diagonal Matrix (D=Pˆ (-1)*A*P):’) DM=round(inv(P)*A*P, 2); disp(DM) ;orthogonal transofrmation clc clear A=input(’Enter the symetric matrix for diagonalization :’); [P D]=eig(A); disp(’Given Matrix (A) :’) disp(A) disp(’Modal Matrix (P):’) disp(P) NP=normc(P); disp(’Normalized Modal Matrix (N):’) disp(NP) disp(’Diagonal Matrix (D=Nˆ T*A*N) :’) DM=round(NP’*A*NP,2); disp(DM)
#include <stdio.h> #include <stdlib.h> #include <string.h> void addIntStrings(char *n1, char *n2, char *res) { int value; value = atoi(n1) + atoi(n2); sprintf(res, "%d", value); } int main(void) { char a[100]; char b[100]; char res[100]; scanf("%s", a); getchar(); scanf("%s", b); getchar(); addIntStrings(a, b, res); printf("%s\n", res); return 0; }
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <stdio.h> #include <math.h> #include <time.h> int nmbr=1; int t_int =0; int milliseconds = 500; double cpu_time_used, process_time; int main() { //time initialization clock_t t_1, t_2, t_start, t_end; t_start = clock(); t_1 = 0; t_2 = t_1; while (t_int<8) { t_1 = clock(); ////sleep int ms_remaining = milliseconds % 1000; long usec = ms_remaining * 1000; struct timespec ts_sleep; ts_sleep.tv_sec = (milliseconds) / 1000; ts_sleep.tv_nsec = 1000*usec; nanosleep(&ts_sleep, NULL); cpu_time_used = ((double) (t_1 - t_2)); /// CLOCKS_PER_SEC; printf("Hello World %i, time : %f \n",nmbr, cpu_time_used); t_2 = t_1; nmbr++; t_int++; } t_end = clock(); process_time = ((double) (t_end - t_start)) / CLOCKS_PER_SEC; printf("INTERRUPTED, process time :%f \n", process_time); }
#include <stdio.h> #include <stdlib.h> double circularArea(double r); int main() { double radius = 1.0, area = 0.0; printf("Areas of Circles\n\n"); return 0; }
#include "stdio.h" #include "stdlib.h" #include "time.h" int main(int argc, char **argv){ printf("Arguments: %d \n", argc); srand((unsigned int)(time(NULL))); //char *ptr; //int num; int length = 6;//(int) strtol(argv[1], &ptr, 10); printf("Password length: %d \n", length); char password[length]; int i; for(i = 0; i < length; i++){ password[i] = 33 + rand() %94; } password[i] = '\0'; printf("Generated Password with length %d: %s \n", length, password); return 0; }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more