Question

In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

In C++ please. Thank you!!

#include <iostream>
#include <cstring>

// print an array backwards, where 'first' is the first index
// of the array, and 'last' is the last index 
void writeArrayBackward(const char anArray[], int first, int last) {
  int i = 0;

  for (i = last; i >= first; i--) {
    std::cout << anArray[i];
  }
  std::cout << std::endl;
}

// test driver
int main() {
  const char *s = "abc123";
  writeArrayBackward(s, 0, strlen(s) - 1);
}

// Using the above iterative version of writeArrayBackward as a reference, write a recursive version of this function, but with a couple of twists:

1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below).

void writeArrayNthBackward(const char [], int, int, int);

The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n == 1, then the string is printed normally in reverse (no characters are dropped). If n == 2, then every "other" character is printed, and so on.

You can constrain N to be 1 <= N <= 3.

Your code does NOT need to output a newline ('endl') within the recursive function itself (as the above iterative version does), you can output the newline after the return of the function.

2) If any of the removed characters in the string were numbers (not letters), output their sum. For example, if your function removed the numbers 3 and 1 from the string 'abc123', the result would be 3 plus 1, which is 4. See the below table for more examples..

Converting a String Digit to an Integer

If you have a string and want to convert a character in the string to an int, there are a few ways to do it. The easiest is probably this one:

char *a = "123";
int n;
n = (int)a[0] - '0';

Notice we can't simply cast a[0] to an int, because that will give us the ASCII code for the character '1', which is 49. We want the value of 'n' to be 1, so by subtracting the ASCII code for 0 (which is 48), it "translates" the character to an integer value.

What to Submit in Canvas

Submit both a test driver main() function (that tests at least all the below examples) as well as your writeArrayNthBackward function. You can submit the entire lab in a single file if you prefer (the function and a driver main()) together in one .cpp file.

Example inputs and outputs:

Input string N Output
abc123 2 2ca (sum of numbers removed: 4)
hello 1 olleh (sum of numbers removed: 0)
hello 3 l (sum of numbers removed: 0)
c1c1c1 2 ccc (sum of numbers removed: 3)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code in dev-c++ :-

#include <iostream>
#include <cstring>
using namespace std;

// print an array backwards, where 'first' is the first index
// of the array, and 'last' is the last index
void writeArrayBackward(const char anArray[], int first, int last, int n) {
int m=0,count=0,x=0,j=0;
if(n==1){

int i = 0;

for (i = last; i >= first; i--) {
std::cout << anArray[i];
}
std::cout << std::endl;
}
else if(n>=2)
{
   j=n-1;
for (int i = last; i > n; i--) {
   x=i-j;
   //cout<<x<<endl;
   m = (int)anArray[x+1] - '0';
   //cout<<m<<endl;
   if( m> 0 && m <= 9 ) count+=m;
   //cout<<count<<endl;
std::cout << anArray[x] ;
j=j+n-1;
}
cout<<"(sum of numbers removed: "<<count<<")"<<endl;
}
}

// test driver
int main() {
const char *s = "c1c1c1";
int choice;
cout << "enter the choice:" <<endl;
cin>>choice;
  
writeArrayBackward(s, 0, strlen(s) - 1,choice);
}

Output:-

changing the string to hello and then the output will be:-

changing the string to c1c1c1 and then the output will be:-

If you didn't understood the code or need any further clarification or modification please do comment.

Thank you

Add a comment
Know the answer?
Add Answer to:
In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...
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
  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Please convert this C function into ARM ASSEMBLY LANGUAGE CORTEX M (Must be cortex m) #include<...

    Please convert this C function into ARM ASSEMBLY LANGUAGE CORTEX M (Must be cortex m) #include<stdio.h> int ascii(char c) {                 return (int)c; } int computeMagicNumber(char* name) {                 int sum, i; //varibales                 sum = 0; //set sum to 0                 //calculate sum                 int N = sizeof(name)/sizeof(name[0]); //get name size                 for(i=0;i<N;i++)                                 sum = sum + (i+1)*ascii(name[i]);                                 return sum%1001; //return magic number MAIN FUNCTION : extern int computeMagicNumber(char *); int main(void) { char name[255]="Yusuf Ozturk"; int magic; magic=computeMagicNumber(name); printf("\n Magic number is %d\n",magic); return...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

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