Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose.
Why is the runtime for the output for the OpenMP parallel program much longer?
Serial Program
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(){
char str[1000], ch;
int i, frequency = 0;
clock_t t;
struct timespec ts, ts2;
printf("Enter a string: ");
gets(str);
int len = strlen(str);
printf("Enter a character to find the frequency:
");
scanf("%c",&ch);
t = clock();
timespec_get(&ts, TIME_UTC);
for(i = 0; i < len; ++i)
{
if(ch == str[i])
frequency += 1;
}
t = clock() - t;
timespec_get(&ts2, TIME_UTC);
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("Frequency of %c = %d\n", ch, frequency);
printf("Time required = %ld \n",(ts2.tv_nsec -
ts.tv_nsec));
return 0;
}
Parallel Program using OpenMP directives
#include <omp.h>
#include <string.h>
int main(){
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);
int len = strlen(str);
printf("Enter a character to find the frequency:
");
scanf("%c",&ch);
double start = omp_get_wtime();
#pragma omp parallel for reduction(+ :
frequency)
for(i = 0; i < len; ++i)
{
if(ch == str[i])
frequency += 1;
}
double end = omp_get_wtime();
printf("Frequency of %c = %d\n", ch, frequency);
printf("Time required = %lf \n", end - start);
return 0;
}
I compared the time taken by the two methodologies used but it seems that the parallel approach is taking very less time compared to serial approach.Its only taking 0.000002 while the other one is taking 300
Serial
Parallel
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...
I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...
If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...
can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...
I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...
Which part of this program is used for input one context free grammar? And if you want to give another different context free grammar, how to do that? code: #include<stdio.h> #include<stdlib.h> #include<string.h> #define MaxVtNum 20 #define MaxVnNum 20 #define MaxPLength 20 #define MaxSTLength 50 char stack[20]={'#','E'}; char input[MaxSTLength]; char termin[MaxVtNum]={'i','+','*','(',')','#'}; char non_termin[MaxVnNum]={'E','G','T','H','F'}; struct product{ char left; char right[MaxPLength]; int length; }; struct product E,T,G,G1,H,H1,F,F1; struct product M[MaxVnNum][MaxVtNum]; int flag=1; int top=1; int l; void print_stack(){ } } } } }...
C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...
Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...