Need To Print Repeated Numbers Just Once / C++ - c++

Can't use libraries and other methods.
As you can see my program finds the repeated numbers and print it but I need to print the numbers just once.
As example if entered:
7 1 1 2 1 2 2 9
It should print
1 2
In case there is no any repeated number:
7 1 2 3 4 5 6 7
There should not be any output!
Also note, that the first number is the length of array:
#include <iostream>
int main()
{
unsigned size;
std::cin >> size;
int* myArray = new int[size];
for (int i = 0; i < size; i++) {
std::cin >> myArray[i];
}
for (int i = 0; i < size; i++) {
bool found = false;
for (int j = 0; j < i && !found; j++) {
found = (myArray[i] == myArray[j]);
}
if (!found) {
std::cout << myArray[i] << " ";
}
}
delete []myArray;
}

The easiest approach would probably be to use a set, but I'm not sure if that's allowed under the "can't use other libraries" rule.
Using just arrays, for each item you could iterate over all the items before it, and only print it if it wasn't found there:
for (int i = 0; i < size; i++) {
bool found = false;
for (int j = 0; j < i && !found; j++) {
found = (myArray[i] == myArray[j]);
}
if (!found) {
cout << myArray[i] << " ";
}
}

The first occurrence of a repeated number has no occurrences before it and at least one after it.
This is reasonably easy to detect:
for (int i = 0; i < size; i++) {
bool before = false;
for (int j = 0; j < i && !before; j++) {
before = myArray[i] == myArray[j];
}
if (!before) {
bool after = false;
for (int j = i + 1; j < size && !after; j++) {
after = myArray[i] == myArray[j];
}
if (after)
{
cout << myArray[i] << " ";
}
}
}

Instead of two bools as the other user suggested I would use the counter basically doing the same thing but with one variable. The trick is to check if you have already had the number you are checking right now before so that you wouldn't print it again.
And then to check the rest of the list for duplicates.
#include <iostream>
int main()
{
unsigned size;
std::cin >> size;
int* myArray = new int[size];
for (int i = 0; i < size; i++) {
std::cin >> myArray[i];
}
for (int i = 0; i < size; i++) {
int count = 0;
//check if the number has already been found earlier
for (int j = 0; j < i && !count; j++) {
if(myArray[i] == myArray[j]) count++;
}
//check the rest of the array for the repeated number
if (!count) {
for (int j = i; j < size; j++) {
if(myArray[i] == myArray[j]) count++;
}
}
//print if repeated
if (count > 1) {
std::cout << myArray[i] << " ";
}
}
delete []myArray;
}

Related

I need to print this shape in c++ but I am having issue with last row

image of shape
I have no issues with first 4 rows. 5th row is the problem. I am required to use loops but dont know how i am suppose to print 6 (-.*) with 0 spaces when all rows above follow a pattern.
Something like this should work for you
for (int i = 1; i <= 6; i++) {
for (int j = 28 - i * 3; j >= 0; j--) {
std::cout << " ";
}
for (int j = 0; j < i; j++) {
std::cout << "-.*";
}
std::cout << std::endl;
if (i == 4) i++;
}
Basically check when you are on the 4th row and just have it skip a row by incrementing your row index loop.
Something like this?
std::string repeat(std::string s, int n) {
std::string repeat;
for (int i = 0; i < n; ++i)
repeat += s;
return repeat;
}
int main()
{
int PADDING = 20;
int MAX = 7;
for (int i = 1; i < MAX; ++i) {
if (i == 5) continue;
std::string padding(3*(PADDING-MAX-i), ' ');
std::cout << padding << repeat("-.*", i) << std::endl;
}
}
Output:
-.*
-.*-.*
-.*-.*-.*
-.*-.*-.*-.*
-.*-.*-.*-.*-.*-.*
Live demo:
http://cpp.sh/6mtpx
In C (not C++):
#include <stdio.h>
int main(void) {
int height = 5;
char txt[3*height+1];
char sym[]= "-.*";
for(int i=0; i<3*height; ++i) txt[i]=sym[i%3];
txt[3*height]=0;
for(int i=0; i<height; ++i)
printf("%s%*.*s\n", (i+1==height)? &txt[3*(height-1)] : "", 3*(height+(i+1<height)), (i+1)*3, txt);
return 0;
}
Output:
Success #stdin #stdout 0s 5476KB
-.*
-.*-.*
-.*-.*-.*
-.*-.*-.*-.*
-.*-.*-.*-.*-.*-.*
IDEOne Link

print duplicate elements in array

I want to find duplicate elements in a dynamic array. In most cases it works, but how can it work for this case. I'm finding duplicate elements in a given array. But there is one problem that my duplicate elements can repeat too. How can I solve that part.
#include <iostream>
int main() {
unsigned n;
std::cin >> n;
int count = 0;
int* dynArr = new int[n];
for (int i = 0; i < n; i++) {
std::cin >> dynArr[i];
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(dynArr[j] == dynArr[i]){
std::cout<<dynArr[j]<<" ";
break;
}
}
}
}
I have a problem with this part. When I'm inputting a my array length 6, and elements {1,1,2,1,2,2}.
I got (1,1,2,2).
But I need to get only 1,2.
input 6
1 1 2 1 2 2
output 1 1 2 2
but must be
output 1 2
Here's a simple and fast solution.
std::map<int,size_t> element_count;
for(int i = 0; i < n; i++){
if ( ++element_count[dynArr[i]] == 2 ){
// Only report on the second occurrance
std::cout<<dynArr[j]<<" ";
}
}
const int listSize = 5;
int list1[listSize] = {0,0,1,1,3};
bool dupList[listSize] = {false};
for (int index = 0; index < listSize; index++) {
int val = list1[index];
for (int i = 0; i < listSize; i++) {
if (i != index) {
if (list1[i] == val) {
dupList[index] = true;
}
}
}
}
int printList[listSize];
int addNum = 0;
for (int index = 0; index < listSize; index++) {
if (dupList[index] == true) {
bool run = true;
int print = list1[index];
for (int i = 0; i < listSize; i++) {
if (printList[i] == print) {
run = false;
}
}
if (run == true) {
std::cout << print << " ";
printList[addNum] = print;
addNum++;
}
}
}
Note this code will NOT run quickly at all only use it if the operation does not need to get run very many times but it will only display the single numbers that are duplicates. It will defernatly need to get optimised more
You can simply get it done using two std::set<int>s.
#include <set>
#include <iostream>
int main() {
unsigned n;
std::cin >> n;
int count = 0;
std::set<int> all;
std::set<int> redundant;
for (int i = 0; i < n; i++) {
int input = 0;
std::cin >> input;
// You cant enter the entry because its present. This will return a std::pair<,> with the second value false.
if (!all.insert(input).second)
redundant.insert(input); // We store this in another set so we dont duplicate the redundant entries.
}
for (auto itr = redundant.begin(); itr != redundant.end(); itr++)
std::cout << *itr << " ";
}

How do i add all the values in my ascending array?

First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.

Simplifying C++ For Loop

at the moment I have the following code:
for(int i = 0; i < 4; i++){
cout << rowNo[i] << endl;
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cout << rowNo[i] << '.';
cout << rowNo[j] << endl;
}
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
cout << rowNo[i] << '.';
cout << rowNo[j] << '.';
cout << rowNo[k] << endl;
}
}
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
for(int l = 0; l < 4; l++){
cout << rowNo[i] << '.';
cout << rowNo[j] << '.';
cout << rowNo[k] << '.';
cout << rowNo[l] << endl;
}
}
}
}
Where rowNo[] is an array {1,2,3,4}
And I was wondering two things:
Can this be simplified, so maybe put into some sort of recursive loop?
Following that, can this then be made for an array of size N?
Your looking for Cartesian_product
With
bool increment(std::vector<std::size_t>& v, std::size_t maxSize)
{
for (auto it = v.rbegin(); it != v.rend(); ++it) {
++*it;
if (*it != maxSize) {
return true;
}
*it = 0;
}
return false;
}
then you can do:
void print_cartesian_product(const std::vector<int>&v, int n)
{
std::vector<std::size_t> indexes(n);
do {
print(v, indexes);
} while (increment(indexes, v.size()));
}
Demo
You are actually trying to print a number encoded in base4 with digit {1, 2, 3, 4}. To achieve it, You only need to define a function to increment by one. I propose a generic solution in the term of amount of number to print and base.
Like others, I use a number to mean "empty digit", and I use zero which is quite convenient.
Complete source code :
#include <iostream>
#include <vector>
bool increment_basep(std::vector<int>& number, int p)
{
int i = 0;
while(i < number.size() && number[i] == p)
{
number[i] = 1;
++i;
}
if(i >= number.size())
return false;
++number[i];
return true;
}
void print_vect(std::vector<int>& number)
{
for(int i = number.size() -1 ; i >= 0; --i)
{
if(number[i] != 0)
std::cout << number[i];
}
std::cout << std::endl;
}
int main() {
int n = 4;
int p = 4;
std::vector<int> num4(n);
std::fill(num4.begin(), num4.end(), 0);
while(increment_basep(num4, p))
{
print_vect(num4);
}
return 0;
}
The increment return whether or not the computation has overflown. When we overflow we know we need to stop.
First solution it comes my mind is that on every loop to put in a buffer and finally to print all the buffers.
I think there are some other ingenious methods
for(int i = 0; i < 4; i++){
put in buffer1 rowNo[i]
for(int j = 0; j < 4; j++){
put in buffer2 rowNo[i],rowNo[j]
for(int k = 0; k < 4; k++){
put in buffer3 rowNo[i],rowNo[j],rowNo[k]
for(int l = 0; l < 4; l++){
put in buffer4 rowNo[i],rowNo[j],rowNo[k],rowNo[l],endl.
}
}
}
}
print(buffer1);
print(buffer2);
print(buffer3);
print(buffer4);
The following is the simplest code I came up with. There has to be a more direct way of doing this though...
It basically introduces a "ghost" index -1, corresponding to an empty place in a number. The ternary operators in the loops conditions are there to avoid duplicates.
int main()
{
int N = 4;
int rowNo[4] = {1, 2, 3, 4};
for (int i = -1; i < N; i++)
for (int j = (i > -1 ? 0 : -1); j < N; j++)
for (int k = (j > -1 ? 0 : -1); k < N; k++)
for (int l = (k > -1 ? 0 : -1); l < N; l++)
{
if (i > -1) std::cout << rowNo[i] << '.';
if (j > -1) std::cout << rowNo[j] << '.';
if (k > -1) std::cout << rowNo[k] << '.';
if (l > -1) std::cout << rowNo[l];
std::cout << std::endl;
}
}
It can of course be generalized to an array of arbitraty size, possibly with some code generation script.

'for' loops for making groups of combination

I have an array in c++ with unknown entries (minimum 6) i need a for loop (probably includes a few for loop) which makes 3 groups of 2. I don't care about order of groups or in the group. And tricky part is that double counting is not allowed. I tried something like this but it is obviously triple counts...
for(int i = 0; i < nArray - 1; i++)
{
for(int j = i+1; j < nArray; j++)
{
for(int k = 0; k < nArray - 1; k++)
{
for(int l = k+1; l < nArray; l++)
{
for(int m = 0; m < nArray - 1; m++)
{
for(int n = m+1; n < nArray; n++)
{
if(k!=i && k!=j && l!=i && l!=j && m!=i && m!=j && n!=i && n!=j && m!=k && m!=l && n!=k && n!=l)
{
std::cout << array[i] << "-" << array[j] << std::endl << array[k] << "-" << array[l] << std::endl << array[m] << "-" << array[n] << std::endl << std::endl;
}
}
}
}
}
}
}
Edit: for example let our array is {1,2,3,4,5,6} which has 6 entries. Output should look like:
12-34-56
12-35-46
12-36-45
13-24-56
13-25-46
13-26-45
14-23-56
14-25-36
14-26-35
15-23-46
15-24-36
15-26-34
16-23-45
16-24-35
16-25-34
But there should not be 12-43-56 or 34-12-56 since there is 12-34-56.
And for the array {1,2,3,4,5,6,7}
12-34-56
12-34-57
12-34-67
12-35-46
12-35-47
12-35-67
...
and so on.
Make it recursively. Choose two elements from the array to form a pair, remove them from the array and call the same function on reduced array. That way you'll have only two nested loops and you'll cover the whole array.
Alternatively, use additional array with information, which elements were already used instead of removing them from original array.
void GeneratePairs(std::vector<int> & values, std::vector<bool> & used, std::vector<std::pair<int, int>> & results)
{
int i = 0;
while (i < values.size() && used[i])
i++;
if (i != values.size())
{
used[i] = true;
for (int j = i + 1; j < values.size(); j++)
{
if (!used[j])
{
used[j] = true;
std::pair<int, int> tmp(values[i], values[j]);
results.push_back(tmp);
GeneratePairs(values, used, results);
results.pop_back();
used[j] = false;
}
}
used[i] = false;
}
else
{
for (int i = 0; i < results.size(); i++)
{
printf("%d,%d", results[i].first, results[i].second);
if (i < results.size() - 1)
printf("-");
}
printf("\n");
}
}
// (...)
int main(int argc, char * argv)
{
std::vector<int> input;
input.push_back(1);
input.push_back(2);
input.push_back(3);
input.push_back(4);
input.push_back(5);
input.push_back(6);
std::vector<bool> used;
for (int i = 0; i < input.size(); i++)
used.push_back(false);
std::vector<std::pair<int, int>> results;
GeneratePairs(input, used, results);
getchar();
}
I just want to mention I have figured out the simple detail which solves this problem:
for(int i=0;i<input.size();i++)
{
for(int j=i+1;j<input.size();j++)
{
for(int k=i+1;k<input.size();k++)
{
for(int l=k+1;l<input.size();l++)
{
if( j==k || j==l) continue;
for(int m=k+1;m<input.size();m++)
{
for(int n=m+1;n<input.size();n++)
{
if( m==j || m==l || n==j || n==l) continue;
std::cout << input[i] << input[j] << "-" << input[k] << input[l] << "-" << input[m] << input[n] << std::endl;
}
}
}
}
}
}