Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that accepts integers for as long as the input is positive. If a

ID: 3928838 • Letter: W

Question

Write a program that accepts integers for as long as the input is positive. If a negative or 0 is input, the program would stop and display the number of inputs that are equal to the first number entered. Example: if the inputs were 1, 2, 3, 1, 4, 1, 4, 1, 3, -1 the program would display: The number of numbers similar to the first one is 3. Write a function that asks the user to input an integer and returns it; that is a function that gets an integer from the keyboard and returns it. int getInt() {...} Include the above function in you previous program.

Explanation / Answer

Count.cpp

#include <iostream>

using namespace std;
int getInt();
int main()
{
int n=0, first;
int matchCount = 0;
int count = 0;
n = getInt();
while(n>0){
count++;
if(first == n){
matchCount++;
}
if(count == 1){
first = n;
}
n = getInt();
}
cout<<"The number of numbers similar to the first ont is "<<matchCount<<endl;
return 0;
}
int getInt(){
int n=0;
cout<<"Enter a number: ";
cin >> n;
return n;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                             

sh-4.3$ main                                                                                                                                                                                                                     

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 2                                                                                                                                                                                                                

Enter a number: 3                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 4                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 4                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 3                                                                                                                                                                                                                

Enter a number: -1                                                                                                                                                                                                               

The number of numbers similar to the first ont is 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote