Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_COUNT 100
- int main()
- {
- int numbers[MAX_COUNT];
- int n, num;
- char option;
- while (1)
- {
- printf("a. Enter numbers from console\n");
- printf("b. Check for number in the array\n");
- printf("c. Print the numbers in console\n");
- printf("d. Count of elements squared which are smaller then max element\n");
- printf("e. Read the numbers from file\n");
- printf("f. Save the numbers in file\n");
- printf("g. Exit\n");
- option = getch();
- if (option == 'a')
- {
- n = enterArrayConsole(numbers);
- }
- else if (option == 'b')
- {
- printf("Enter number: ");
- scanf("%d", &num);
- if (checkForElement(numbers, n, num) != -1)
- {
- printf("%d is found!", num);
- }
- else
- {
- printf("%d is not found!", num);
- }
- }
- else if (option == 'c')
- {
- printArray(numbers, n);
- }
- else if (option == 'd')
- {
- printf("Count is: %d", func4(numbers, n));
- }
- else if (option == 'e')
- {
- n = readArrayFile(numbers);
- }
- else if (option == 'f')
- {
- saveArrayFile(numbers, n);
- }
- else if (option == 'g')
- {
- return;
- }
- else
- {
- printf("Invalid input!\n");
- }
- printf("\n\n");
- getch();
- }
- return 0;
- }
- int enterArrayConsole(int *numbers)
- {
- int i, n;
- printf("Enter number of elements: ");
- scanf("%d", &n);
- for (i = 0; i < n; i++)
- {
- printf("Enter number[%d]:", i);
- scanf("%d", &numbers[i]);
- }
- return n;
- }
- void printArray(int *numbers, int n)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- printf("%d, ", numbers[i]);
- }
- }
- int checkForElement(int *numbers, int n, int num)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- if (numbers[i] == num)
- {
- return i;
- }
- }
- return -1;
- }
- void saveArrayFile(int *numbers, int n)
- {
- int i;
- char fname[50];
- FILE *fwrite;
- printf("Enter file name: ");
- scanf("%s", fname);
- if((fwrite = fopen(fname, "w")) == 0)
- {
- printf("Problem to open/create the file!");
- return;
- }
- for (i = 0; i < n; i++)
- {
- fprintf(fwrite, "%d\n", numbers[i]);
- }
- fclose(fwrite);
- }
- int readArrayFile(int *numbers)
- {
- int i = 0, n = 0;
- char fname[50];
- FILE *fread;
- printf("Enter file name: ");
- scanf("%s", fname);
- if((fread = fopen(fname, "r")) == 0)
- {
- printf("Problem to open the file!");
- return;
- }
- while (fscanf(fread, "%d", &numbers[i]) != EOF)
- {
- i++;
- n++;
- }
- fclose(fread);
- return n;
- }
- // how elements squared are smaller then max element
- int func4(int *numbers, int n)
- {
- int i, count = 0, maxIndex = 0;
- int max = numbers[0];
- for (i = 1; i < n; i++)
- {
- if (numbers[i] > numbers[maxIndex])
- {
- maxIndex = i;
- }
- }
- for (i = 0; i < n; i++)
- {
- if (i == maxIndex)
- {
- continue;
- }
- if (numbers[i] * numbers[i] < numbers[maxIndex])
- {
- count++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement