How can 7 characters be printed from an array of 20? They must be entered in fro
ID: 3633368 • Letter: H
Question
How can 7 characters be printed from an array of 20? They must be entered in from keyboard and end with a sentinel value of '.' that is not allowed to print out. The maximum characters that can be read in is 20 I can't get the sentinel value to work and am using a previous program i wrote that ends integer values with a -1 as an example to apply to characters. The only way it works now is to enter 20 characters and the period prints out. the 1st example is what I am using to reference sentinel value, and the one following that is the program for reversing characters that the sentinel value is applied to. Thanks in advance. Any comments will be a great help in learning this C++ program.#include <iostream>
using namespace std;
// Main Program
int main( )
{
int sum ;
int number;
cout << "Enter a list of non-negative integers. "
<< "Place a negative integer after the list. ";
sum = 0;
cin >> number;
while (number >=0)
{
sum = sum + number;
cin >> number;
}
cout << "The sum is: " << sum;
cout << endl;
return 0;
}
// Include Section
#include <iostream>
using namespace std;
int main( )
{
char letter[20];
// User Input:
cout << "Enter Letters" << endl;
cout << ". to end list. ";
for( int i = 0; i < 20; i++ )
{
cin >> letter[ i ];
}// End of for loop
// Reverse Output:
cout << endl << "Reverse Output:";
for( int i = 19; i >= 0; i-- )
{
cout << letter[ i ] << " ";
}// End of for loop
return 0;
}
Explanation / Answer
#include using namespace std; // Main Program int main( ) { int count = 0; //used to track how many characters were entered. char theChar = ' '; char character[20];//declare the character array of length 20 //Tell the user to enter any character they wish per the problem description. cout > theChar; character[count] = theChar; count++; }//end while */ //delete this row to uncomment //now print out the array in reverse order //notice that i is initialized to count-2 because in the do..While loop, //the count variable is incremented before the check in the While check, so it is one more than the number //of characters entered....so that takes us to count-1. //Now, since the sentinal * is the last value in the array, which is count-1, then we make it count-2 //so that the * does not print, per the problem description. coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.