Question

Write a C++ program that reads a string and creates a cyclical array. A cyclical array...

Write a C++ program that reads a string and creates a cyclical array. A cyclical array is an array when the last element is pointing to the first one. Then write a loop that uses pointers and loops through the array starting from the first element and finishes after it visits the first element 5 times.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
class cyclic_array
{
   public:
   char c;
   cyclic_array *next;
   cyclic_array()
   {
      
   }
};

int main()
{
   //reading a string
   string s;
   cout<<"Enter a string:";
   cin>>s;
   //creating cyclic array
   cyclic_array *h=new cyclic_array();
   cyclic_array *temp=h;
   int i;      
   for(i=0;s[i]!=0;i++);
   int l=i;
   i=0;
   while(i<l)
   {
       temp->c=s[i];
       if(l-1!=i)
       {
           temp->next= new cyclic_array();
           temp=temp->next;  
       }  
          
       i++;  
   }
   //linking to fist node
   temp->next=h;
  
   //printing 5 times
   i=0;
   temp=h;
   while(i<5*l)
   {
       cout<<temp->c;
       temp=temp->next;
      
       i++;  
   }
   cout<<endl;

   return 0;
}

output:

Enter a string:hello
hellohellohellohellohello


Process exited normally.
Press any key to continue . . .


Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads a string and creates a cyclical array. A cyclical array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT