Advertisement
Guest User

kursova rabota katerina

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[]) {
  5.     int i = 0;
  6.     int nums [100];
  7.     int array1 [100];
  8.     int array2 [100];
  9.     int array1Index = 0;
  10.     int array2Index = 0;
  11.    
  12.     //choose path for write all data from console
  13.     //
  14.     printf("Enter path for file where program will write the data from console: ");
  15.     char filePath [50];
  16.     scanf("%s", filePath);
  17.     FILE *fptrM;
  18.        
  19.     if (fptrM == NULL){
  20.         fprintf(fptrM,"Error Reading File\n");
  21.         printf("Error Reading File\n");
  22.         exit (0);
  23.     }
  24.        
  25.     strncat(filePath, ".txt", 4);
  26.        
  27.     fptrM = (fopen(filePath, "w"));
  28.    
  29.    
  30.    
  31.     //Menu with options for reading array:
  32.     // a) from console
  33.     // b) from text file
  34.     //
  35.    
  36.     char option;
  37.     printf("//type \"a\" to import data from console\n");
  38.     fprintf(fptrM, "//type \"a\" to import data from console\n");
  39.     printf("//type \"b\" to import data from text file\n");
  40.     fprintf(fptrM, "//type \"b\" to import data from text file\n");
  41.     printf("How you want to import data for array? ");
  42.     fprintf(fptrM, "How you want to import data for array? ");
  43.     scanf(" %c", &option);
  44.     if(option == 'a'){
  45.         for(i = 0; i < 100; i = i+1){
  46.             fprintf(fptrM, "Enter value for %d element:", i + 1);
  47.             printf("Enter value for %d element:", i + 1);
  48.             scanf("%d", &nums[i]);
  49.         }
  50.     }
  51.    
  52.     if(option == 'b'){
  53.         char filePath [50];
  54.         fprintf(fptrM, "Enter path for array load: ");
  55.         printf("Enter path for array load: ");
  56.        
  57.         scanf("%s", filePath);
  58.         FILE *fptr;
  59.        
  60.         if (fptr == NULL){
  61.             fprintf(fptrM, "Error Reading File\n");
  62.             printf("Error Reading File\n");
  63.             exit (0);
  64.         }
  65.        
  66.         strncat(filePath, ".txt", 4);
  67.        
  68.         fptr = (fopen(filePath, "a+"));
  69.        
  70.         for(i = 0; i < 100; i = i + 1){
  71.             fscanf(fptr, "%d,", &nums[i]);
  72.         }
  73.        
  74.         fclose(fptr);
  75.     }
  76.    
  77.     fprintf(fptrM, "\n\n---------------------\n\n");
  78.     printf("\n\n---------------------\n\n");
  79.    
  80.    
  81.    
  82.     //Separate numbers in 2 arrays and display all numbers from the first array
  83.     //
  84.    
  85.     for(i = 0; i < 100; i = i+1){
  86.         if(nums[i] < 0 && nums[i] % 3 == 0){
  87.             array1[array1Index] = nums[i];
  88.             array1Index = array1Index + 1;
  89.         } else if (nums[i] > 0 && nums[i] % 7 == 0){
  90.             array2[array2Index] = nums[i];
  91.             array2Index = array2Index + 1;
  92.         }
  93.         fprintf(fptrM, "Value for %d element is: %d\n", i + 1, nums[i]);
  94.         printf("Value for %d element is: %d\n", i + 1, nums[i]);
  95.     }
  96.    
  97.     fprintf(fptrM, "\n\n---------------------\n\n");
  98.     printf("\n\n---------------------\n\n");
  99.    
  100.     //Option menu asking for saving array in file
  101.     // y for yes
  102.     //n for no
  103.     //
  104.    
  105.     fprintf(fptrM, "Do you want to save this array in new file? ");
  106.     printf("Do you want to save this array in new file? ");
  107.     char opt;
  108.     scanf(" %c", &opt);
  109.     if(opt == 'y'){
  110.         char filePath [50];
  111.         fprintf(fptrM, "Enter path to save array: ");
  112.         printf("Enter path to save array: ");
  113.        
  114.         scanf("%s", filePath);
  115.         FILE *fptr;
  116.        
  117.         if (fptr == NULL){
  118.             fprintf(fptrM, "Error Reading File\n");
  119.             printf("Error Reading File\n");
  120.             exit (0);
  121.         }
  122.        
  123.         strncat(filePath, ".txt", 4);
  124.        
  125.         fptr = (fopen(filePath, "w"));
  126.        
  127.         for(i = 0; i < 100; i = i + 1){
  128.             fprintf(fptr,"%d ", nums[i]);
  129.             if((i + 1) % 3 == 0){
  130.                 fprintf(fptr,"\n");
  131.             }
  132.         }
  133.        
  134.         fclose(fptr);
  135.        
  136.         fprintf(fptrM, "\n\n---------------------\n\n");
  137.         printf("\n\n---------------------\n\n");
  138.     }
  139.    
  140.    
  141.    
  142.     //Display all numbers from 1-st array
  143.     //
  144.    
  145.     for(i = 0; i < array1Index; i = i+1){
  146.         fprintf(fptrM, "Array 1 => %d value: %d \n", i + 1, array1[i]);
  147.         printf("Array 1 => %d value: %d \n", i + 1, array1[i]);
  148.     }
  149.     fprintf(fptrM, "\n\n---------------------\n\n");
  150.     printf("\n\n---------------------\n\n");
  151.    
  152.    
  153.    
  154.     //Display all numbers from 2-nd array
  155.     //
  156.    
  157.     for(i = 0; i < array2Index; i = i+1){      
  158.         fprintf(fptrM, "Array 2 => %d value: %d \n", i + 1, array2[i]);
  159.         printf("Array 2 => %d value: %d \n", i + 1, array2[i]);
  160.     }
  161.    
  162.     fprintf(fptrM, "\n\n---------------------\n\n");
  163.     printf("\n\n---------------------\n\n");
  164.    
  165.    
  166.    
  167.     //Find the most repetitive number in first array
  168.     //
  169.    
  170.     int seen [100];
  171.     int mostRepetitiveNum = array1[0];
  172.     int mostRepetitiveNumCount = 1;
  173.     for(i = 0; i < array1Index; i = i+1){
  174.         seen[i] = 0;
  175.     }
  176.    
  177.     for(i = 0; i < array1Index; i++) {
  178.         if(seen[i] == 0) {
  179.             int count = 0;
  180.             int j;
  181.             for(j = i; j < array1Index; j++)
  182.                 if(array1[j] == array1[i]) {
  183.                     count += 1;
  184.                     seen[j] = 1;
  185.                 }
  186.             if(count > mostRepetitiveNumCount){
  187.                 mostRepetitiveNumCount = count;
  188.                 mostRepetitiveNum = array1[i];
  189.             }
  190.         }
  191.     }
  192.    
  193.     fprintf(fptrM, "Most repetitive number in array1 is %d => %d times\n",
  194.     mostRepetitiveNum,
  195.     mostRepetitiveNumCount);
  196.     printf("Most repetitive number in array1 is %d => %d times\n",
  197.     mostRepetitiveNum,
  198.     mostRepetitiveNumCount);
  199.    
  200.     fprintf(fptrM, "\n\n---------------------\n\n");
  201.     printf("\n\n---------------------\n\n");
  202.    
  203.    
  204.    
  205.    
  206.     //Find the most repetitive number in second array
  207.     //
  208.    
  209.     mostRepetitiveNum = array2[0];
  210.     mostRepetitiveNumCount = 1;
  211.     for(i = 0; i < array2Index; i = i+1){
  212.         seen[i] = 0;
  213.     }
  214.    
  215.     for(i = 0; i < array2Index; i++) {
  216.         if(seen[i] == 0) {
  217.             int count = 0;
  218.             int j;
  219.             for(j = i; j < array2Index; j++)
  220.                 if(array2[j] == array2[i]) {
  221.                     count += 1;
  222.                     seen[j] = 1;
  223.                 }
  224.             if(count > mostRepetitiveNumCount){
  225.                 mostRepetitiveNumCount = count;
  226.                 mostRepetitiveNum = array2[i];
  227.             }
  228.         }
  229.     }
  230.    
  231.    
  232.     fprintf(fptrM, "Most repetitive number in array2 is %d => %d times\n",
  233.     mostRepetitiveNum,
  234.     mostRepetitiveNumCount);
  235.     printf("Most repetitive number in array2 is %d => %d times\n",
  236.     mostRepetitiveNum,
  237.     mostRepetitiveNumCount);
  238.    
  239.     fprintf(fptrM, "\n\n---------------------\n\n");
  240.     printf("\n\n---------------------\n\n");
  241.    
  242.     //sort arrays and save them in another files
  243.     //
  244.     int c, swap, d;
  245.     for (c = 0 ; c < array1Index - 1; c++)
  246.     {
  247.         for (d = 0 ; d < array1Index - c - 1; d++)
  248.         {
  249.             if (array1[d] > array1[d+1])
  250.             {
  251.                 swap = array1[d];
  252.                 array1[d] = array1[d+1];
  253.                 array1[d+1] = swap;
  254.             }
  255.         }
  256.     }
  257.    
  258.    
  259.     for (c = 0 ; c < array2Index - 1; c++)
  260.     {
  261.         for (d = 0 ; d < array2Index - c - 1; d++)
  262.         {
  263.             if (array2[d] > array2[d+1])
  264.             {
  265.                 swap = array2[d];
  266.                 array2[d] = array2[d+1];
  267.                 array2[d+1] = swap;
  268.             }
  269.         }
  270.     }
  271.    
  272.     FILE *fptr1;
  273.        
  274.     if (fptr1 == NULL){
  275.         fprintf(fptrM,"Error Reading File\n");
  276.         printf("Error Reading File\n");
  277.         exit (0);
  278.     }
  279.    
  280.     fptr1 = (fopen("sortedArray1.txt", "w"));
  281.        
  282.     for(i = 0; i < array1Index; i = i + 1){
  283.         fprintf(fptr1,"%d\n", nums[i]);
  284.     }
  285.        
  286.     fclose(fptr1);
  287.  
  288.    
  289.     FILE *fptr2;
  290.        
  291.     if (fptr2 == NULL){
  292.         fprintf(fptrM,"Error Reading File\n");
  293.         printf("Error Reading File\n");
  294.         exit (0);
  295.     }
  296.    
  297.     fptr2 = (fopen("sortedArray2.txt", "w"));
  298.        
  299.     for(i = 0; i < array2Index; i = i + 1){
  300.         fprintf(fptr2, "%d\n", array2[i]);
  301.     }
  302.        
  303.     fclose(fptr2);
  304.  
  305.     fclose(fptrM);
  306.     return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement