Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function Name: coldwar nputs: 1. (char) a scrambled character vector 2. (double)

ID: 3879294 • Letter: F

Question

Function Name: coldwar nputs: 1. (char) a scrambled character vector 2. (double) an integer representing the shift of odd indices 3. (double) an integer representing the shift of even indices Outputs: 1. (char) the unscrambled character vector Background: While enjoying the winter weather with some pals, you decide to have a snowball fight. Your teammate, on the other side of Tech Green, wants to communicate a frosty message to you without the opposing side being able to decode it. They decide to shout a scrambled version of your message along with two numbers that represent shift values. Since your and your friend are well versed in MATLAB, you decide to write a quick function to decode the encrypted message Function Description: You must 'shift the letters at even indices by a certain amount and the letters at odd indices by a different amount. For example, if the letter is 'a and the shift amount is 4 the letter will then become 'e'. A negative shift means shifting towards 'a' in the alphabet. The shift should 'wrap' around the alphabet - 'x' shifted by 7 should become 'e Example [out] = coldwar("usph',2,4) out => 'word' Notes: There will be no spaces or uppercase letters. .The shift may be greater than 26. A positive shift of 28 is the same as a shift of 2. Hints: mod() will be useful.

Explanation / Answer

#include<iostream>
using namespace std;
void coldWar(char c[],int o,int e)
{
   for(int i=0;c[i]!='';i++)
   {
       int n =(int)c[i];
       if(i%2==0)//posiion odd,,index even
       {
               n=n+o;
               if(n>(int)'z')
               {
                   n=n-122;
                   n=n+97-1;  
               }
               else if(n<97)
               {
                   n=97-n;
                   n=122-n+1;
               }
               c[i]=(char)n;
       }
       else
       {
           n=n+e;
               if(n>(int)'z')
               {
                   n=n-122;
                   n=n+97-1;  
               }
               else if(n<97)
               {
                   n=97-n;
                   n=122-n+1;
               }
               c[i]=(char)n;
          
       }  
   }
  
}
int main()
{
   char c[100];
   cout<<"Enter a string:";
   cin>>c;
   int o,e;
   cout<<"Enter odd position difference:";
   cin>>o;
   cout<<"Enter even position difference:";
   cin>>e;
  
   coldWar(c,o,e);
  
   cout<<"output: "<<c<<endl;
   return 0;
}

output:

Enter a string:usph
Enter odd position difference:2
Enter even position difference:-4
output: word


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