Advertisement
Waliul

Sifat's Assignment

Jun 25th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef struct date
  4. {
  5. // DO NOT MAKE ANY CHANGE HERE
  6.     int day;
  7.     int month;
  8.     int year;
  9. } DATE;
  10.  
  11. typedef struct person
  12. {
  13. // DO NOT MAKE ANY CHANGE HERE
  14.     char first_name[20];
  15.     char last_name[20];
  16.     DATE birth_date;
  17.     long int nid;
  18.     char address[200];
  19. } PERSON;
  20.  
  21. void print_options()
  22. {
  23. // DO NOT MAKE ANY CHANGE HERE
  24.     printf("************­********************­***********\n");
  25.     printf("1: Calculating multiplication using recursion\n");
  26.     printf("2: Testing symmetricity for a matrix\n");
  27.     printf("3: Finding frequency of letters in a line\n");
  28.     printf("4: Storing a person's information in a structure\n");
  29.     printf("q: quit\n");
  30.     printf("************­********************­***********\n");
  31.     printf("Enter a option: ");
  32. }
  33.  
  34. int recursion_task()
  35. {
  36.     int x,y;
  37.     printf("\nEnter the first number: ");
  38.     scanf("%d", &x);
  39.     printf("Enter the second number: ");
  40.     scanf("%d", &y);
  41.     printf("Result is: %d", result(x,y));
  42.     return 0;
  43. }
  44. int result (int n1,int n2)
  45. {
  46.     if(n2==0)
  47.         return 0;
  48.     if(n2>0)
  49.         return (n1+result(n1,n2-1));
  50.     if(n2<0)
  51.         return -result(n1,-n2);
  52. }
  53. void array_task()
  54. {
  55.     int n, i, j, k;
  56.     printf("Enter dimension of the matrix: ");
  57.     scanf("%d", &n);
  58.     int arr[n][n];
  59.     printf("Enter the matrix:\n");
  60.     for(i = 0; i < n; i++)
  61.         for(j = 0; j < n; j++)
  62.             scanf("%d", &arr[i][j]);
  63.     int sym = 0;
  64.     for(i = 0; i < n; i++)
  65.     {
  66.         for(j = 0; j < n; j++)
  67.         {
  68.             if(arr[i][j] == -arr[j][i])
  69.                 sym = -1;
  70.             else if(arr[i][j] != arr[j][i])
  71.                 sym = 1;
  72.  
  73.         }
  74.     }
  75.     if(!sym)
  76.         printf("Symmetric matrix\n");
  77.     else if(sym == -1)
  78.         printf("Skew-symmetric matrix\n");
  79.     else
  80.         printf("Asymmetric matrix\n");
  81. }
  82.  
  83. void string_task()
  84. {
  85.     char str[1000];
  86.     printf("Enter the line: ");
  87.     gets(str);
  88.     int len = strlen(str);
  89.     int occ[30];
  90.     for(int i = 0; i < 30; i++)
  91.         occ[i] = 0;
  92.     for(int i = 0; i < len; i++)
  93.     {
  94.         int id = str[i] - 'A';
  95.         occ[id]++;
  96.     }
  97.     for(int i = 0; i < 26; i++)
  98.     {
  99.         printf("%c\t%d\t\t", 'A'+i, occ[i]);
  100.     }
  101. }
  102.  
  103. void structure_task()
  104. {
  105.     PERSON temp;
  106.     printf("Provide citizen Information....\n\n");
  107.     printf("First Name: ");
  108.     scanf("%s", temp.first_name);
  109.     printf("Last Name: ");
  110.     scanf("%s", temp.last_name);
  111.     printf("\nDate of Birth...\n\n");
  112.     printf("Date: ");
  113.     scanf("%d", &temp.birth_date.day);
  114.     printf("Month: ");
  115.     scanf("%d", &temp.birth_date.month);
  116.     printf("Year: ");
  117.     scanf("%d", &temp.birth_date.year);
  118.     printf("\nNID: ");
  119.     scanf("%lld", &temp.nid);
  120.     printf("\nAddress: ");
  121.     gets(temp.address);
  122.     gets(temp.address);
  123.     printf("**************************************\n");
  124.     printf("%s %s\n", temp.first_name, temp.last_name);
  125.     printf("Date of Birth: %d-%d-%d\n", temp.birth_date.day, temp.birth_date.month, temp.birth_date.year);
  126.     int cnt1 = 0, cnt2 = 0, temp1, temp2;
  127.     temp1 = temp.birth_date.day, temp2 = temp.birth_date.month;
  128.     while(temp1)
  129.     {
  130.         cnt1++;
  131.         temp1 /= 10;
  132.     }
  133.     while(temp2)
  134.     {
  135.         cnt2++;
  136.         temp2 /= 10;
  137.     }
  138.     printf("NID: %d ", temp.birth_date.year);
  139.     if(cnt2 == 1)
  140.         printf("0");
  141.     printf("%d", temp.birth_date.month);
  142.     if(cnt1 == 1)
  143.         printf("0");
  144.     long long int id = temp.nid;
  145.     printf("%d %I64d\n", temp.birth_date.day, id);
  146.     printf("Address: %s\n", temp.address);
  147.     printf("**************************************\n");
  148. }
  149.  
  150.  
  151. int main()
  152. {
  153. // DO NOT MAKE ANY CHANGE HERE
  154.     char op,c;
  155.     do
  156.     {
  157.         print_options();
  158.         fflush(stdin); //If we don't clear the standard input, we will keep getting '\n' after the first operation in op which will cause the wrong operation
  159.         scanf("%c%c", &op,&c);
  160.         switch(op)
  161.         {
  162.         case '1':
  163.             recursion_task();
  164.             break;
  165.         case '2':
  166.             array_task();
  167.             break;
  168.         case '3':
  169.             string_task();
  170.             break;
  171.         case '4':
  172.             structure_task();
  173.             break;
  174.         case 'q':  //for not showing default in case of quit
  175.             break;
  176.         case 'Q':  //for not showing default in case of quit
  177.             break;
  178.         default:
  179.             printf("\nEnter a valid option\n");
  180.             break;
  181.  
  182.         }
  183.         printf("\n\n");
  184.     }
  185.     while(op!='q' && op!='Q');
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement