Help with program

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.

Last edited on
You will need to use a loop with a sentinel value. http://en.wikipedia.org/wiki/Sentinel_value

A counter that will count the number of inputs during the live span of the loop.

a variable to hold the first input.

a dynamically allocated array that will hold the numbers that have the same 3 last digits as the first input.

The number of numbers that have the same last 3 digits will be the size of the array.

implement this and let us know what you have done.
Last edited on
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 ?

Thank you very much.
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <conio.h>

using namespace 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

This can be one idea. Good luck!

Last edited on
Using an array[100] limits the input to 100 input. The question explicitly specifies unlimited input.

This program will solve your problem and hope you understand it.

about, C++ books and references, i tought my self with Prentice Hall, C++ How to Program (8th Edition) http://downloadfreecomputerbooks.blogspot.com/2012/08/c-how-to-program-8th-edition.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace 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;
    }
}
thanks to all of you !! I solved the problem :)
Topic archived. No new replies allowed.