Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[]) {
- int i = 0;
- int nums [100];
- int array1 [100];
- int array2 [100];
- int array1Index = 0;
- int array2Index = 0;
- //choose path for write all data from console
- //
- printf("Enter path for file where program will write the data from console: ");
- char filePath [50];
- scanf("%s", filePath);
- FILE *fptrM;
- if (fptrM == NULL){
- fprintf(fptrM,"Error Reading File\n");
- printf("Error Reading File\n");
- exit (0);
- }
- strncat(filePath, ".txt", 4);
- fptrM = (fopen(filePath, "w"));
- //Menu with options for reading array:
- // a) from console
- // b) from text file
- //
- char option;
- printf("//type \"a\" to import data from console\n");
- fprintf(fptrM, "//type \"a\" to import data from console\n");
- printf("//type \"b\" to import data from text file\n");
- fprintf(fptrM, "//type \"b\" to import data from text file\n");
- printf("How you want to import data for array? ");
- fprintf(fptrM, "How you want to import data for array? ");
- scanf(" %c", &option);
- if(option == 'a'){
- for(i = 0; i < 100; i = i+1){
- fprintf(fptrM, "Enter value for %d element:", i + 1);
- printf("Enter value for %d element:", i + 1);
- scanf("%d", &nums[i]);
- }
- }
- if(option == 'b'){
- char filePath [50];
- fprintf(fptrM, "Enter path for array load: ");
- printf("Enter path for array load: ");
- scanf("%s", filePath);
- FILE *fptr;
- if (fptr == NULL){
- fprintf(fptrM, "Error Reading File\n");
- printf("Error Reading File\n");
- exit (0);
- }
- strncat(filePath, ".txt", 4);
- fptr = (fopen(filePath, "a+"));
- for(i = 0; i < 100; i = i + 1){
- fscanf(fptr, "%d,", &nums[i]);
- }
- fclose(fptr);
- }
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //Separate numbers in 2 arrays and display all numbers from the first array
- //
- for(i = 0; i < 100; i = i+1){
- if(nums[i] < 0 && nums[i] % 3 == 0){
- array1[array1Index] = nums[i];
- array1Index = array1Index + 1;
- } else if (nums[i] > 0 && nums[i] % 7 == 0){
- array2[array2Index] = nums[i];
- array2Index = array2Index + 1;
- }
- fprintf(fptrM, "Value for %d element is: %d\n", i + 1, nums[i]);
- printf("Value for %d element is: %d\n", i + 1, nums[i]);
- }
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //Option menu asking for saving array in file
- // y for yes
- //n for no
- //
- fprintf(fptrM, "Do you want to save this array in new file? ");
- printf("Do you want to save this array in new file? ");
- char opt;
- scanf(" %c", &opt);
- if(opt == 'y'){
- char filePath [50];
- fprintf(fptrM, "Enter path to save array: ");
- printf("Enter path to save array: ");
- scanf("%s", filePath);
- FILE *fptr;
- if (fptr == NULL){
- fprintf(fptrM, "Error Reading File\n");
- printf("Error Reading File\n");
- exit (0);
- }
- strncat(filePath, ".txt", 4);
- fptr = (fopen(filePath, "w"));
- for(i = 0; i < 100; i = i + 1){
- fprintf(fptr,"%d ", nums[i]);
- if((i + 1) % 3 == 0){
- fprintf(fptr,"\n");
- }
- }
- fclose(fptr);
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- }
- //Display all numbers from 1-st array
- //
- for(i = 0; i < array1Index; i = i+1){
- fprintf(fptrM, "Array 1 => %d value: %d \n", i + 1, array1[i]);
- printf("Array 1 => %d value: %d \n", i + 1, array1[i]);
- }
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //Display all numbers from 2-nd array
- //
- for(i = 0; i < array2Index; i = i+1){
- fprintf(fptrM, "Array 2 => %d value: %d \n", i + 1, array2[i]);
- printf("Array 2 => %d value: %d \n", i + 1, array2[i]);
- }
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //Find the most repetitive number in first array
- //
- int seen [100];
- int mostRepetitiveNum = array1[0];
- int mostRepetitiveNumCount = 1;
- for(i = 0; i < array1Index; i = i+1){
- seen[i] = 0;
- }
- for(i = 0; i < array1Index; i++) {
- if(seen[i] == 0) {
- int count = 0;
- int j;
- for(j = i; j < array1Index; j++)
- if(array1[j] == array1[i]) {
- count += 1;
- seen[j] = 1;
- }
- if(count > mostRepetitiveNumCount){
- mostRepetitiveNumCount = count;
- mostRepetitiveNum = array1[i];
- }
- }
- }
- fprintf(fptrM, "Most repetitive number in array1 is %d => %d times\n",
- mostRepetitiveNum,
- mostRepetitiveNumCount);
- printf("Most repetitive number in array1 is %d => %d times\n",
- mostRepetitiveNum,
- mostRepetitiveNumCount);
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //Find the most repetitive number in second array
- //
- mostRepetitiveNum = array2[0];
- mostRepetitiveNumCount = 1;
- for(i = 0; i < array2Index; i = i+1){
- seen[i] = 0;
- }
- for(i = 0; i < array2Index; i++) {
- if(seen[i] == 0) {
- int count = 0;
- int j;
- for(j = i; j < array2Index; j++)
- if(array2[j] == array2[i]) {
- count += 1;
- seen[j] = 1;
- }
- if(count > mostRepetitiveNumCount){
- mostRepetitiveNumCount = count;
- mostRepetitiveNum = array2[i];
- }
- }
- }
- fprintf(fptrM, "Most repetitive number in array2 is %d => %d times\n",
- mostRepetitiveNum,
- mostRepetitiveNumCount);
- printf("Most repetitive number in array2 is %d => %d times\n",
- mostRepetitiveNum,
- mostRepetitiveNumCount);
- fprintf(fptrM, "\n\n---------------------\n\n");
- printf("\n\n---------------------\n\n");
- //sort arrays and save them in another files
- //
- int c, swap, d;
- for (c = 0 ; c < array1Index - 1; c++)
- {
- for (d = 0 ; d < array1Index - c - 1; d++)
- {
- if (array1[d] > array1[d+1])
- {
- swap = array1[d];
- array1[d] = array1[d+1];
- array1[d+1] = swap;
- }
- }
- }
- for (c = 0 ; c < array2Index - 1; c++)
- {
- for (d = 0 ; d < array2Index - c - 1; d++)
- {
- if (array2[d] > array2[d+1])
- {
- swap = array2[d];
- array2[d] = array2[d+1];
- array2[d+1] = swap;
- }
- }
- }
- FILE *fptr1;
- if (fptr1 == NULL){
- fprintf(fptrM,"Error Reading File\n");
- printf("Error Reading File\n");
- exit (0);
- }
- fptr1 = (fopen("sortedArray1.txt", "w"));
- for(i = 0; i < array1Index; i = i + 1){
- fprintf(fptr1,"%d\n", nums[i]);
- }
- fclose(fptr1);
- FILE *fptr2;
- if (fptr2 == NULL){
- fprintf(fptrM,"Error Reading File\n");
- printf("Error Reading File\n");
- exit (0);
- }
- fptr2 = (fopen("sortedArray2.txt", "w"));
- for(i = 0; i < array2Index; i = i + 1){
- fprintf(fptr2, "%d\n", array2[i]);
- }
- fclose(fptr2);
- fclose(fptrM);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement