Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include<stdlib.h>
- #define M 3
- void my_fill(int array1[M][M])
- {
- // -----func: my_fill-----
- //Receives every time one of the two dimensional
- //arrays and fills it by the absorption of real
- //numbers only between 18 and 75.
- int i,j;
- printf("\n\n");
- for(i = 0;i < M;i++)//runs on the rows
- {
- for(j = 0;j < M;j++)//runs on the colums
- {
- printf("Enter number between 18 to 75 include\n");
- scanf("%d",&array1[i][j]);// gets number from the user
- while ((array1[i][j] < 18) || (array1[i][j] > 75))// loop for cheaking if the number is less then 18 or more then 75 then
- {
- printf("wrong number , try again\n");//print's suitable message
- printf("Enter number between 18 to 75 include\n");//
- scanf("%d",&array1[i][j]);//get number from the user
- }
- }
- }
- }
- void my_print(int matrix[M][M])
- {
- // -----func: my_print-----
- //prints the the array that was filled in my_fill
- int i,j;
- printf("\n\n\n");
- for(i = 0;i < M;i++)//runs on the rows
- {
- for(j = 0;j < M;j++)//runs on the colums
- { //
- printf(" %d",matrix[i][j]); //for correct print of the numbers of the array //
- }
- printf("\n");
- }
- printf("\n\n");
- }
- int magic_board(int matrix[M][M])
- {
- int i,j,sum_row[M] = {0},sum_col[M] = {0},sum_main = 0,sum_secondry = 0,row_col_equal = 0;
- for(i = 0;i < M;i++)//runs on the rows
- {
- for(j = 0;j < M;j++)//runs on the colums
- {
- sum_row[i] += matrix[i][j];
- sum_col[i] += matrix[j][i];
- if (i == j)//if main diagonal
- sum_main += matrix[i][j];//sums the main diagonal
- if ((i + j) == (M-1))//if secondry diagonal
- sum_secondry += matrix[i][j];//sums the secondry diagonal
- }
- }
- for (i = 0;i < M-1;i++)
- {
- if ((sum_row[i] == sum_row[i+1]) && (sum_col[i] == sum_col[i+1]))
- row_col_equal++;
- }
- if((sum_row[0] == sum_col[0]) && (sum_main == sum_secondry) && (sum_main == sum_row[0]) && (sum_secondry == sum_col[0]) && (row_col_equal == M-1))
- return 1;
- else
- return 0;
- }
- int *build_array(int n)
- {
- int *new_array,i,num;
- new_array = (int*)malloc(n*sizeof(int));
- if (new_array == NULL)
- {
- printf("Memory faild\n");
- exit(1);
- }
- for (i = 0;i < n;)
- {
- printf("Enter number between -30 to 30\n");
- scanf("%d",&num);
- if ((num <= 30) && (num >= -30))
- new_array[i] = num;
- i++;
- }
- return new_array;
- }
- void main ()
- {
- int matrix[M][M],num_menu = 0,return_resualt,array_size,*a,i;
- while (num_menu != 6)//loop that keep the menu running till the user press 5
- {
- printf("Choice function:\n\n1 - Fill array function\n2 - Print array function\n3 - Magic Board function\n4 - Build Array function\n5 - Checking function\n6 - Exit \n\n");//menu
- scanf("%d",&num_menu);//get users choise
- while ((num_menu < 1) || (num_menu > 6))//loop that print wrong choise if press wrong number in the menu < 1 or > 6
- {
- printf("Wrong choice - Try again\n\n");
- printf("Choice function:\n\n1 - Fill array function\n2 - Print array function\n3 - Magic Board function\n4 - Build Array function\n5 - Checking function\n6 - Exit \n\n");//menu
- scanf("%d",&num_menu);
- }
- switch (num_menu)
- {
- case 1:
- my_fill(matrix);
- break;
- case 2:
- my_print(matrix);
- break;
- case 3:
- return_resualt = magic_board(matrix);
- printf("\n");
- if (return_resualt == 1)
- printf("The array is Magic Board\n\n");
- else
- printf("The array is NOT Magic Board\n\n");
- break;
- case 4:
- printf("Enter a positive number for size of the array.\n");
- scanf("%d",&array_size);
- a= build_array(array_size);
- for (i = 0;i < array_size;i++)
- {
- printf(" %d",a[i]);
- }
- break;
- case 5:
- break;
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement