-1

I am a C++ noob. I have a list of numbers that I put into a Vector. All numbers are 9 digit integers and are unique. I want to know what is the least amount of digits (starting from the right) that can be used to uniquily identify each number in the set. right now there are only 6 numbers, but the list could potentially grow into the thousands. I have posted my code thus far (not working.)

EDIT output is the following...

digit is 1
digit is 1
digit is 1

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

This is mostly a learning exercise. Please be generous and explicit with your comments and solutions.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main() {

    //declare stream variable and load vector with values 
    ifstream myfile("mydata.txt");
    vector<int> myVector;
    int num;
    while (myfile >> num) {
        myVector.push_back(num);
    }



    //sort and squack if there is a duplicate.  
    std::sort(myVector.begin(), myVector.end());
    for (int i = 0; i < (myVector.size() - 1); i++) {
        if (myVector.at(i) == myVector.at(i + 1)) {
            printf("There are duplicate student numbers in the file");
            exit(EXIT_FAILURE);
        }

    }
    //if it get here, then there are no duplicates of student numbers








    vector<int> newv;
    int k = 1;
    bool numberFound = false;
    bool myflag = false;

    while (numberFound == false) {
        //loop  through original numbers list and add a digit to newv.  
        for (int j = 0; j < myVector.size(); ++j) {
            newv.push_back(myVector.at(j) % (10^k));
        }
        sort(newv.begin(), newv.end());
        for (int i = 0; i < (newv.size() - 1); i++) {
            if (newv.at(i) == newv.at(i + 1)) {
                //there is a duplicate for this digit. Set flag.  
                myflag = true;
            }
            if (myflag == false) {
                numberFound = true;
                cout << "digit is " << k << endl;
            } else {
                k++;
            }

        }

    }






    //    for (int i = 0; i < myVector.size(); i++) {
    //        cout << "||" << myVector.at(i) << "||" << endl;
    //    }
    //
    //    for (int i = 0; i < newv.size(); i++) {
    //        cout << "---" << newv.at(i) << "---" << endl;
    //    }





    return 0;
}
user3738926
  • 1,178
  • 1
  • 10
  • 17
  • problem is my code is not working and I don't know why. I am reaching out for an answer after banging my proverbial head against the wall. Am I in the wrong place Jarod? – user3738926 Sep 24 '15 at 07:31
  • 3
    `10^k` has nothing to do with power of `10`. – Jarod42 Sep 24 '15 at 07:31
  • Show the error, compiler error message, linker error message. If algorithm doesn't do what you expect, show input, output end expected output. – Jarod42 Sep 24 '15 at 07:33
  • @user3738926 once your code does what it should do, feel free to come over to us on CR if you want it to be better. Right now it would be off topic because we request fully working code. – Heslacher Sep 24 '15 at 07:33
  • 2
    `x^y` in C++ does not mean "x to the y:th power", you can use `pow` from `cmath` instead. – Jacob Raihle Sep 24 '15 at 07:36
  • compiler output is now posted – user3738926 Sep 24 '15 at 07:38
  • pow from math.h does not work here as I am working with integers and get a floating point exception. – user3738926 Sep 24 '15 at 07:50
  • @user3738926 cast it to int. [Why is my power operator (^) not working?](http://stackoverflow.com/q/4843304/995714) – phuclv Sep 24 '15 at 10:57

1 Answers1

2

Check the below code.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm> 
#include <math.h>
using namespace std;

int main() {

//declare stream variable and load vector with values
ifstream myfile("mydata.txt");
vector<int> myVector;
int num;
while (myfile >> num) {
    myVector.push_back(num);
}
//sort and squack if there is a duplicate.
std::sort(myVector.begin(), myVector.end());
for (int i = 0; i < (myVector.size() - 1); i++) {
    if (myVector.at(i) == myVector.at(i + 1)) {
        printf("There are duplicate student numbers in the file");
        exit(EXIT_FAILURE);
    }
}
//if it get here, then there are no duplicates of student numbers
vector<int> newv;
int k = 1;
bool numberFound = false;
bool myflag = false;
int p = 1;
while (numberFound == false) {
    //loop  through original numbers list and add a digit to newv.
    newv.clear();
    p = p * 10;
    for (int j = 0; j < myVector.size(); ++j) {
        newv.push_back(myVector[j] % p);
    }
    sort(newv.begin(), newv.end());
     myflag = false;
    for (int i = 0; i < (newv.size() - 1); i++) {

        if ( newv[i] == newv[i+1]) {
            //there is a duplicate for this digit. Set flag.
            myflag = true;
            break;
        }
    }
    if (myflag == true){
        k ++;
    }else{
        numberFound = true;
            cout << "digit is " << k << endl;
            break;
    }
}
return 0;
}

Sample Input:

123451789
123456687
125456789
123456780

Output:

digit is 4
Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37