Question

1.1. Write a function named areFirstTwoTheSame AsLast TwoChars that accepts a string. It returns true if the first two char

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

1.1

#include<iostream>
#include<string>
using namespace std;
bool areFirstTwoTheSameAsLastTwo(string str)
{
   if (str.length() == 0 || str.length() == 1)
       return false;
   string firstTwo, lastTwo;

   firstTwo = str.substr(0, 2);
   lastTwo = str.substr(str.length() - 2);

   return (firstTwo == lastTwo);
}

int main()
{
   cout << "" <<" = "<<areFirstTwoTheSameAsLastTwo("") << endl;
   cout << "A" << " = " << areFirstTwoTheSameAsLastTwo("A") << endl;
   cout << "AB" << " = " << areFirstTwoTheSameAsLastTwo("AB") << endl;
   cout << "ABA" << " = " << areFirstTwoTheSameAsLastTwo("ABA") << endl;
   cout << "ABAB" << " = " << areFirstTwoTheSameAsLastTwo("ABAB") << endl;
   system("pause");
   return 0;
}

output

C:\Users\josee\source\repos\Simple functions\Debug\Simple functions.exe A = 0 AB = 1 ABA = 0 ABAB = 1 Press any key to contin1.2

#include<iostream>

using namespace std;

int checkGradeList(char grade[], int size)
{
   int count = 0;
   for (int i = 0; i < size; i++)
   {
       int isValidate = (grade[i] == 'A' || grade[i] == 'a' || grade[i] == 'B' || grade[i] == 'b' || grade[i] == 'C' || grade[i] == 'c' || grade[i] == 'D' || grade[i] == 'd' || grade[i] == 'F' || grade[i] == 'f');
       if (!isValidate)
       {
           grade[i] ='I';
           count++;
       }
   }
   return count;
}
int main()
{
   char grade[] = { 'A','B','X','C' };
   cout <<"Total invalid grade: "<< checkGradeList(grade, 4)<<endl;
   cout << "Grades after validation: " << endl;
   for (int i = 0; i < 4; i++)
   {
       cout<<grade[i] << endl;
   }
   system("pause");
   return 1;
}

outputE C:\Users\josee\source\repos Simple functions\Debug\Simple functions.exe Total invalid grade: 1 Grades after validation: A т1.3

#include<iostream>

using namespace std;

bool isTheSamllestLast(int &smallest)
{
   int num, count = 0,lastNum;
   cout << "Enter a number and 0 to stop: ";
   cin >> num;
   while (num > 0)
   {
       if (count == 0)
       {
           smallest = num;
           lastNum = num;
       }
       if (num < smallest)
           smallest = num;
       cout << "Enter a number and 0 to stop: ";
       cin >> num;
       count++;
   }
   if (count == 0)
   {
       smallest = 0;
       return 0;
   }
   if(count==1)
   {
       return true;
   }
   if (lastNum <= smallest)
       return true;
   return false;
}
int main()
{
   int smallest;

   if(isTheSamllestLast(smallest))
   {
       cout << "Lat num is smallest." << endl;
   }
   else
   {
       cout << "Lat num is not smallest." << endl;
   }
   cout << "Smallest nume is: " << smallest << endl;
   system("pause");
   return 1;
}

output

C:\Users\josee\source\repos\Simple functions\Debug\Simple functions.exe Enter a number and to stop: 50 Enter a number and to1.4

#include<iostream>

using namespace std;
bool checkYourGrade()
{
   char grade;
   cout << "Please enter your grade: ";
   cin >> grade;
   int count = 1;
   while (true)
   {
       if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F')
           break;
       if (count == 3)
           return false;
       cout << "Invalid grade. Please try again.";
       cout << "Please enter your grade: ";
       cin >> grade;
       count++;
   }
   if (grade == 'A' || grade == 'B' || grade == 'C')
       return true;
   return false;
}
int main()
{
   cout << (checkYourGrade() ? "Passed" : "Failed") << endl;
   system("pause");
   return 0;
}

outputs

ID: Chegg\C++ Check grade\Debug Check grade.exe Please enter your grade: B Passed Press any key to continue . ..ID: Chegg\C++ Check grade\Debug Check grade.exe Please enter your grade: Y Invalid grade. Please try again.Please enter yourID: Chegg\C++ Check grade\Debug Check grade.exe Please enter your grade: Y Invalid grade. Please try again.Please enter your

1.5

#include<iostream>

using namespace std;
int GetScoreTotalWithoutHigLow()
{
   int min, max, total=0, count = 0,num;
   while (true)
   {
       cout << "Please enter non=negative score: ";
       cin >> num;
       if (num < 0)
           break;
       if (count == 0)
       {
           min = num;
           max = num;
       }
       count++;
       total += num;
       if (num < min)
           min = num;
       if (max < num)
           max = num;
   }
   if (count <= 2)
       return 0;
   return total - min - max;
}
int main()
{
   cout <<"The total is : "<< GetScoreTotalWithoutHigLow() << endl;
   system("pause");
   return 0;
}

output

ID: Chegg\C++ Check grade\Debug Check grade.exe Please enter non=negative score: 10 Please enter non=negative score: 20 PleasIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if...
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
  • C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if...

    C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if the string contains the digits in descending order. The function can ignore (e.g. skip checking) all the characters that are not digits in the string. Empty string will return false. For example, string of “95421” or “862” or “8622” or “88” or “9” will return true and string of “95423” or “889” or “9445” or “449” or “” will return false.

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • 1. Write a function named “areFirstTwoTheSameAsTheLastTwo” that accepts an array of integers, its size and return...

    1. Write a function named “areFirstTwoTheSameAsTheLastTwo” that accepts an array of integers, its size and return true if the first two numbers in the array are the same as the last two numbers in the array. It returns false otherwise. If the array has less than 4 elements, it always returns false. For example, this array of {10, 20, 30, 40, 10, 20} or {10, 20, 10, 20} will return true but this array of {10, 20, 20, 10} or...

  • C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa...

    C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa parameter */ //get_letter_grade.cpp /* Write function code for gpa_to_letter_grade that returns a string and accepts a double gpa parameter YOU MUST USE A SWITCH STATEMENT Given a double 3.5 returns the string A TIP: You'll have to convert the double to an int using multiplication Table 3.5 to 4 returns an A 3.0 to 3.49 returns a B 1.7 to 2.99 returns a C...

  • Write a function named vertical that accepts a string as its parameter and prints each letter...

    Write a function named vertical that accepts a string as its parameter and prints each letter of the string on separate lines. For example, a call of vertical("hey now") should produce the following output: h e y n o w

  • Write a function check palindrome, which takes a string x as argument and which returns True...

    Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

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