Question

In: Computer Science

C++ I am a beginner at this and have a hard time with for loops.. I...

C++
I am a beginner at this and have a hard time with for loops.. I was given this prompt.


part 1.
1. Create an array to store 8 characters.
2. Using a for loop, get an 8-letter word from the user, one letter at a time.
3. Then use another for loop to print the complete word onto the screen one letter at a time.
4. Then use a third for loop to print out the word backwards.

part 2.
Write a function that takes your array of characters and a single character and searches through the array cell by cell for the specified character. If found, display the index at which the letter was found; if not, display a message stating that the character array does not contain that letter.
​void find (char letters [ ], int size, char c)

Solutions

Expert Solution

//part 1
#include<iostream>
using namespace std;
int main(){
   char array[9];
   //reading 8 letter word signle character at a time
   for(int i=0;i<8;i++){
       cin>>array[i];
   }
   cout<<"word : ";
   //printing complete 8 letter word one character at a time
   for(int i=0;i<8;i++){
       cout<<array[i];
   }
   cout<<"\nbackword word : ";
   //printing complete 8 letter word backward one character at a time
   for(int i=7;i>=0;i--){
       cout<<array[i];
   }
   return 0;
}

//part 2
#include<iostream>
using namespace std;
void find(char letters[],int size,char c);
int main(){
   int size;
   cout<<"Enter size of word : ";
   cin>>size;
   char letters[size];
   char c;
   //reading 8 letter word signle character at a time
   cout<<"Enter word : ";
   for(int i=0;i<size;i++){
       cin>>letters[i];
   }
   cout<<"Enter character for search location : ";   //reading character for searching
   cin>>c;
   find(letters,size,c);   //calling function
   return 0;
}
//function for searching element in character array
void find(char letters[],int size,char c){
   int i,flag=0;
   for(i=0;i<size;i++){
       //if letter is equal to any letter of charcter array then printing location and terminating the program
       if(letters[i]==c){
           flag=1;
           cout<< c <<" is found at location "<< i+1 <<endl;
           break;
       }
   }
   //if if block not executed then no element equal to that letter
   if(flag!=1)
       cout<<"character array does not contain letter "<<c;
}


Related Solutions

I have figured out this assignment, but I am having a hard time figuring out the...
I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks! Creating a Rectangle class We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following: The...
I understand the answer to this I am just having a hard time creating a graph...
I understand the answer to this I am just having a hard time creating a graph for it. Bill the butcher is upset because the government plans to tax beef $.10 a pound. "I hate paying taxes," he says. "Because of this, I'm raising all my beef prices by $.10 a pound. The consumers will bear this burden, not me." Do you see anything wrong with this way of thinking? Explain. Draw a graph describing your answer and attach it...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
Hello, I am just having a hard time understanding this question. The question is below. I...
Hello, I am just having a hard time understanding this question. The question is below. I am not supposed to write a program but instead in regular english sentence form just name the methods, fields, and variables I would use for this loan class. Im not really sure what a loan class means as well, thank you for your help. USING JAVA Given a Loan class, name the necessary fields and methods that we would need in this class. Be...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I am a beginner in this quantum-mechanics stuff. I understand the quantum eraser only from an...
I am a beginner in this quantum-mechanics stuff. I understand the quantum eraser only from an experimental view. So I didn't understand the formalism that describes the quantum eraser. But what does the experiment tells us? Does the photon know that there is somebody watching it? And this is why it behaves in another way? Does the photon also see the future?
can someone explain to me what osmolality is.? i am having a hard time understanding it
can someone explain to me what osmolality is.? i am having a hard time understanding it
I am having a hard time getting my an output after putting in the second set...
I am having a hard time getting my an output after putting in the second set of functions and I was hoping to be able to have the results from the functions be rounded to 2 decimal places. <html> <head> <title>Length Conversion</title> <script language='JavaScript' type='text/JavaScript'> <!-- function validate(type) { if(document.my_form.textinput.value=='') { alert('Fill the Input box before submitting'); return false; }else{ if(type=="to_feet"){ var res=3.2808*document.my_form.textinput.value; var unit=" feet"; }else{ var res=0.3048*document.my_form.textinput.value; var unit=" meter"; } document.getElementById("result").innerHTML=res.toFixed(2) + unit; return false; } }...
I am having a hard time understanding these two questions. If someone can explain that would...
I am having a hard time understanding these two questions. If someone can explain that would be great. 1) Explain the steps that enables body to metabolize fat to ATP? 2) Write out the amount of ATP, NADH, FADH2 produced in each step of cellular metabolism of a glucose.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT