How to change the order of an array in c++ - c++

I am try to change the order of an array of char like this :
char arr_char[]="ABCDEFGHIJABCDEFGHIJ";
i used the rand() function in the following code :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char arr_char[]="ABCDEFGHIJABCDEFGHIJ";
int arrSize=sizeof(arr_char)-1;
srand(time(0));
for(int i=0;i<20;i++)
{
cout<<arr_char[rand() % arrSize]<<" ";
}
}
but the rand function repeat some characters more than twice and i want to change the order of the array in which every characters repeat only twice not more .

This will probably suffice
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string arr_char = "ABCDEFGHIJABCDEFGHIJ";
random_shuffle(arr_char.begin(), arr_char.end());
cout << arr_char;
}

Related

how can I use string variable in system("say string variable")?

/* I thought of doing in this way, but it invalid.
so any help will be appreciated. */
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Karma";
string greet = "How was your day?";
string sums = name + greet;
system("say %s", sums) // not working
// system("say sums") not working
return 0;
}
You can use:
system(("say" + sums).c_str())
Instead of:
system("say %s", sums)

Segmentation fault in reversing string program

I am trying to reverse a string. Can someone explain me why this is giving me segmentation fault?
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
int len=str.length(),i=0;
cin>>str;
while(str[i]!='\0'){
rstr[--len]=str[i++];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}
P.S.: Need to reverse it without using library functions.
If you want to go the way you are doing it, for practice purposes, try this changes and start from there
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
cin>>str; // --- Moved this line up
rstr = str; // --- Added this line
int len=str.length(),i=0;
while(str[i]!='\0'){
rstr[--len]=str[i++];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}
Or just use reverse iterator
std::string s = "Hello";
std::string r(s.rbegin(), s.rend());
str is nothing but a declared string here:
int len=str.length(),i=0;
So you can't do str.length()
Do something like:
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
int len,i=0;
cin>>str;
len = str.length();
while(str[i]!='\0'){
rstr[i++]=str[--len];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}

Several links errors in my project: LNK2019, LNK2005

I tried every solution for those errors in google, but found no answer for this problem.
The project is really big, but here is one of the files:
cpp file:
#include"Cluster.h"
Cluster::Cluster()
{}
Cluster::~Cluster() //close files and reomve dynamically-allocated memory
{
Splittedfile.close();
clustring.close();
Linefile.close();
delete[] protein;
delete[] NextProtein;
}
void spllitFile()
{
// check length of the file, and set length of NumOfSeq
Linefile.seekg(0, Linefile.end);
long sizeOfFile = Linefile.tellg();
NumOfProteins = sizeOfFile - 20;
//from the begining of LineFile:
//read 1 protein from LineFile & write it to Splittedfile.
//Each loop is advaced with:
// /n to Splittedfile & another 1 character "slide" in LineFile.
Linefile.seekg(ios_base::beg);
char* CopyProtein = new char[20]; // allocate buffer for reading 1 protein
long startPlaceOfRead = 0;
while (!(Linefile.eof()))
{
if ((startPlaceOfRead != 0) || (((sizeOfFile - startPlaceOfRead) < 20.0)))
Splittedfile << "\n";
Linefile.seekg(startPlaceOfRead);//to next protein - one for enter. one for back
if ((sizeOfFile - startPlaceOfRead) < 20.0) break;//if not enough for 1 protein
Linefile.read(CopyProtein, 20); // read 1 protein from infile
Splittedfile.write(CopyProtein, 20);// write to outfile
startPlaceOfRead++;
}
delete[] CopyProtein; // release dynamically-allocated memory
}
void buildClustrs()
{
Form Form;
char X[] = "XXXXXXXXXXXXXXXXXXXX‎‎‎‎««««««««";
int removed = 0;
for (int first = 0; first <= NumOfProteins; first++)//for the 1st
{
Splittedfile.seekg(0);
Splittedfile.seekg(first * 20 + 2 * first, ios_base::beg);
//int ThisPlace = Splittedfile.tellg();
Splittedfile.read(protein, 20);
if (strcmp(X, protein) == 0) continue; // 0= EQUAL
clustring << "\n\n\n";
clustring.write(protein, 20);
cout << "protein number " << first << " written as a lonely cluster " << endl; // WHEN FOUND belonging only-printing!
//remove this protein
Splittedfile.seekg(-20, Splittedfile.cur);
Splittedfile << "XXXXXXXXXXXXXXXXXXXX";
removed++;
for (int Allother = first + 1; Allother <= NumOfProteins; Allother++) //the following protein
{
Splittedfile.seekg(Allother * 20 + 2 * Allother);
Splittedfile.read(NextProtein, 20); // READ next protein, -read -go on automaticly-
if (strcmp(X, NextProtein) == 0) continue;
if ( (Form.Compare2Proteins (protein, NextProtein) ) !=-1)//>=60%
{
clustring << "\n";
clustring.write(NextProtein, 20);// write to clustring second protein in cluster
cout << "protein number " << Allother << " written to cluster " << first << endl; // WHEN FOUND belonging only-printing!
//remove this protein
Splittedfile.seekg(-20, Splittedfile.cur);//to next protein
Splittedfile << "XXXXXXXXXXXXXXXXXXXX";
removed++;
}
}
}
}
Header file:
#pragma once
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <tchar.h>
#include <string.h>
#include "Main.h"
#include "Form.h"
using namespace std;
class Cluster
{
public:
Cluster();
~Cluster();
void spllitFile();
void buildClustrs();
};
Screenshot with the errors:
I add more headers files, it may assist, beacuse I see no one know where the problem exactly is. thank you.
form.h:
#pragma once
#include <string>
#include <tchar.h>
#include <string.h>
using namespace std;
char *formString[6] = { "111100", "111010", "101110", "110110", "111001", "110101" };
class Form
{
public:
Form();
~Form();
//void spllitBySizeOfForm(int sizeOfForm, char protein[20], int i);
int Compare2Proteins(char *protein1, char *Nextprotein);
char* stringXform(char str[6], char form[6]);
};
HashEntry.h:
#pragma once
#include <string>
#include <tchar.h>
#include <string.h>
using namespace std;
class HashEntry
{
public:
HashEntry(int key, int value);
int getValue();
int getKey();
};
HashMap.h:
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <tchar.h>
#include <string.h>
#include "HashEntry.h"
using namespace std;
const int TABLE_SIZE = 20;
class HashMap
{
public:
HashMap::HashMap();//
HashMap::~HashMap();
int get(int key); // get value. for example: B--> get 0.
void put(int key, int value);//build entry AND put value in it.
void PutHmap(); // Put values to HashMap. get it by Hmap.get
};
main.h:
#pragma once
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <tchar.h>
#include <string.h>
#include "Cluster.h"
using namespace std;
long NumOfProteins = 0;
char* protein = new char[20]; // allocate memory for file content
char* NextProtein = new char[20]; // allocate memory for file content
fstream Splittedfile;
fstream newClusering;
ofstream clustring;
ifstream Linefile;
class Main
{
public:
//void position(char form[6], char str[6])
};

character array and printf makes program slow

(Um,My english is not well) :)
My friend is learning C++ now,and he find a problem that I can't explain why.
The First Code ,it runs more than 2000MS
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int ans[2000000];
char a[2000000];
int main()
{
scanf("%s\n",a);
int l=1,r=strlen(a);
for (int i=0;i<strlen(a);i++)
if (a[i]=='l')
ans[r--] = i+1;
else
ans[l++] = i+1;
for (int i=1;i<=strlen(a);i++)
printf("%d\n",ans[i]);
return 0;
}
The Second Code,it runs 465MS
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int ans[2000000];
char a[2000000];
int size;
int main()
{
scanf("%s",a);
int l=1,r=strlen(a);
size = r;
for (int i=0;i<strlen(a);i++)
if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
for (int i=1;i<=size;i++)
printf("%d\n",ans[i]);
return 0;
}
The Third Code,it runs more than 2000MS
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int ans[2000000];
char a[2000000];
int size;
int main()
{
scanf("%s",a);
int l=1,r=strlen(a);
size = r;
for (int i=0;i<size;i++)
if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
for (int i=1;i<=strlen(a);i++)
printf("%d\n",ans[i]);
return 0;
}
The Last Code ,it runs 515MS
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int ans[2000000];
string a;
int main()
{
cin >>a;
int l=1,r=a.size();
for (int i=0;i<a.size();i++)
if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
for (int i=1;i<=a.size();i++)
printf("%d\n",ans[i]);
return 0;
}
When input string size is 10^5
So , the reason is about second for loop.
And my question is why "strlen" function and print in for loop will make the code so slow?
By writing strlen in the loop condition — a loop with two million iterations — you are scanning through a two-megabyte string two million times. Of course that's going to take some time!
That delay goes away when you pre-calculate the string length just once.
Also, you will probably find that streaming 2MB of text to the console is going to be noticeably "slow" in any case.
The strlen function has to be executed each time through the loop when you have it in the loop. That's an extra function call, and it has to run through the full string to find it's length.
Caching the length in size, means that none of that has to be redone every time through the loop.
So, there is no error. This is expected behavior.

How to compare char variables (c-strings)?

#include <iostream>
using namespace std;
int main() {
char word[10]="php";
char word1[10]="php";
if(word==word1){
cout<<"word = word1"<<endl;
}
return 0;
}
I don't know how to compare two char strings to check they are equal. My current code is not working.
Use strcmp.
#include <cstring>
// ...
if(std::strcmp(word, wordl) == 0) {
// ...
}
Use std::string objects instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word="php";
string word1="php";
if(word==word1){
cout<<"word = word1"<<endl;
}
return 0;
}
To justify c++ tag you'd probably want to declare word and word1 as std::string. To compare them as is you need
if(!strcmp(word,word1)) {
word and word1 in your submitted code are pointers. So when you code:
word==word1
you are comparing two memory addresses (which isn't what you want), not the c-strings they point to.
#include <iostream>
**#include <string>** //You need this lib too
using namespace std;
int main()
{
char word[10]="php";
char word1[10]="php";
**if(strcmp(word,word1)==0)** *//if you want to validate if they are the same string*
cout<<"word = word1"<<endl;
*//or*
**if(strcmp(word,word1)!=0)** *//if you want to validate if they're different*
cout<<"word != word1"<<endl;
return 0;``
}