Question

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 == 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..

Submit both a test driver main() function (that tests at least all the below examples) as well as the writeArrayNthBackward function.

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

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Thank You !!


===========================================================================================

#include<iostream>
using namespace std;

void writeArrayNithBackward(const char sentence [], int size, int &sum, int N){
  
   char letter;
   int countLetters=0;
   for(int i=size-1;i>=0; i--){
       countLetters+=1;
       letter = sentence[i];
       //cout<<countLetters<<" "<<letter<<endl;
       if(countLetters%N==0)cout<<letter;
       else{
           if('0'<=letter && letter<='9')sum+= letter-'0';
       }
   }
}
int main(){
  
   char sentence[] ="c1c1c1";
   int size = sizeof(sentence)/sizeof(char)-1;
   int sum=0;
   int N=2;
   writeArrayNithBackward(sentence,size,sum,N);
   cout<<"\nsum="<<sum<<endl;
  
  
}

============================================================================================

Add a comment
Know the answer?
Add Answer to:
Assignment 1) Your function will only write the Nth character of the array, supplied as a...
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
  • 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...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • C++ Demonstrate an understanding of array processing. Write a function that accepts the name of a...

    C++ Demonstrate an understanding of array processing. Write a function that accepts the name of a file (by asking the user), an array of strings, and the size of the array. Display each character of each string vertically to the file. For example, if the array is called with an array of 3 strings “hello”, “abc”, “bye”, the output will be: h e l l o …. y e Test the function with a string declaration of: string name[] =...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • C Question: Write a function p3 which receives a C string as a parameter, and an...

    C Question: Write a function p3 which receives a C string as a parameter, and an array of integers which will serve as indices into the string. The third parameter is the length of the integer array. The function jumbles the characters in the string according to the indices in the array of numbers. For example: char course [] = "CSC 373 Computer Systems I"; int indices[ ] = {0,1,4,5,6,7,0,1,25}; Then the function call p3(course, indices, 9); would cause course...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • 11) Write a method (including header and body) that takes as an input an array of...

    11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...

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