Well my program is working with huge digits and I should wait to much for example calculating 20! takes too much time ( calculating all permutation).
How can I output first 20 results not all 20? I tried to capture this while the program was running in icon, but I can't.
Here is my code
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <vector>
#include <fstream>
using namespace std;
int X[100];
LARGE_INTEGER start, finish, freq;
int N;
int count_number;
vector<int> numb;
vector<int> cycle;
bool search (vector<int> source, int data)
{
for (int i=0; i<source.size(); i++)
if (source[i] == data)
return true;
return false;
}
void fill(int find)
{
find++;
int data = X[find-1];
int temp = find;
while (data != find){
temp = data;
data = X[temp-1];
numb.push_back(temp);
cycle.push_back(temp);
}
if (data == find) {
numb.push_back(find);
cycle.push_back(find);
}
}
void Swap(int a,int b)
{
int t=X[a];
X[a]=X[b];
X[b]=t;
}
void Generate(int k)
{
if (k==N)
{
for(int i=0;i<N;i++)
cout<<X[i]<<" ";
cout<<"Циклы: ";
for (int i=0; i<N; i++) {
if (!search(numb, i+1)) {
fill(i);
cout<<'(';
for (int i=0; i<cycle.size(); i++) {
cout<<cycle[i];
if(i != cycle.size()-1)
cout<<',';
}
cout<<") ";
cycle.clear();
}
}
numb.clear();
cout<<"\n";
count_number++;
}
else
{
for(int j=k;j<N;j++)
{
Swap(k,j);
Generate(k+1);
Swap(k,j);
}
}
}
int main()
{
setlocale(LC_CTYPE,"English");
cout<<"N=";
cin>>N;
QueryPerformanceFrequency( &freq );
QueryPerformanceCounter( &start );
for(int i=0;i<N;i++)
X[i]=i+1;
Generate(0);
cout<<endl;
cout<<endl;
QueryPerformanceCounter( &finish );
double time = (finish.QuadPart - start.QuadPart) / (double)freq.QuadPart;
cout<<"Algorith time:"<<time<<" \t seconds"<<endl;
cout<<"All permutation:"<<count_number<<endl;
FILE myfile;
cin.get();
getch();
return 0;
}
included
created FILE mmm.
how to make?
Related
This question already has an answer here:
Clion exit code -1073741571 (0xC00000FD)
(1 answer)
Closed 2 years ago.
The c++ code below works fine for some inputs, but it is stuck at test 9 (number of inputs here is 6000) where it gives me this message "Process returned -1073741571 (0xC00000FD)".
This code reads information for n babies (their gender and name). Next it counts the appearances of each name then sorts the list of structures according to the appearances. Finally, it removes the duplicates and prints the top m female names and top m male names.
What does this error mean and what do I need to change to eliminate this error?
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string.h>
using namespace std;
ifstream fin("input.txt");
struct baby
{
string gender,name;
int cnt;
};
bool cmp(baby a,baby b)
{
if (a.cnt>b.cnt)
return true;
else if (a.cnt==b.cnt && a.name<b.name)
return true;
return false;
}
int howmany(baby babies[],int n,int i)
{
int cnt=0;
for (int j=0; j<n; j++)
{
if (babies[i].name==babies[j].name && babies[i].gender==babies[j].gender)
{
cnt++;
}
}
return cnt;
}
void getData(baby babies[],int n)
{
for (int i=0; i<n; i++)
{
fin>>babies[i].gender>>babies[i].name;
}
}
int removeDuplicates(baby babies[],int n)
{
int j=0;
for (int i=0; i<n-1; i++)
{
if (babies[i].name!=babies[i+1].name)
babies[j++]=babies[i];
}
babies[j++]=babies[n-1];
return j;
}
int main()
{
int n,i,top,j;
fin>>n>>top;
baby babies[50000];
getData(babies,n);
for (i=0; i<n; i++)
{
babies[i].cnt=howmany(babies,n,i);
}
sort(babies,babies+n,cmp);
j=removeDuplicates(babies,n);
int cnt=0;
for (int i=0; i<j; i++)
{
if (cnt<top)
{
if (babies[i].gender=="F")
{
cout<<babies[i].name<<" ";
cnt++;
}
}
}
cout<<endl;
cnt=0;
for (int i=0; i<j; i++)
{
if (cnt<top)
{
if (babies[i].gender=="M")
{
cout<<babies[i].name<<" ";
cnt++;
}
}
}
return 0;
}
As you can see in Window's NT status reference, error code 0xC00000FD means stack overflow (usually caused by infinite recursion). In your case, it seems that you simply allocate a far too large array on the stack (line 57, baby babies[50000];), which is an array of size 50000*20=1000000. The simplest solution will be a dynamic allocation
baby* babies = new baby[50000];
// Your code here
delete[] babies;
A better solution would be to use std::vector which is a dynamic array that can grow and shrink. The simplest thing to do is to take a vector of size 50000, this way:
#include <vector>
...
std::vector<baby> babies(50000);
However, this is a poor solution as your pre-allocate 50000 elements even though you probably need much much less, and a better solution would be to add an element on-demand, using .push_back(element) method, or in your case, allocate n elements to the vector (impossible in a stack-allocated array).
I added your code with some modifications of mine:
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("input.txt");
struct baby
{
string gender;
string name;
int cnt = 0;
};
bool cmp(const baby& a, const baby& b)
{
if (a.cnt > b.cnt) {
return true;
}
return a.cnt == b.cnt && a.name < b.name;
}
bool are_equal(const baby& lhs, const baby& rhs)
{
return lhs.gender == rhs.gender && lhs.name == rhs.name;
}
int howmany(const std::vector<baby>& babies, int i)
{
int cnt = 0;
for (int j = 0; j < babies.size(); j++)
{
if (babies[i].name == babies[j].name && babies[i].gender == babies[j].gender)
{
cnt++;
}
}
return cnt;
}
void getData(std::vector<baby>& babies)
{
for (int i = 0; i < babies.size(); i++)
{
fin >> babies[i].gender >> babies[i].name;
}
}
int removeDuplicates(std::vector<baby>& babies)
{
int j = 0;
for (int i = 0; i < babies.size() - 1; i++)
{
if (babies[i].name != babies[i + 1].name) {
babies[j++] = babies[i];
}
}
babies[j++] = babies.back();
return j;
}
void remove_duplicates_improved(std::vector<baby>& babies)
{
babies.erase(babies.begin(), std::unique(babies.begin(), babies.end(), are_equal));
}
int main()
{
int n;
int top;
fin >> n >> top;
std::vector<baby> babies(n);
getData(babies);
for (int i = 0; i < n; i++)
{
babies[i].cnt = howmany(babies, i);
}
sort(babies.begin(), babies.begin() + n, cmp);
remove_duplicates_improved(babies);
int cnt = 0;
for (int i = 0; i < babies.size(); i++)
{
if (cnt < top)
{
if (babies[i].gender == "F")
{
cout << babies[i].name << " ";
cnt++;
}
}
}
cout << endl;
cnt = 0;
for (int i = 0; i < babies.size(); i++)
{
if (cnt < top)
{
if (babies[i].gender == "M")
{
cout << babies[i].name << " ";
cnt++;
}
}
}
return 0;
}
Good luck
I need to write a program that get a sentence and split its words by a delimiter(space);so I've wrote the code below but it doesn't seem to be working properly.
any idea's how to debug this code?
thanks in advance for your help.
here's what I come up with so far:
#include <iostream>
using namespace std;
const int BUFFER_SIZE=255;
int main()
{
char* buffer;
buffer=new char[255];
cout<<"enter a statement:"<<endl;
cin.getline(buffer,BUFFER_SIZE);
int q=0, numofwords=1;
while(buffer[q] != '\0'){
if(buffer[q]==' ') numofwords ++;
q ++;
}
char** wordsArray;
wordsArray= new char* [numofwords];
int lenofeachword=0, num=0;
int* sizeofwords=new int [numofwords];
for(int i=0;i<q;i++){
if(buffer[i]==' ')
{
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
num++;
}else{
lenofeachword++;
}
}
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
int k=0;
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
wordsArray[i][j]=buffer[k];
k++;
}
k++;
}
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
cout<<wordsArray[i][j];
}
cout<<endl;
}
}
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main() {
int size, j;
char s[1005];
gets(s);
scanf("%d", &size);
j=0;
for(int i=size; i<strlen(s); i+=size) {
for(; j<i; j++) {
printf("%c", s[j]);
}
printf(" ");
}
return 0;
}
You forgot to assign zero to lenofeachword after you deretmine lenght of a word. And if int main(), you should return an int value.
I am trying to convert this code that works for '0' - '3' strings to integer so that it will work for higher numbers
#include <string>
#include <iostream>
using namespace std;
void permutate(char[], int );
bool recurse(char[], int );
int main()
{
int strLength;
cout << "Enter your desired length: ";
cin >> strLength;
char strArray[strLength];
for (int i = 0; i<strLength; i++)
strArray[i] = '0';
permutate(strArray, sizeof(strArray));
return 0;
}
void permutate(char charArray[], int length)
{
string wait;
length--;
bool done = false;
while(!done)
{
for (int i = 0; i <= length; i++)
cout << charArray[i];
cout << endl;
if (charArray[length] == '3')
done = recurse(charArray, length);
else
charArray[length] = (char)(charArray[length]+1);
}
}
bool recurse(char charArray[], int length)
{
bool done = false;
int temp = length;
if (temp > 1)
{
charArray[temp] = '0';
if (charArray[temp-1] == '3')
{
temp--;
done = recurse(charArray, temp);
}
else
(charArray[temp-1] = (char)(charArray[temp-1] + 1));
}
else
{
charArray[temp] = '0';
if (charArray[temp-1] == '3')
done = true;
else
charArray[temp-1] = (char)(charArray[temp-1]+1);
}
return done;
}
I changed every char to int,
every '0' = 0, '3' = 3
every (charArray[temp-1] = (char)(charArray[temp-1] + 1)); to charArray[temp-1]++;
I tried to debug but I still can`t make it work :(
Manged to fix it( works for high numbers):
#include <string>
#include <iostream>
using namespace std;
void permutate(int[], int, int );
bool recurse(int[], int, int );
int main()
{
int strLength, nrElem;
cout << "Enter your desired length: ";
cin >> strLength;
cout << "Enter nr elem: ";
cin >> nrElem;
int strArray[strLength];
for (int i = 0; i<strLength; i++)
strArray[i] = 0;
permutate(strArray, strLength, nrElem );
cout << "\nSTOP";
return 0;
}
void permutate(int charArray[], int length, int nrElem)
{
// length--;
bool done = false;
while(!done)
{
for (int i = 0; i < length; i++)
cout << charArray[i] << " ";
cout << endl;
if (charArray[length - 1] == nrElem)
//done = true;
done = recurse(charArray, length, nrElem);
else
charArray[length - 1]++;
}
}
bool recurse(int charArray[], int length, int nrElem)
{
bool done = false;
int temp = length ;
if (temp > 1)
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
{
temp--;
done = recurse(charArray, temp, nrElem);
}
else
charArray[temp-1]++;
}
else
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
done = true;
else
charArray[temp-1]++;
}
return done;
}
In your permutate function, you're incrementing charArray[length] but checking to see if charArray[length - 1] is equal to nrElem, so you never end up calling recurse.
Here is a short piece of code to do the same (not an answer, however it did not look right in a comment field), not sure if you need the recursion, if you do not this code may be of interest:
#include <iostream>
#include <sstream>
using namespace std;
string output(int firstIntSize, int secondIntSize)
{
std::ostringstream oss;
for (int i = 0; i<firstIntSize; i++)
{
for (int j = 0; j< secondIntSize; j++)
{
oss << i << j << " ";
}
}
return oss.str();
}
int main()
{
cout << output(2,3);
return 0;
}
hmmm... Why not simply make a Permutations algorithm and then use a generic function to print whatever you are permutating. Here's how I would do it for strings:
#include <iostream>
#include <string>
template<class T>
void print(T * A, unsigned n){ //for printing purposes
for(unsigned i=0;i<n;i++){
std::cout<<A[i]<<" ";
}
std::cout<<std::endl;
}
void generate_permutations(unsigned k, std::string str, char *A, bool *U){
// k is the position that we need to fill, starts from 0 and goes to the end.
if(k<str.size()) //if k==str.size() then we will print it
for(unsigned i=0;i<str.size();i++){
if(U[i]==0){
A[k]=str[i]; U[i]=1;
generate_permutations(k+1, str, A,U);
U[i]=0; //after the recursion is finished and printed, we can release the letter.
}
}
else
print(A,str.size());
}
int main(){
std::string str;
std::cout<<"Enter the string to be permutated: \n";
std::cin>>str;
int n;
n = str.length(); // You don't really need to ask the user the size of the string he/she wants to enter.
bool *U; // we will keep track of the used letters with the help of this boolean vector
char *A; // we will copy the contents of str here, so that we keep the str intact
U = new bool[n];
for (int i=0;i<n;i++) U[i]=false;
A = new char[n];
for (int i=0;i<n;i++) A[i]=str[i];
generate_permutations(0,str,A,U);
return 0;
}
Now if you want to convert to numbers (ints), it's almost the same:
#include <iostream>
template<class T>
void print(T * A, int n){
for(int i=0;i<n;i++){
std::cout<<A[i]<<" ";
}
std::cout<<std::endl;
}
void generate_permutations(int k, int *A, bool *U, int n){
if(k==n)
print(A,n);
else {
for(int i=0;i<n;i++){
if(U[i]==0){
A[k]=i; U[i]=1;
generate_permutations(k+1,A,U,n);
U[i]=0;
}
}
}
}
int main(){
int n;
std::cout<<"Permutations of how many objects? \n";
std::cin>>n;
int * A;
bool *U;
A = new int[n];
U = new bool[n];
for (int i=0;i<n;i++) U[i]=false;
print(U, n);
generate_permutations(0,A,U,n);
return 0;
}
After reading through the theory of Merge Sort on TopCoder, I tried to write it's implementations, but it's getting weird, and I'm more or less a beginner in programming, especially algorithms. Can somebody assist me?
#include <iostream>
using namespace std;
int arr[] = {2, 0, 43, 12, 98};
int sizeOfarr(int a[])
{
return sizeof(a)/sizeof(a[0]);
}
int minElement(int x, int y)
{
if (x > y)
{
return y;
}
else if (x < y)
{
return x;
}
else
{
return x, y;
}
}
int main()
{
int t, z;
int n = sizeOfarr(arr);
int finalList[n];
int list1[n];
int list2[n];
for(int i = 0; i<=((n/2)-1); i++)
{
list1[i] = arr[i];
}
for(int j = n/2; j<n; j++)
{
for(int k = 0; k<=((n/2)-1); k++ )
{
list2[k] = arr[j];
}
}
for(int y = 0; y<=n; y++)
{
while(sizeOfarr(finalList)!=n)
{
t = list1[0];
z = list2[0];
finalList[y] = minElement(t, z);
if(finalList[y]==t)
{
list1[0] = list1[1];
}
else if(finalList[y]==z)
{
list2[0] = list2[1];
}
else
{
list1[0] = list1[1];
list2[0] = list2[1];
}
}
}
cout << "The sorted list is: " << finalList << endl;
return 0;
}
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int temp[10000];
void merge(int *A,int low,int mid,int high)
{
int i=low;
int j=mid+1;
int k=low;
int l;
while(i<=mid && j<=high)
{
if(A[i]<A[j])
{
temp[k]=A[i];
i=i+1;
}
else
{
temp[k]=A[j];
j=j+1;
}
k++;
}
for(l=i;l<=mid;l++,k++)
{
temp[k]=A[l];
}
for(l=j;l<=high;l++,k++)
{
temp[k]=A[l];
}
memcpy(A,temp,sizeof(A[0])*k);
}
void mergeSort(int *A,int low,int high)
{
int mid;
if(low<high)
{
mid=floor((low+high)/2);
mergeSort(A,low,mid);
mergeSort(A,mid+1,high);
merge(A,low,mid,high);
}
}
int main(int argc,char *argv[])
{
int n;
int array[10000];
cout<<"please enter the number numbers\n";
cin>>n;
cout<<"please enter the nubers\n";
for(int i=0;i<n;i++)
{
cin>>array[i];
}
mergeSort(array,0,n-1);
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
cout<<"\n";
}
This is my implementation
mergeSort function divide recursively at middle and repeats until low lt(less than) high then a merge function is called.
I see from your code that the operator "," (return x,y) would replace x value by y value.
A few comments on the code:
return x,y // this just returns y. this is the case when x==y so it probably is OK bit not what one would write.
while(sizeOfarr(finalList)!=n) // The size of your array finalist is n elements. This is never going to change so this while condition is always false and the loop will never execute.
I'm trying to implement this sorting algorithm: http://en.wikipedia.org/wiki/Library_sort. I have some functions grupBul for finding suitable group, grupSirala for sort every group. I have to leave some gaps and contain them in a vector another data structure.
How can I perform this? So far I have this code:
#include "librarySort.h"
#include <iostream>
#include <vector>
#include <math.h>
#include "sort.h"
using namespace std;
#define EPSILON 10 //Epsilon sabiti
#define GRUPDEFBOYUT 20 //Gruplarin baslangiç boyutu
//gruplar[grupno][degerler] grupboyutu->[0] minimumeleman->[1] maksimumeleman->[2]
vector<vector<int> > gruplar;
int grupBul(vector<int*> &dizi,int sayi){
for(int i=0;i<gruplar.size();i++){
if(*dizi[gruplar[i][1]]<sayi && *dizi[gruplar[i][2]]>sayi){
return gruplar[i][2];
}
}
for(int i=gruplar.size()-1;i>=0;i--){
if (*dizi[gruplar[i][2]]<sayi){
return gruplar[i][2];
}
}
if(*dizi[gruplar[0][1]]>=sayi){
return gruplar[0][2];
}
}
int maxBul(vector<int*> &dizi,int baslangic,int son){
int max=*dizi[baslangic];
int index=baslangic;
for(int i=baslangic+1;i<son;i++){
if(*dizi[i]>max){
max=*dizi[i];
index=i;
}
}
return index;
}
int minBul(vector<int*> &dizi,int baslangic,int son){
int min=*dizi[baslangic];
int index=baslangic;
for(int i=baslangic+1;i<son;i++){
if(*dizi[i]<min){
min=*dizi[i];
index=i;
}
}
return index;
}
void ilkGruplariOlustur(vector<int*> &dizi,int son){
int grupboyut=0;
int grupno=0;
for(int i=0;i<5;i++){
vector<int> grup;
grup.push_back(GRUPDEFBOYUT);
grup.push_back(minBul(dizi,grupboyut,GRUPDEFBOYUT+grupboyut));
grup.push_back(maxBul(dizi,grupboyut,GRUPDEFBOYUT+grupboyut));
gruplar.push_back(grup);
/*for(int j=0;j<EPSILON;j++)
dizi.push_back(NULL);*/
grupboyut+=GRUPDEFBOYUT;
grupno++;
}
}
void grupSirala(vector<int*> &dizi,int baslangic,int son){
int j = 0;
int mover;
for ( int i = baslangic + 1 ; i < son ; i++ ) {
mover = *dizi[i];
j = i-1;
while ( ( j >= 0 ) && ( *dizi[j] > mover) ){
*dizi[j+1] = *dizi[j];
j--;
}
*dizi[j+1] = mover;
}
}
void bosluklariOlustur(vector<int*> &dizi){
}
int librarySort( int *data , unsigned int size) {
int kok_n=(int)sqrt((double)size);
int boyut=size;
vector<int*> sirali_dizi;
for(int i=0;i<kok_n;i++){
sirali_dizi.push_back(new int(*(data+i)));
}
ilkGruplariOlustur(sirali_dizi,0);
for(int i=0;i<sirali_dizi.size();i++)
cout << *sirali_dizi.at(i)<< " ";
for(int a = 0;a<10;a++)
sirali_dizi.push_back(NULL);
grupSirala(sirali_dizi,0,kok_n);
for(int i=0;i<5;i++){
for(int j=0;j<3;j++)
cout << gruplar[i][j] << " ";
}
/*
cout<<endl<<"Sirali dizi"+i<<endl;
for(int t=0;t<20*(i+1)+EPSILON;t++)
cout << *sirali_dizi.at(t)<< " ";
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<endl;
grupSirala(sirali_dizi,0,sirali_dizi.size());*/
for(int i=0;i<sirali_dizi.size();i++)
cout << *sirali_dizi.at(i)<< " ";
return 0;
}
Well, vector has a resize method that will allow you to basically preset the vector's size before inserting data. Then from there you'll be able to use the operator[] to insert data at the appropriate spaces.
As a side note, I personally would not use STL if I'm working on this project/homework.