Hey here's my answer to that!
Some comment before though: Next it's not the best coded programme or most efficient or whatever--- what I wanted to do was introduce new approach since it's a C++ forum. What i mean by that was C++ not C/C++. So what's the difference between:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <stdio.h>
int main() //: main() C compiler.
{
printf("Hello world!\n");
return 0;
}
To this other one?
#include <iostream>
int main()
{
std::cout << "Hello world! << std::end;
return 0;
}
|
Just diferent libraries, but what about this one?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
class HelloWorld
{
public:
HelloWorld() { std::cout << "Hello world!" << std::end; }
};
int main()
{
HelloWorld hello;
return 0;
}
|
Ok, some might say that C++ is not only OOP or that's not for a beginner level and so on... but I'm a proponent that the sooner u get to understand this OPP approach the easiest it's gonna be learn
to program in C++.
I mean to fully use its potencial... well enough already.
Repeated number removal:
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 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
class Array
{
public:
Array(char const * prompt): r_cnt(0)
{
std::cout << prompt << max <<": \n";// can be improved!!!
for(int i(0); i < max; ++i)
{
std::cin >> vect[i];
if( i != 0 && is_repeated(vect[i], i) )
{
rep_vect[r_cnt++] = vect[i];
/*for(int k(0); k < i; ++k)
{
if(vect[k] == vect[i])
{
rep_vect[r_cnt] = vect[k];
r_cnt++;
}
}*/
}
}
}
void Display()
{
for(int i(0); i < max; ++i)
if(i == 0)
std::cout << vect[i] << ' ';
else if( !is_repeated(vect[i], i) )
std::cout << vect[i] << ' ';
std::cout << "\n\nThese are the repeated\n";
for(int i(0); i < r_cnt; ++i)
std::cout << rep_vect[i] << ' ';
}
private:
enum {max = 10};
int vect[max];
int rep_vect[max/2];// how many time can u repeat an element in N element vector?
int r_cnt;
bool is_repeated(int num, int id)
{
bool found(false);// local found.
for(int i(0); i < id && !found; ++i) //: while not found iterates
{
if(vect[i] == num)
found = true;
}
if(found) return true;
return false;// notice i dont return found; Not a good idea to return local variables.
}
};
int main()
{
Array arr("Enter integers hitting return after each one; Only: ");
arr.Display();
}
|
Look i didn't remove the repeated numbers just decided to print the non-repeated and store repeated ones in another vector. But if what you wanted was to remove, which is hard in a staticly allocated vector, one thing to do is to REPLACE it for some maybe negative number;
1 2 3 4 5 6 7 8 9
|
for(int i(0); i < max; ++i)
{
std::cin >> vect[i];
if( i != 0 && is_repeated(vect[i], i) )
{
vect[i] = -1;
}
}
|
Then u just print the positives... Anothe way around is to create even another vector to store them.
Again, maybe I'm not being of much help if u wanted to get your code corrected but i still hope at least to have brought in some new insight! Study the functions in the class try to minimize the code or even find other ways around it.