Lab Work 12 Solution
Problem 1
#include <iostream>using namespace std;int main(){ char L[100]; int i = 0; int j = 0; int len = 0; int max = 0; cout << "Enter a line terminated by #: "; cin.getline(L, 100, '#'); for (i = 0; L[i] != '\0'; i++) if (L[i] == 'a' || L[i] == 'e' || L[i] == 'i' || L[i] == 'o' || L[i] == 'u') L[i] = toupper(L[i]); for (i = 0; L[i] != '\0'; i++) cout << L[i]; cout << endl; cout << "Words in your line are: " << endl; for (i = 0; L[i] != '\0'; i++) if (L[i] == ' ') cout << endl; else cout << L[i]; for (i = 0; L[i] != '\0'; i++) j++; for (i = j; i < j + 3; i++) L[i] = '*'; L[i] = '\0'; for (i = 0; L[i] != '\0'; i++) cout << L[i]; cout << endl; for (i = 0; L[i] != '\0'; i++) { if (L[i] != ' ') len++; else if (len > max) { max = len; len = 0; } } cout << "Length of the longest word is: " << max << endl; system("pause"); return 0;} | |
Problem 2
#include <iostream>using namespace std; int main(){ char S1[20]; int countVowels = 0; cout << "Enter your word: "; cin >> S1; for (int i = 0; S1[i] != '\0'; i++) if (S1[i] == 'a' || S1[i] == 'e' || S1[i] == 'i' || S1[i] == 'o' || S1[i] == 'u' || S1[i] == 'A' || S1[i] == 'E' || S1[i] == 'I' || S1[i] == 'O' || S1[i] == 'U') countVowels++; cout << "Number of vowels in " << S1 << " is: " << countVowels << endl; for (int i = 0; S1[i] != '\0'; i++) if (islower(S1[i])) S1[i] = toupper(S1[i]); else S1[i] = tolower(S1[i]); cout << "Modified Word: " << S1 << endl; system("pause"); return 0;} |
Problem 3
#include <iostream>#include <ctime>using namespace std; int highest(int[], int);double perPrime(int[], int);int main(){ srand(time(0)); int A[100]; int N; int choice; do { cout << "Enter N: "; cin >> N; } while (N > 100); for (int i = 0; i < N; i++) A[i] = 1 + rand() % 30; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: cout << "Number with the highest occurrence in A is: " << highest(A,N) << endl; break; case 2: cout << "Percentage of prime numbers in A is: " << perPrime(A, N) << "%" << endl; break; case 3: cout << "Exit"; break; } system("pause"); return 0;}int highest(int A[], int N){ int occ[31] = { 0 }; int highest = 0; int number = 0; cout << "Array Elements are:" << endl; for (int i = 0; i < N; i++) cout << A[i] << endl; for (int i = 0; i < N; i++) occ[A[i]]++; for (int i = 1; i < 31; i++) cout << i << " was repeated " << occ[i] << " times" << endl; for (int i = 0; i < 30; i++) if (occ[i]>highest) { highest = occ[i]; number = i; } return number;}double perPrime(int A[], int N){ int j = 0; int countPrime = 0; double per = 0.0; cout << "Array Elements are:" << endl; for (int i = 0; i < N; i++) cout << A[i] << endl; cout << "Prime numbers are:" << endl; for (int i = 0; i < N; i++) { if (A[i] == 1) { countPrime++; cout << A[i] << endl; } for (j = 2; j < A[i]; j++) if (A[i] % j == 0) break; if (A[i] == j) { countPrime++; cout << A[i] << endl; } } per = (double)countPrime / N * 100; return per;} |