Question

In: Computer Science

Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence)...

Please write code in C++ and include all header files used not bits/stdc:

(Same number subsequence)

Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds longest subsequence with the same number.

Sample Run

Enter a series of numbers ending with 0: 2
4
4
8
8
8
8
2
4
4
0
The longest same number sequence starts at index 3 with 4 values of 8

Solutions

Expert Solution

The C++ code for the above problem is:

#include <iostream> 
using namespace std; 
int main() 
{
        double num, num_past;
        cout << "Enter a series of numbers ending with 0: ";
        cin >> num; // input number
        num_past = num;
        double val = num; // maximum number of repitions of a number
        int max_ind = 0, ind = 0; // index with maximum number of repitions of a number initialised to 0
        int max_rep = 1, rep = 0; // maximum number of repitions of a number initialised to 1
        while(num != 0) { // loop continues till 0 is not entered
                cin >> num;
                if(num_past != num) { // if new entered number is not equal to past number
                        if(max_rep < rep) {
                                max_rep = rep;
                                max_ind = ind;
                                val = num_past;
                        }
                        num_past = num; rep = 0;
                }
                rep++; ind++;
        }
        cout << "The longest same number sequence starts at index "<< max_ind - max_rep + 1 <<
                " with " << max_rep << " values of " << val; // display the required output
        return 0; 
} 

The sample of the code is:

The output sample of the code is:

The complexity of the above problem is O(n). Comment down if you have queries regarding the code.


Related Solutions

write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All programs work with binary numbers which are powers of 2 (2, 4, 8, …).  Write a program that will have a user enter a number (n) less than 10,000. Then have the program put each power of 2 (int) into an array up to and possibly including the number n. This means you don't know at the start how large the array will be, so...
Please Write Code in C++ and include the correct #include <header> and not a catchall such...
Please Write Code in C++ and include the correct #include <header> and not a catchall such as bits/stdc: Write a recursive, string-valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves: Nothing if the string is empty Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the...
PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please. Write a C++ program to find...
PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please. Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() {...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() { char repeat = 'Y'; for (;repeat == 'Y';){ char empname[222]; float basicSalary, h_r_a, DearnessAllow, tax, netSalary; int e_id; cout<<"\nEmployee Name :"; cin>>empname; cout<<"\nEmployee Id :"; cin>>e_id; cout << "Enter Basic Salary : "; cin >> basicSalary; DearnessAllow = 0.30 * basicSalary; h_r_a= 800; switch (1) { case 1: if (basicSalary <= 2,50,000) tax = 0; case 2: if (basicSalary > 250000 && basicSalary <=...
(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to...
(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to create a Priority Queue Follow these steps: Declare an integer array to use for the heap (size of 20 to 30 elements) Create an insertion function (and any needed helper functions) that creates a min-heap (smallest priority is most important) Create a remove function (and any needed helper functions) that removes the min value from the heap and returns it In main, call the...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argb, char *argv[]){ char ch; int upper=1; if(argb>1){ if(strcmp(argv[1],"-s")==0){ upper=1; } else if(strcmp(argv[1],"-w")==0){ upper=0; } } while((ch=(char)getchar())!=EOF){ if(upper){ printf("%c",toupper(ch)); } else{ printf("%c",tolower(ch)); } } return 0; } ======================================================== #include <stdio.h> #include <stdlib.h> int calcRange(int array[],int size){ int max=array[1]; int min=array[0]; int a=0; for(a=1;a<size;a++){ if(array[a]>max){ max=array[a]; } } for(a=1;a<size;a++){ if(array[a]<min){ min=array[a]; } } printf("%d-%d=%d\n",max,min,max-min); } int main(int arga, char **argv){...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...
C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programmer created functions. Just do everything in main and make sure you comment each step so I can grade more easily. Also, this program will be expanded in Chapter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT