I'm new programming and I need to build a program that tells you when you input an unlimited number of numbers, how many and which numbers finish in the same three digits of the first input number.
I don't know how to put an unlimited number of inputs and also don't know how to do the rest.
I have started CS major and have to deliver this tomorrow and don't know how. I would appreciate any help. Thanks.
Thanks for your quick reply, well the fact is that I have had only two theory sessions and in theory and to explain things clearly I'll say that I have no fucking idea of what I'm doing. Other students have programmed a lot before but I haven't, and the teacher assumes that we already know things that I don't. So I won't be able to solve that problem with your indications at the moment...
What I really need to see are examples of basic programs so I can see the syntaxis and how c++ works.
Do you know any website or book I can read that has that ?
One idea will be to use an array to store the numbers.
We define the array A[100] which means we can store 100 numbers, in this array.
The variable n will be the number of the numbers.
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
int A[100], i, n;
cout << "How many numbers do you want to input?\n";
cin >> n; // reading the number of the numbers
for(i=0; i<n; i++)
{
cin >> A[i]; //reading the array
}
cout << "The numbers are:\n";
for(i=0; i<n; i++)
{
cout << A[i] << " "; //displaying the numbers you entered
}
_getch();
}
Now if you want to get the last 3 digits of the number you should use the modulo operator. Modulo will get the remainder of the division.
Number % 3 // will get the last 3 digits of the number
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
usingnamespace std;
void getLastThree(vector<int> &, int ); //as the name implies
int main()
{
int first; //first input number: reference
int number; //consegative numbers
int count = 0; //number of inputs
vector<int> lastThree; //last three digits of reference input
vector<int> newLastThree; //last three digits of consegative input
vector<int> matchLastThree; //numbers with matches
ostream_iterator<int> output(cout,"\n"); //ouput
do
{
cout<<"Enter number(>= 100): "; //at least 3 digits
cin>>first;
getLastThree(lastThree,first);
}while(first < 100); //loop until the first digit is at least 3 digits
do
{
cout<<"Enter number( 0 to end): ";
cin>>number;
if(number > 100)
{
getLastThree(newLastThree,number);
}
if(lastThree == newLastThree)
matchLastThree.push_back(number); //add the number to the matches vector
newLastThree.erase(newLastThree.begin(),newLastThree.end()); //reset
++count;
}while(number != 0);
cout<<"\nThe total number of numbers entered is "<<count<<endl;
cout<<"The number of last three digit matches with the first is "<<matchLastThree.size()<<endl;
cout<<"The matches are :\n\n";
copy(matchLastThree.begin(),matchLastThree.end(),output); //output the vector
}
void getLastThree(vector<int> &vec, int number)
{
for(int i = 0; i < 3; i++)
{
int r = number%10;
vec.push_back(r);
number /= 10;
}
}