Advertisement
davsank

Untitled

Apr 24th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include<stdlib.h>
  4. #define M 3
  5.  
  6.  
  7.  
  8. void my_fill(int array1[M][M])
  9. {
  10.  
  11.     //          -----func: my_fill-----
  12.     //Receives every time one of the two dimensional
  13.     //arrays and fills it by the absorption of real
  14.     //numbers only between 18 and 75.
  15.  
  16.     int i,j;
  17.     printf("\n\n");
  18.  
  19.     for(i = 0;i < M;i++)//runs on the rows
  20.     {
  21.         for(j = 0;j < M;j++)//runs on the colums
  22.         {
  23.             printf("Enter number between 18 to 75 include\n");
  24.             scanf("%d",&array1[i][j]);// gets number from the user
  25.             while ((array1[i][j] < 18) || (array1[i][j] > 75))// loop for cheaking if the number is less then 18 or more then 75 then
  26.             {
  27.                 printf("wrong number , try again\n");//print's suitable message
  28.                 printf("Enter number between 18 to 75 include\n");//
  29.                 scanf("%d",&array1[i][j]);//get number from the user                                      
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. void my_print(int matrix[M][M])
  37. {
  38.  
  39.     //           -----func: my_print-----
  40.     //prints the the array that was filled in my_fill
  41.  
  42.     int i,j;
  43.     printf("\n\n\n");
  44.     for(i = 0;i < M;i++)//runs on the rows
  45.     {
  46.         for(j = 0;j < M;j++)//runs on the colums
  47.         {                                                             //
  48.             printf(" %d",matrix[i][j]);                          //for correct print of the numbers of the array                                                         //
  49.         }
  50.         printf("\n");
  51.     }
  52.     printf("\n\n");
  53. }
  54.  
  55.  
  56. int magic_board(int matrix[M][M])
  57. {
  58.     int i,j,sum_row[M] = {0},sum_col[M] = {0},sum_main = 0,sum_secondry = 0,row_col_equal = 0;
  59.  
  60.     for(i = 0;i < M;i++)//runs on the rows
  61.     {
  62.         for(j = 0;j < M;j++)//runs on the colums
  63.         {
  64.             sum_row[i] += matrix[i][j];
  65.             sum_col[i] += matrix[j][i];
  66.             if (i == j)//if main diagonal
  67.                 sum_main += matrix[i][j];//sums the main diagonal
  68.  
  69.             if ((i  + j) == (M-1))//if secondry diagonal
  70.                 sum_secondry += matrix[i][j];//sums the secondry diagonal
  71.  
  72.         }
  73.  
  74.     }
  75.     for (i = 0;i < M-1;i++)
  76.     {
  77.         if ((sum_row[i] == sum_row[i+1]) && (sum_col[i] == sum_col[i+1]))
  78.  
  79.             row_col_equal++;
  80.  
  81.     }
  82.  
  83.     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))
  84.         return 1;
  85.     else
  86.         return 0;
  87.  
  88. }
  89.  
  90.  
  91. int *build_array(int n)
  92. {
  93.     int *new_array,i,num;
  94.  
  95.     new_array = (int*)malloc(n*sizeof(int));
  96.  
  97.         if (new_array == NULL)
  98.         {
  99.             printf("Memory faild\n");
  100.             exit(1);
  101.         }
  102.         for (i = 0;i < n;)
  103.         {
  104.             printf("Enter number between -30 to 30\n");
  105.             scanf("%d",&num);
  106.             if ((num <= 30) && (num >= -30))
  107.                 new_array[i] = num;
  108.                 i++;
  109.         }
  110.  
  111.         return new_array;
  112.  
  113. }
  114.  
  115.  
  116. void main ()
  117. {
  118.     int matrix[M][M],num_menu = 0,return_resualt,array_size,*a,i;
  119.  
  120.  
  121.     while (num_menu != 6)//loop that keep the menu running till the user press 5
  122.     {
  123.         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
  124.         scanf("%d",&num_menu);//get users choise
  125.  
  126.         while ((num_menu < 1) || (num_menu > 6))//loop that print wrong choise if press wrong number in the menu < 1 or > 6
  127.         {
  128.             printf("Wrong choice - Try again\n\n");
  129.             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
  130.             scanf("%d",&num_menu);
  131.         }
  132.         switch (num_menu)
  133.         {
  134.  
  135.         case 1:
  136.             my_fill(matrix);
  137.             break;
  138.  
  139.         case 2:
  140.             my_print(matrix);
  141.             break;
  142.  
  143.         case 3:
  144.             return_resualt = magic_board(matrix);
  145.             printf("\n");
  146.             if (return_resualt == 1)
  147.                 printf("The array is Magic Board\n\n");
  148.             else
  149.                 printf("The array is NOT Magic Board\n\n");
  150.  
  151.             break;
  152.  
  153.         case 4:
  154.             printf("Enter a positive number for size of the array.\n");
  155.             scanf("%d",&array_size);
  156.             a= build_array(array_size);
  157.             for (i = 0;i < array_size;i++)
  158.             {
  159.                 printf("  %d",a[i]);
  160.             }
  161.  
  162.             break;
  163.  
  164.         case 5:
  165.             break;
  166.  
  167.         }
  168.         printf("\n");
  169.  
  170.  
  171.  
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement