// This C program provides a skeleton for a FINITE ELEMENTS METHOD // Author (anonymous) // Date 27.11.2007 // For Assignment 4 for Computer Programming, Ajou University //--------------------------------------------------------------------------- //Libraries used by this program #include //Handles the keyboard input and screen output #include //Handles DOS system commands ("cls", "pause") #include //Handles date and time standard functions #include //Handles (among other things) the atoi() function //Here are "function prototypes" for all 6 functions used by this program: void matrix_display(void); //displays matrix void matrix_input(void); //allows user to input matrix void matrix_update(void); //dummy function void matrix_generate(void); //dummy function void matrix_resolve(void); //solves matrix void date_and_time(void); //retrieves date from the system //Here is the "main" function (module) void main() { //Local variables initiated here int reply=0; //Reply with the menu choice int reply_sub=0; //Reply with the submenu choice int i=44, j=88; //Loop counter, loop limit //This part displays the menu items system("cls"); //clears screen printf("Welcome to MATRIX program!"); //prints welcome message date_and_time(); //call to function while (reply < 6) { printf("\n\nThis program works with matrices!\n"); printf("\nHere are menu options that this program offers:\n"); //This part displays menu options printf ("\n%d. Display", 1); //menu choice 1 printf ("\n%d. Input", 2); //menu choice 2 printf ("\n%d. Update", 3); //menu choice 3 printf ("\n%d. Generate", 4); //menu choice 4 printf ("\n%d. Resolve", 5); //menu choice 5 printf ("\n6. (or 7, ..., etc.) EXIT program."); //menu choice 6 printf ("\n"); // Changes the line printf("\nTo terminate choose the option 6 or higher!"); printf("\nPlease choose the item to run (e.g. 1, ..., 6) "); scanf("%d", &reply); // Inputs your reply printf("\nYour choice was option number %d", reply); printf("\n"); //Completes the processing for subsequent options if (reply == 1) // if user chose option 1 this part of program is executed { matrix_display(); //Call to the function system("pause"); // DOS "Pause" command (for user to look at results) } if (reply == 2) //if user chose option 2 this part of program is executed { matrix_input(); //Call to the function system("pause"); // DOS "Pause" command (for user to look at results) } if (reply == 3) //if user chose option 3 this part of program is executed { matrix_update(); //Call to the function system("pause"); // DOS "Pause" command (for user to look at results) } if (reply == 4) //if user chose option 3 this part of program is executed { matrix_generate(); //Call to the function system("pause"); // DOS "Pause" command (for user to look at results) } if (reply == 5) //if user chose option 3 this part of program is executed { matrix_resolve(); //Call to the function system("pause"); // DOS "Pause" command (for user to look at results) } if (reply >= 6) { //This completes actions for the option PROGRAM EXIT printf (" which terminates this program!"); } } //The final displays printf ("\n\nThank you for using this program. Goodbye!"); printf ("\n"); // Changes the line system("pause"); // DOS "Pause" command (for user to look at results) } //End of "main" void date_and_time() { char date_string [9]; //stores the retrieved date and time int year_pc = 01; // stores the retrieved year in integer form int month_pc = 01; // stores the retrieved month in integer form int day_pc = 01; //stores the retrieved day in integer form _strdate (date_string); //This retrieves the current date day_pc = (date_string[3] -48)*10 + (date_string[4] - 48)*1; // converts day month_pc = (date_string[0] -48)*10 + (date_string[1] -48)*1; // converts month year_pc = ((date_string[6] -48)*10 +(date_string[7] -48)*1)+2000; // converts year printf("\nToday is the %d.%d.%d", day_pc, month_pc, year_pc); //outputs the current date on the screen } void matrix_display() //matrix display function { FILE *fromfile; //Variable pointer to a file fromfile = fopen ("graph_matrix.txt", "r"); //opens file for reading char content_in[60]; //Contains string value to write into the file printf("\nThe matrix stored in disk file looks like this:\n\n"); //output while (!feof(fromfile)) { fgets (content_in, 60, fromfile); //Reads up to a next new line printf ("\n%s", content_in); //displays content of text file } fclose(fromfile); //closes file printf("\n\n"); //new lines } void matrix_input() //matrix input function { //variable declarations FILE *tofile; //Variable pointer to a file char name_in[30]; //Contains string value to write into the file int two_d[50][50]; //two dimensional array storing the matrix int n = 0; //number of equations int n_store; //stores the number of equations int i = 1; //counter int j = 0; //counter int save; //buffer variable int calc = 0; //calculates the sum of all numbers in a line int counter = 1; //counter int first = 1; //first element of two_d array (lines) int second = 0; //second element of two_d array (rows) int q = 0; //counter int g = 1; //counter tofile = fopen ("graph_matrix.txt", "w"); //This opens the output file printf("\nPlease input the number of equations you want to process!(e.g.3) "); //prompts user for input scanf("%d", &n); //scans user input fprintf (tofile, "%d", n); //writing user input to the file n_store = n; //sets n_store to n printf("\nNow the numbers for row 1 should be inputted\n"); //output //first line of matrix is built while (i <= n_store) { printf("Please type %d.matrix field(e.g. 3): ", i); //prompts user for input scanf("%s", &name_in); //scans user input save = atoi(name_in); //converts user input to integer two_d[0][second] = save; //saves user input in array i++; //incrementing i second++; //incrementing the row } //resetting counters, lines and rows i = 0; first = 1; second = 0; for (i = 1; i < n; i++) //for-loop incrementing i { printf("\nThe next row starts now!\n"); //output for (j = g; j < n; j++) //for-loop incrementing j { printf("Please type next number(e.g.4): "); //prompts user for input scanf("%s", &name_in); //scans user input save = atoi(name_in); //converts user input to integer two_d[i][j] = save; //saves user input in array } g++; //incremting g } while (first <= n_store) { q = n_store; //setting q to n_store for (int f = first; f > 0; f--) //for-loop decrementing f { two_d[first][f-1] = two_d[f-1][first]; //setting the quadratic matrix (e.g. a12 = a21) second++; //incrementing row } first++; //incrementing line } //output into text file for (int y = 0; y<=(n_store-1);y++) //for-loop incrementing y { calc = 0; //setting calc to 0 fprintf (tofile, "\n"); //writing a new line to the file for (int z = 0; z<=(n_store-1); z++) //for-loop incrementing z { fprintf (tofile, "%d ", two_d[y][z]); //Writing array to file calc = calc + two_d[y][z]; //calculating the sum of a line } fprintf(tofile, "%d", calc); //writing calc to the file } fclose(tofile); //close file n_store = q; //setting n_store to q again printf("\n\n\n"); //printing new lines //output on screen printf("Your matrix now looks like this:\n\n"); //output printf("%d\n", n); //output number of equations for (y = 0; y<=(n_store-1);y++) //for-loop incrementing y { calc = 0; //setting calc to 0 for (int z = 0; z<=(n_store-1); z++) //for-loop incrementing z { printf("%d ", two_d[y][z]); //output array to screen calc = calc + two_d[y][z]; //calculating the sum of a line } printf("%d", calc); //output sum of a line printf("\n"); //new line } printf("\n"); //new line } void matrix_update() //dummy function { printf("\nNot available"); } void matrix_generate() //generates automatically a new matrix of linear equations { int vertex, i, j; double **arry,*length, Lij; FILE *fp; fp = fopen("new_graph_matrix.txt", "w"); //call newstaff.txt file and mode is write mode printf("Input the number of linears ex 4) : "); //print scanf("%d",&vertex); //input number of linears printf("You input %d \n",vertex); //echo arry=(double**)malloc(vertex+2*sizeof(double*));//memory allocate for(i=0;i < vertex;i++) arry[i]=(double *)malloc(vertex+1*sizeof(double));//memory allocate length=(double *)malloc(vertex*sizeof(double));//memory allocate for(i=0;ij) arry[i][j]=arry[j][i]; //must symmetrical else if(i==j) { arry[i][j]=1; //aii must '1' } else { Lij=Lij+length[j-1]; arry[i][j]=0.26*0.52/Lij; //calculate aij } } } arry[vertex-1][vertex]=0; for(i=0;i=0;j--) //for-loop decrementing j { if (matrix[i][c]!=0)//division by 0 not possible { matrix[i][j]=matrix[i][j]/matrix[i][c]; //divide matrix line through row } } } a++; //incrementing a for (j=(amount);j>=0;j--) //for-loop decrementing j { for (i=a;i<(amount);i++) //for-loop incrementing i { if (matrix[i][c]!=0)//if number is 0 { matrix[i][j]=matrix[i][j]-matrix[c][j]; //substract matrix line with next matrix line } } } c++;//next row } while(c<(amount-1)); //do as often as rows exist for (i=c;i=0;j--) //for-loop decrementing j { matrix[i][j]=matrix[i][j]/matrix[i][c]; //divide matrix line by next number } } b=2; for (j=(amount-1);j>0;j--) //for-loop decrementing j { for (i=(amount-b);i>=0;i--) //for-loop decremting i { matrix[i][j]=matrix[i][j]*matrix[j][amount]; //matrix line multiplied with last number of row matrix[i][amount]=matrix[i][amount]-matrix[i][j]; //last number of row equals the difference //between last number of row and matrix line } b++; } printf("\nThe results after solving the matrix are:\n\n"); //output of results on the screen for (i=0;i