Question

In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s"...

In C++

1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s" is an object of the type "string" and "pos" is a boolean value with the default argument of "true". If "pos" is "true", the function returns the summation of the ASCII codes of all the characters located at the even indices of "s" and if "pos" is "false", the function returns the summation of the ASCII codes of all the characters located at the odd indices of "s" . For example, if the caller sends the values of "ABCD" and "true" to the function respectively for the parameters "s" and "pos", the function returns 132 (i.e. 65 (the ASCII code of 'A') + 67 (the ASCII code of 'C')). If the function is called by the values of "ABCD" and "false" respectively for the parameters "s" and "pos", the function returns 134 (i.e. 66 (the ASCII code of 'B') + 68 (the ASCII code of 'D')).

2.Write a program that reads a string from the keyboard and generates a random integer in the range [0, 1]. If the random integer generated is 0, the program calls the "sumCodes" function to calculate the summation of the ASCII codes of the characters located at the odd indices of the input string, and if the random integer generated is 1, the program calls the "sumCodes" function to calculate the summation of the ASCII codes of the characters located at the even indices of the input string and displays the result on the screen.

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

C++ code for above problem

#include<iostream>
#include<time.h>
using namespace std;

int sumCodes(string str,bool pos)
{
    int sum=0;
    for(int i=0;i<str.length();i++)
    {
        char ch=str.at(i);
        if(i%2==pos)
        {
            sum+=ch;
        }
    }
    return sum;
}

int main()
{
    string str;
    cout << "Enter a string: ";
    cin >> str;
    srand(time(NULL));
    bool pos=rand()%2==0 ? false : true;
  
    cout << endl;
    cout << "Entered string: " << str << endl;
    cout << "Position value: " << pos << endl;
    cout << "Value returned by SumCodes() is: " << sumCodes(str,pos) << endl;
    return 0;
}

Sample output

Enter a string: ABCD Entered string: ABCD Position value: 0 Value returned by Sumcodes() is: 132

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s"...
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++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • c++ write a boolean function that accepts two integer arrays of size 10 as input parameters,...

    c++ write a boolean function that accepts two integer arrays of size 10 as input parameters, the function should return true if there are any similar values between the two arrays and shall return false if not, if there are similar values, print out on screen

  • C Program: Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not by returning either true or false:

    A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. Examples: “Dad”, “Anna”, “Never odd or even”, etc. For this problem, we will only consider a string to be palindromic if its combined alpha-numeric characters (‘A’-’Z’, ‘a’-’z’, and ‘0’-’9’) can be read the same both forward and backward, by skipping all other non-alphanumeric characters.Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not...

  • 1. a. Function Description: The Python function is "clearSecurity". It receives 2 parameters (dat...

    1. a. Function Description: The Python function is "clearSecurity". It receives 2 parameters (databaseScore, obsScore). The first is a float; the second is an integer. The function's purpose is to determine if the person can enter the secure zone. (databasescore-80.5 and obScore-5). The function returns a message saying whether the person is eligible to enter or needs further scrutiny. Fill in the following table. Expected result Function NamePurpose Input (s) and Parameter(s) clearSecurity0 1.b. Write the function code: 1.c. Write...

  • /* Write a recursive function named editDistance that accepts two string    * parameters and returns...

    /* Write a recursive function named editDistance that accepts two string    * parameters and returns the "edit distance" between the two strings as an    * integer. Edit distance (also called Levenshtein distance) is the minimum    * number of "changes" required to get from s1 to s2 or vice versa. A "change"    * is a) inserting a character,    * b) deleting a character, or    * c) changing a character to a different character.    *...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and...

    Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and divides the first parameter by the second. Your divide_two_numbers function must check the second parameter before dividing; if it is zero, raise an ZeroDivisionError exception which includes the string "second parameter was zero!" as a parameter instead of processing the division. Your main function must call your divide_two_numbers function at least twice; one call must use two normal parameters to verify normal operation, 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