Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <omp.h>
- int main()
- {
- //starting time for calculating run time
- double start = omp_get_wtime( );
- //enum for purposes of declaring array size with variable
- enum SIZE {SIZE = 26};
- //array to hold number of occurences of each letter in alphabet
- //with index 0 representing A,..., index 25 = Z
- int s_res[SIZE] = {};
- //number of threads requested
- const int NUMB = 10;
- //char string for identifying chars from txt
- char * alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- //requesting num threads is num of books
- omp_set_num_threads(NUMB);
- //parallel block
- #pragma omp parallel
- {
- //task block
- #pragma omp task
- { //for use in calculating file name
- int ID = omp_get_thread_num();
- //adding ID to char '0' will result in char '0' - '9' depending on ID
- char filename[] = {'0' + ID,'.','t','x','t'};
- FILE *fptr;
- fptr=fopen(filename,"r");
- char ch;
- //loop through txt file char at a time
- while((ch=toupper((fgetc(fptr))))!=EOF)
- {
- //pointer to memory address of ch in alpha
- const char *ptr = strchr(alpha, ch);
- if(ptr!=NULL)
- {
- //calculate correct index
- int index = ptr - alpha;
- #pragma omp atomic
- //use index to increment array
- s_res[index]++;
- }
- }
- }
- }
- //ending time for calculating run time
- double end = omp_get_wtime( );
- printf("\nCompleted in %f seconds", end-start);
- printf("\nThe letter frequencies are:\n");
- //print results
- for (int i =0; i < 26; i++)
- {
- printf("%c's: %d\n", alpha[i], s_res[i] );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement