Problem 1. Please complete all parts and explain. Thank you. c++ a. Write a func
ID: 3737477 • Letter: P
Question
Problem 1. Please complete all parts and explain. Thank you. c++
a. Write a function, string rotate(sting s) that rotates it right by num characters.
b. Write a function, bool anagram(string s1, string s2) that returns true or false depending on whether or not s1 and s2 are anagrams of each other.
c. Write a function void removeDups(string s) that removes duplicated characters in a string. For example, “to beeeee or nnot to be” would be changed to “to be or not to be”.
d. Input a sequence of non-negative numbers using cin (terminated with a -1). Output the average of the even-indexed and odd-indexed ones.
Ex: if the input is 3,7,2,18,7,8,-1, output 4 and 11.
e Input a positive integer k. Repeat the step: If k is even, divide it by 2; otherwise replace it with 3k+1. Print a success message if the number eventually reaches 1 (infinite loop otherwise).
f Input a positive integer n. Print a success message iff the above works for all integers k [1,n].
Hint: write a loop in which the previous code is nested.
g Input a string s. Repeat the step: If s has even length, remove the right half of s; otherwise replace s with 3 copies of s followed by s’s first character. Print a success message iff the string eventually has length 1. You may use the substr library function. Hint: how does the previous code change?
h. Draw a circle using character graphics. (using nested loops)
Explanation / Answer
Solution:
The first question is done as per Chegg guidelines, please repost others. ?
a)
void stringRotate(char* str, int num)
{
char temp1, temp2; //temp variables to hold the value
for(int i= 0; i<num; i++)
temp1= str[0]; //hold the first element of the array in temp1
{
for(int j=0; j<str.length; j++)
{
temp2= str[j+1];
str[j]= temp2;
}
str[str.length-1]= temp1; //Now put it in the back side
}
}
b)
bool anagram(string s1, string s2){
int n= strlen();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n; j++)
{
if(A[i] == B[j])
{
count++;
}
}
}
if(count == n)
{
cout<<"Anagrams Strings from Algo 2"<<endl;
}
else
cout<<"Not Anagrams string from Algo 2"<<endl;
}
}
}
c)
void removeDupsSorting(char *s)
{
int res1= 1, res2= 1;
while (*(s + res2))
{
if (*(s + res2) != *(s + res2- 1))
{
*(s + res1) = *(s + res2);
res1++;
}
res2++;
}
*(s + res1) = '';
return s;
}
char *removeDups(char *s)
{
int n = strlen(s);
sort(s, s+n);
return removeDupsSorting(s);
}
}
d)
void calcAverage()
{
int array[100], i= 0, sumEven= 0, sumOdd= 0;
while(true){
cout<<"Please enter an int: "
cin>>array[i];
i++;
if(i%2==0)
sumEven+= array[i];
else
sumOdd+= array[i];
}
evenAverage= sumEven/(ceil(i/2));
oddAverage= sumOdd/(i/2);
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.