How to compare numbers from a stream if the same number repeats - c++

I am trying to get the same numbers that repeat to be writen to a different stream
the function void lygink
void lygink (int s,int e,int n,int a , int m,info K[],int &sk)
{
a=0;
for (int i=1; i<=m; i++)
{
if(K[i].j==K[i].l) ;
else {
e=K[i].j;
s=K[i].j;
if (e==s) {
cout << e<< endl;
}
}
}
}
The whole code is as follows:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct info
{
int jurg,lin,k;
};
void skaityk(int &n,int &m,int lin, int jurg, info K[])
{
{
ifstream fd ;
fd.open ("U2duom.txt");
fd >> n >> m;
for (int i=1; i<=n; i++)
{
fd>>K[i].lin ;
cout<<fixed<<setprecision(2)<<" " <<K[i].lin;
}
cout << " "<< endl;
for (int i=1; i<=m; i++)
{
fd>>K[i].jurg ;
cout<<fixed<<setprecision(2)<<" " <<K[i].jurg;
}
cout << " "<< endl;
fd.close();
}
}
void lygink (int s,int e,int n,int a , int m,info K[],int &k)
{
a=0;
for (int i=1; i<=m; i++)
{
if(K[i].jurg==K[i].lin) ;
else {
e=K[i].jurg;
s=K[i].jurg;
if (e==s) {
cout << e<< endl;
}
}
}
}
int main()
{
int n,e,m,a,lin,jurg,s,k;
s=22;
info K[500];
skaityk(n, m,lin, jurg, K);
lygink(s,e,n,a,m,K,k);
int i;
return 0;
}
and the file is
8 14
5 6 6 9 14 25 8 26
5 20 6 7 13 7 9 10 12 20 15 16 21 5
It should only give me 7 and 20 but it gives all that are not the same and I can't figure out how to extract those numbers I need to eliminate the numbers that do not repeat or somehow get the numbers that repeat

I would do the job somewhat differently. As a first attempt at things, I'd probably do something on this general order:
#include <iostream>
#include <set>
#include <sstream>
int main() {
std::istringstream input(R"(
8 14
5 6 6 9 14 25 8 26
5 20 6 7 13 7 9 10 12 20 15 16 21 5
)");
std::set<int> numbers;
int n;
while (input >> n)
if (!numbers.insert(n).second)
std::cout << n << "\n";
}
This produces more output than just 7 and 20, but hand inspection seems to confirm that everything it says is a duplicate actually is.

Use std::sort to sort the array and std::adjacent_find to find duplicates. A good programmer uses the available tools and doesn't reinvent the wheel every time.
std::unordered_set<int> unique(std::vector v) {
std::sort(std::begin(v), std::end(v));
auto it = std::adjacent_find(std::begin(v), std::end(v));
std::unordered_set<int> ret;
while (it != std::end(v)) {
ret.insert(*it);
std::advance(it, 2);
it = std::adjacent_find(it, std::end(v));
}
return ret;
}

Related

Bubble sort the numbers from a file in descending order for the first number

I want to sort using the "Bubble Sort" algorithm of the 2d array. My array size will be about array[100000][100000]. my input number will be n=100,000.
For now we can use a small size of the array to fix the sorting issue.
I need to sort them in descending order for the first number(first number's line).
If the first number of 2 values are the same, then I have to sort them according to their second number.
Finally I have to output the result into a txt file
Let's' understand using an example. Here, my input looks like this
41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10
above our input example and we have a couple of inputs. Now I need to sort them descending orders for the first number. But if the first number of 2 values are the same, then sort them according to their second number.
Example output below,
81 3
78 6
69 4
62 8
41 4
34 4
5 10
5 5
If anyone can please help me.
I am a beginner so I am trying to input the file manually to solve this sorting problem. I can solve the sorting problem then I will try to input and out the text.
Something I have tried but not worked. I am still trying to solve it.
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
int main ()
{
int arr[100][100];
int n,j;
cin >>n;
cout << "Please enter a number: " << endl;
for(int i=0;i<n;i++)
{ for (int j=i; j<n; j++)
{
cin>>arr[i][j];
}
}
cout << "Unsorted array:" << endl;
for (int i=0; i<n; i++)
{
for (int j=i; j<n; j++)
{
cout<<arr[i][j]<<"\t";
}
}
for (int i=0; i<=n; i++)
{
for (int j=i+1; j<=n-1; j++)
{
int temp;
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return 0;
}
Use a std::vector<std::array<int,2>>for your base container. The dynamic growth capabilities of std::vector solves your stack space issue, and the std::array use gives you tied cell comparison. I.e. you can do this:
std::array<int, 2> ar1{1,2}, ar2{1,3};
if (ar1 < ar2) ...
and it will do the right thing. The result then boils down to effectively this:
#include <iostream>
#include <array>
#include <vector>
#include <utility>
int main()
{
std::vector< std::array<int,2> > v;
std::size_t n;
if (std::cin >> n && n > 0)
{
std::array<int,2> row;
while (n-- && std::cin >> row[0] && std::cin >> row[1])
v.emplace_back(row);
// bubblesort the content
std::size_t len = v.size();
while (len-- > 0)
{
bool swapped = false;
for (std::size_t i=0; i<len; ++i)
{
// std::array support multi-cell comparison.
if (v[i] < v[i+1])
{
// use library swap to swap entire row.
std::swap(v[i], v[i+1]);
swapped = true;
}
}
// early exit if no swaps happened on the last pass
if (!swapped)
break;
}
// report final output.
for (auto const& row : v)
std::cout << row[0] << ' ' << row[1] << '\n';
}
}
Input
8
41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10
Output
81 3
78 6
69 4
62 8
41 11
34 4
5 10
5 5

Sorting arrays and in output getting extra first line of random symbols

I wrote a program where program sorts people by the each person time, and if time is the same, program sorts by the name alphabetically. Everything works fine, just I am getting extra first line full of random symbols. I of course can write if function and say not to show that first line, but is it good thing to do?
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
struct people {
string full_name;
int h;
int min;
int sec;
};
bool comp(const people &p1, const people &p2) {
return (p1.min < p2.min || p1.min == p2.min && p1.sec < p2.sec ||
p1.min == p2.min && p1.sec == p2.sec && p1.full_name < p2.full_name);
}
int main() {
int time;
string fullnamechange[30];
people peo[30];
cin >> time;
for (int i = 0; i < time; i++) {
getline(cin, peo[i].full_name); // taking everything into a string
fullnamechange[i] = peo[i].full_name;
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() - 8);
peo[i].h = atoi(fullnamechange[i].c_str());
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() -
5); // changing time in string to int
peo[i].min = atoi(fullnamechange[i].c_str());
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() - 2);
peo[i].sec = atoi(fullnamechange[i].c_str());
}
for (int i = 0; i < time; i++) { // erasing time from string
peo[i].full_name.erase(peo[i].full_name.begin() + 20,
peo[i].full_name.end());
}
sort(peo, peo + time, comp);
cout << endl;
for (int i = 0; i < time; i++) {
cout << peo[i].full_name << " " << peo[i].min << " " << peo[i].sec << endl;
}
return 0;
}
/*
input for example:
6
Petras A. Petraitis 0 20 00
Jurgis Jurgutis 0 12 59
Romas Jonas 0 15 12
Zigmas Nosis 0 23 9
Rimas Senasis 0 15 12
output I get:
em3╣Mg n Ç 0 0 //random numbers I get
Jurgis Jurgutis 12 59
Rimas Senasis 15 12
Romas Jonas 15 12
Petras A. Petraitis 20 0
Zigmas Nosis 23 9 */

How do I produce a count down using a nested for loop?

I want the user to input an integer, and then I want my program to be able to count down to zero starting from that number using a nested For loop statement. For example:
user enters 20
20
19 18
17 16 15
14 13 12 11
10 9 8 7 6
5 4 3 2 1 0
I also want the numbers to be printed out in a half pyramid structure, similar to the example shown above. Here is my attempt to solve this problem. Note that I've used a variable and set it to 20, just to test the program. If you can also explain my errors and explain how to make different patterns like a full pyramid or an inverted pyramid, that would be greatly appreciated.
#include <stdio.h>
int main()
{
int sum, i, j, number=20;
for (i=1;i<=20;++i)
{
sum = number-1 ;
for (j=1;j<=i;j++)
{
printf("%d ",sum);
--number;
}
printf("\n");
}
return 0;
}
This is for the half pyramid as given in your example
#include <iostream>
int main()
{
int sum = 20;
for(int i = 1; sum != -1;i++) {
for(int j = 1; j <= i; j++) {
std::cout << sum-- <<' ';
if (sum == -1) break;
}
std::cout << std::endl;
}
}
#include <stdio.h>
int main()
{
int sum, i, j, number=20;
for (i=1;i<=20;++i)
{
**sum = number** ;
for (j=1;j<=i;j++)
{
printf("%d ",sum);
--number;
}
printf("\n");
}
return 0;
}

Create all possible combinations using a deque and recursion C++

I have a deque that contains a series of numbers {0, 1, 2, 3, 4, 5, 6} and I am trying to create all possible combinations of these numbers using recursion.
Here is my current code
void combination(vector<node> &comb, deque<node> &numbers) {
if (numbers.empty()) {
for (unsigned int i = 0; i < comb.size(); i++) {
cout << comb[i].id << " ";
}
cout << "\n";
return;
}
comb.push_back(numbers.front());
numbers.pop_front();
combination(comb, numbers);
comb.pop_back();
combination(comb, numbers);
}
I've ran this through on paper and it makes sense but when I run it this is the output:
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Why isn't the function printing out all possible combinations?
Also, this is what I want to use - A deque that contains the numbers and a vector that contains each combination.
You are using Pass by reference, i have made some minor changes and it works
code :
#include <bits/stdc++.h>
using namespace std;
void combination(vector<int> comb, deque<int> numbers) {
if (numbers.empty()) {
for (unsigned int i = 0; i < comb.size(); i++) {
cout << comb[i] << " ";
}
cout << "\n";
return;
}
comb.push_back(numbers.front());
numbers.pop_front();
combination(comb, numbers);
comb.pop_back();
combination(comb, numbers);
}
int main() {
// your code goes here
vector<int> comb;
deque<int> numbers;
for(int i = 0;i < 7;i++) numbers.push_back(i);
combination(comb, numbers);
return 0;
}
Link to solution on ideone : http://ideone.com/vgukF3

Sorting multidimensional array C++ strange behaviour

I'm doing some programming practice right now by trying to sort a 2D array on it's first "column".
I am reading input from a file:
100 5
8 80
5 20
9 40
3 10
6 30
This is my code:
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool helper(vector<long> k, vector<long> l)
{
return (k[0] < l[0]);
}
int main()
{
ifstream fi("milk.in");
ofstream fo("milk.out");
long price = 0, n, m, i, p, a;
vector< vector<long> > farmers;
vector<long> farmer(2,0);
fi >> n >> m;
for (i=0; i<n; ++i)
{
fi >> p >> a;
farmer[0] = p;
farmer[1] = a;
farmers.push_back(farmer);
}
sort(farmers.begin(),farmers.end(),helper);
for (i=0; i<m; ++i)
{
cout << farmers[i][0] << " " << farmers[i][1] << endl;
}
return 0;
}
As you can see, I try to sort the input by it's first column (I don't care about the first line at the moment).
However, this is the result:
3 10
5 20
6 30
6 30
6 30
This is the expected result:
3 10
5 20
6 30
8 80
9 40
I can't figure it out.
Your first line of your milk.in:
100 5
You're going to end up looping through trying to read in 100 inputs from this file because n = 100.
fi >> n >> m;
for (i=0; i<n; ++i)
If you change milk.in to:
5 5
8 80
5 20
9 40
3 10
6 30
That seems to work.
Perhaps a better idea is to just check if you're done reading input from the filestream:
for(i = 0; i < n; ++i)
{
if(!(fi >> farmer[0] >> farmer[1])) break;
farmers.push_back(farmer);
}
You need this loop to load the data from the file in:
And remember to close the file.
while(!fi.eof())
{
fi >> n >> m;
farmer[0] = n;
farmer[1] = m;
farmers.push_back(farmer);
}
fi.close( );
Also, I made some minor changes to your code:
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool helper(vector<long> k, vector<long> l)
{
return (k[0] < l[0]);
}
int main()
{
long price = 0, n, m, i, p=0, a=0,numlines=0;
vector< vector<long> > farmers;
vector<long> farmer(2,0);
cout<<"\nLoading data from file: milk.in\n\n";
ifstream fi("milk.in");
while(!fi.eof())
{
fi >> n >> m;
numlines++;
cout<<n<<" "<<m<<"\n";
farmer[0] = n;
farmer[1] = m;
farmers.push_back(farmer);
}
fi.close( );
cout<<"--- "<<numlines<<" lines loaded\n";
cout<<"\n---------------------\n";
cout<<"\nSorted data:\n\n";
sort(farmers.begin(),farmers.end(),helper);
ofstream fo("milk.out");
for (i=0; i<numlines; ++i)
{
cout << farmers[i][0] << " " << farmers[i][1] << endl;
fo<< farmers[i][0] << " " << farmers[i][1] << endl;
}
fo.close();
cout<<"\n---------------------\n";
return 0;
}
Output:
Sorted data:
3 10
5 20
6 30
8 80
9 40
100 5