C++
Debug nextVowel.cpp
#include <iostream> using namespace std; // Recursion on a single value // return next character (in ASCII order) // which is a vowel, starting with parameter // If there are no more vowels, return null character ('\0') char nextVowel(char start) { // recursive call return nextVowel(start + 1); // base case if (start > 'z') return '\0'; // incorrect base case if ('a' == start || 'i' == start || 'o' == start || 'u' == start) return start; } // Recursion on a group of values -- a vector int main() { char next; cout << "Simple recursion example with char's" << endl; cout << "Enter a starting char: "; cin >> next; next = nextVowel(next); if ('\0' == next) cout << "There was no next vowel" << endl; else cout << "The next vowel = " << next << endl; return 0; }
Thanks for the question,
every time, we get a char, we must increment the char variable which is started by 1 and then check if its a vowel (in the if clause the start=='e' was missing) and if its a vowel we return the vowel else we must recursively call with the present value of start which was earlier incremented by 1
Also, the base case should be the first condition to be checked in a recursive call.
Below is the corrected code.
===========================================================================
#include <iostream>
using namespace std;
// Recursion on a single value
// return next character (in ASCII order)
// which is a vowel, starting with parameter
// If there are no more vowels, return null character ('\0')
char nextVowel(char start) {
// base case should be the first condition to be checked
if (start > 'z') return '\0';
// incorrect base case
start+=1; // increment by 1 to get the next character
if ('a' == start || 'i' == start || 'e'==start // e was missing
added
|| 'o' == start || 'u' == start)
return start;
else{
return nextVowel(start); // recursive call
}
}
// Recursion on a group of values -- a vector
int main()
{
char next;
cout << "Simple recursion example with char's"
<< endl;
cout << "Enter a starting char: ";
cin >> next;
next = nextVowel(next);
if ('\0' == next)
cout << "There was no next vowel" << endl;
else cout << "The next vowel = " << next <<
endl;
return 0;
}
==============================================================================
C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std; void input(char& car, int& ihour,int& imin, int& ohour, int& omin); void time(char car, int ihour, int imin, int ohour, int omin, int& phour, int& pmin, int& round, double& total); void parkingCharge (char car, int round, double& total); void print(char car, int ihour, int imin, int ohour, int omin, int phour, int pmin, int round, double total); int main() { char car; int ihour; int...
Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...
please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }