Question

In: Computer Science

C++ Write a function that determines if there are three of the same value in a...

C++

  • Write a function that determines if there are three of the same value in a row in an array. The function returns a bool and is passed an array of ints and it's size as an int.
  • Write a function that determines if there are three of the same line in a row in a file. The function returns a bool and is passed a string which contains the filename.
  • Write a function that determines if there are two words that are the same in a row in a string. The function returns a bool and is passed a string.

Solutions

Expert Solution

CODE:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

//function given an int array and size int
bool isThreeCommon(int a[],int n){
//same is the output boolean
bool same = false;
//traversing the array
for(int i=0;i<n-2;i++){
//checking for 3 numbers after each index
for(int j=i;j<i+3;j++){
//if any of the three in a row is not same the loop breaks
if(a[i] != a[j])
break;
//if it didn't break it returns true
if(j == i+2)
same = true;
}
if(same)
return same;
}
//otherwise returns false
return same;
}

//function given an int array and size int
bool isThreeInFileCommon(string filename){
fstream file;
file.open(filename.c_str(),ios::in);
string line;
string *a = new string[100];
int num = 0;
//getting the lines from a file and storing in an array
while(getline(file,line)){
a[num] = line;
num += 1;
}
//same is the output boolean
bool same = false;
//traversing the array
for(int i=0;i<num-2;i++){
//checking for 3 lines after each index
for(int j=i;j<i+3;j++){
//if any of the three in a row is not same the loop breaks
if(a[i] != a[j])
break;
//if it didn't break it returns true
if(j == i+2)
same = true;
}
if(same)
return same;
}
//otherwise returns false
return same;
}

//function given an int array and size int
bool isTwoCommon(string s){
//same is the output boolean
bool same = false;
s = s + " ";
//an array of all the words in the string
string *a = new string[s.length()];
int lastSpace = 0,words = 0;
for(int i = 0;i<s.length();i++){
if(s.at(i) == ' '){
a[words] = s.substr(lastSpace,i-lastSpace);
words += 1;
lastSpace = i+1;

}
}
//traversing the array
for(int i=0;i<words-1;i++){
//checking for 3 numbers after each index
for(int j=i;j<i+2;j++){
//if any of the two in a row is not same the loop breaks
if(a[i] != a[j])
break;
//if it didn't break it returns true
if(j == i+1)
same = true;
}
if(same)
return same;
}
//otherwise returns false
return same;
}
int main(){
//array of int
int nums[] = {1,12,12,1,1,0,1,5,6,6};
//a line
string line = "All I to say is hello hello";
//calling all the three functions
cout<<"Three consecutive integers are same in the array: "<<isThreeCommon(nums,10)<<endl;
cout<<"Three consecutive lines are same in the file: "<<isThreeInFileCommon("input.txt")<<endl;
cout<<"Two consecutive words are same in a line: "<<isTwoCommon(line)<<endl;
return 0;
}
________________________________________

CODE IMAGES:

input.txt:

Hello
this
file
file
file
contains
three
lines
same
same

_________________________________________

OUTPUT:

__________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

This C++ assignment asks to write a function that determines if a C-string begins with a...
This C++ assignment asks to write a function that determines if a C-string begins with a specified prefix. It should have the following signature: bool starts(char *str, char *prefix) It should return true if str begins with prefix, false if not. It should return false if prefix is longer than str. See the table below for some examples of what your function should return for various cases: str prefix returns airplanes air true airplanes abc false airplanes plane false airplanes...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 Codes in C please.s
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2
write a C++ program that uses function swap to enter three real value from the keyboard...
write a C++ program that uses function swap to enter three real value from the keyboard and outputs there in order from minimum to the maximum. For example 10,100,30 if were entered then the output would be 10,30,100. The program should use function get_data(a,b,c) and print_data(a,b,c)
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Write a function getStats(fname) that determines the following statistics for the input file, fname, and write...
Write a function getStats(fname) that determines the following statistics for the input file, fname, and write the results to the text file, FileStats.txt. • Number of occurrences of each day of the week in the file. • Number of lines (determined by end of line character “\n”) • Number of words • Numbers of characters (excludes spaces between words) The output from getStats() should • print the message ‘To view the results go to FileStats.txt’ • return ‘Thanks for using...
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
write a function that determines if a given vector of integersis a min–heap. By default,...
write a function that determines if a given vector of integers is a min–heap. By default, a vector is asemi–heap satisfying the heap structure propertybut not the heap order property.InputThere is an unknown number of integer data that will be provided from standard input. Each line of input represents the contents of a vector. We do not know how many lines of input there are.1 2 3 4 55 4 3 2 11 9 2 13 10 5 3 15...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT