Hey guys, I am having trouble finishing this program. It does not work. I am suppose to have the user input 10 numbers and if there are duplicates it will only show once.
For example:
User input: 1 2 2 4 5 5
Output: 1 2 4 5
So far I have this
#include<iostream>
#include<iomanip>
using namespace std;
//Main function
int main()
{
//Holds variable for length
const int length = 11;
//Holds variables for length and array
int dedup[length];
int array[10];
cout <<"Please enter 10 integers, hitting return after each one: ";
//Sets i,i2,i3 to 0
int i=0;
int i2=0;
int i3=0;
//Loop for user input of 10 numbers
for(i=0; i < 10; i++)
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;
}
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.
#include <iostream>
class Array
{
public:
Array(charconst * 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] << ' ';
elseif( !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) returntrue;
returnfalse;// 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;
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.
Some comment before thought: 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 18
#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;
}
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.
#include <iostream>
class Array
{
public:
Array(charconst * 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] << ' ';
elseif( !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) returntrue;
returnfalse;// 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;
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.
Your first question you answered yourself. The second question creates an object stored in your memory which upon construction says "Hello World!". You don't need to create a container for a function to use the function. Classes are actually useful when you want data that you can manipulate using its included functions. http://cplusplus.com/doc/tutorial/structures/ http://cplusplus.com/doc/tutorial/classes/
There is no std::end.
Never post full solutions to any problem on this forum.
1- Didn't mean to hurt any feeling or touch any sensitivity!
2-"There is no std::end": it's just a typo and it seems to that u knew that.
3-I dont think the person that posted with the problem will ever hand in a programme like mine. The programme is complete to try it. At least i just wanted to throw in new insight, maybe by studying the functions or anything or getting any idea, some helps.
4-My mistake i dont know how to use the forum!
5-We are all beginners, aint we? I wont play into you for some code layout I would try to help KINDLY!!!!
Erm... Demien... um... we have no guarantees that the OP will not turn in a program identical to yours in any way on this forum. That's... why we have that "rule". No offense to the OP intended. Instead, give the general idea without the implementation. That's what most of us do here.