Menü Fonksiyonu İle Determinant, Eigenvalue ve Görüntüleme Fonksiyonları Yapımı

mustaphos

MB Üyesi
Kayıt
14 Eylül 2015
Mesajlar
34
Tepkiler
7
Yaş
24
Meslek
Öğrenci
Üniv
Anadolu University
Merhaba.
Bu programda fonksiyon yapısı, switch ve matematiksel hesaplamalar konusuna değindim.
Program bir değer ister.
1 ise 3x3 matrisin determinantını bulur.
2 ise 2x2 matrisin eigenvalue'sunu bulur.
3 ise girilen matrisi düzenli olarak görüntüler.
İyi çalışmalar.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void menu_function(); // Function prototype
void determinant_function(); // Function prototype
void eigenvalue_function(); // Function prototype
void display_function(); // Function prototype

int menu_value; // Menu option variable : 1,2,3,4
int i; // Row counter
int j; // Column counter
int row; // For get row number from user
int column; // For get column number from user

char yes_or_no; // For asking continue to program, the variable to ask the user y for yes or n for no

float determinant[3][3]; // Define 3x3 matrix
float calculate_determinant; // A floating point variable to calculate determinant
float eigenvalue[2][2]; // Define 2x2 matrix
float display[100][100]; // Define 100x100 matrix

double calculate_eigenvalue; // Double variable to calculate eigenvalue without loss of data
double calculate_second_eigenvalue; // Double variable to calculate eigenvalue without loss of data

void main()
{
menu_function(); // Call menu_function function
}

void menu_function ()
{
yes_or_no='y'; // Define yer_or_no variable y to get into the while loop
while (yes_or_no=='y') // If user wants to continiue to the program
{
printf("Type [ 1 ] and press [ ENTER ] for find determinant of 3x3 matrix \n");
printf("Type [ 2 ] and press [ ENTER ] for find eigenvalue of 2x2 matrix \n");
printf("Type [ 3 ] and press [ ENTER ] for display the entered matrix \n");
printf("Type [ 4 ] and press [ ENTER ] exit the program \n");
printf(">>> ");
scanf("%d",&menu_value); // Get 1,2,3 or 4 from user
switch(menu_value)
{
case 1:
{
determinant_function(); // If user pressed 1 then call determinant_function function
break;
}
case 2:
{
eigenvalue_function(); // If user pressed 2 then call eigenvalue_function function
break;
}
case 3:
{
display_function(); // If user pressed 3 then call display_function function
break;
}
case 4:
{
printf("Exiting the program\n");
exit(0); // This function stops the program
}
}
yes_or_no='n'; // Define yes_or_no value n for ask continue or not
printf("Continue the program ? [ y ] for yes or [ n ] for no\n");
printf(">>> ");
scanf("%s",&yes_or_no); // Get the y value for continiue the program
}
}

void determinant_function ()
{
printf("a b c \n");
printf("d e f \n");
printf("g h i \n");
printf("Please write 9 components of 3x3 matrix to calculate determinant of this matrix\n");
printf("The order of components must be a,b,c,d,e,f,g,h,i\n");
for (i=0;i<3;i++) // For loop for scan rows
{
for (j=0;j<3;j++) // For loop for scan columns
{
printf(">>> ");
scanf("%f",&determinant[j]); // Scan the values
}
}
printf("The matrix is : \n");
for (i=0;i<3;i++) // For loop for rows
{
for (j=0;j<3;j++) // For loop for columns
{
printf("%f ",determinant[j]); // Print the components
}
printf("\n"); // Go to next row
}
calculate_determinant=determinant[0][0]*(determinant[1][1]*determinant[2][2]-determinant[2][1]*determinant[1][2])-determinant[0][1]*(determinant[1][0]*determinant[2][2]-determinant[2][0]*determinant[1][2])+determinant[0][2]*(determinant[1][0]*determinant[2][1]-determinant[1][1]*determinant[2][0]); // Calculate determinant
printf("Determinant of this matrix is : %f\n",calculate_determinant); // Print determinant
}

void eigenvalue_function ()
{
printf("a b \n");
printf("c d \n");
printf("Please write 4 components of 2x2 matrix to calculate eigenvalue of this matrix\n");
printf("The order of components must be a,b,c,d \n");
for (i=0;i<2;i++) // For loop for scan rows
{
for (j=0;j<2;j++) // For loop for scan columns
{
printf(">>> ");
scanf("%f",&eigenvalue[j]); // Scan the values
}
}
printf("The matrix is : \n");
for (i=0;i<2;i++) // For loop for rows
{
for (j=0;j<2;j++) // For loop for columns
{
printf("%f ",eigenvalue[j]); // Print the components
}
printf("\n"); // Go to next row
}
calculate_eigenvalue = (eigenvalue[0][0]+eigenvalue[1][1]+sqrt((eigenvalue[0][0]+eigenvalue[1][1])*(eigenvalue[0][0]+eigenvalue[1][1])-4*(eigenvalue[0][0]*eigenvalue[1][1]-eigenvalue[0][1]*eigenvalue[1][0])))/(2); // Calculate first eigen value
calculate_second_eigenvalue = (eigenvalue[0][0]+eigenvalue[1][1]-sqrt((eigenvalue[0][0]+eigenvalue[1][1])*(eigenvalue[0][0]+eigenvalue[1][1])-4*(eigenvalue[0][0]*eigenvalue[1][1]-eigenvalue[0][1]*eigenvalue[1][0])))/(2); // Calculate second eigen value
printf("Eigenvalues of this matrix are [ %f ] and [ %f ] \n",calculate_eigenvalue,calculate_second_eigenvalue); // Print eigen values
}

void display_function()
{
printf("Please write row number of the matrix for display : \n");
printf(">>> ");
scanf("%d",&row);
printf("Please write column number of the matrix for display : \n");
printf(">>> ");
scanf("%d",&column);
printf("Please write the components : \n");
for(i=0;i<row;i++) // For loop for scan rows
{
for(j=0;j<column;j++) // For loop for scan columns
{
printf(">>> ");
scanf("%f",&display[j]); // Scan the values
}
}
for(i=0;i<row;i++) // For loop for rows
{
for(j=0;j<column;j++) // For loop for columns
{
printf("%f ",display[j]); // Print the components
}
printf("\n");
}
}
 
Yukarı Alt