Anshul
oh i got it now. If i have an array of numbers then extracting 3 top elements will take O(nlogn) time but in case of having a heap ready with me will take just o(3logn) time although the building of heap takes o (nlogn) time still its better than the array as in case if i get some more elements say m more elements,then i'll need to sort the array again and again while for heap i can insert the elements in o(mlog(n+m)) time and then extraction is easier. thus the building part takes time but its one time job and we're ahead of arrays. So its' gonna be almost the same complexity but still less number of operations.
Anshul
Why is exception handling provided by c++.
If there is some exception then we have to throw it ourselves using throw and then handle it ourselves, so what c++ is doing in it
Why don't just use if else block for it
神 ꜰʟᴀꜰꜰʏ
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, m, j, f;
cout << "enter x: " << endl;
cin >> x;
cout << "enter m: " << endl;
cin >> m;
if (m > -1 && m < x) {
j = sin(5 * f * (x)+3 * m * fabs(f * (x)));
cout << j << endl;
}
else {
if (m > x) {
j = cos(3 * f(x) + 5 * m * fabs(f * (x)));
cout << j << endl;
}
else {
j = pow(f * (x)+m, 2);
cout << j << endl;
}
}
}
Mr
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;
fp = fopen("program.txt","r");
if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 6; ++i){
if(ch == operators[i])
printf("%c is operator\n", ch);
}
if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is indentifier\n", buffer);
}
}
fclose(fp);
return 0;
}
Alex
Hi. do you know c++ string class for utf8? like QString str;
str[5] - got 5th codepoint, not byte. QString is not the option cause QChar is 16 bit width only. Also I don`t need recalculcation for every operator[] call. just get offset from uint32 array
A
#include <iostream>
#include <string>
#include <vector>
std::string DNAStrand(std::string dna)
{
int trA = dna.find("A");
int trT = dna.find("T");
int trC = dna.find("C");
int trG = dna.find("G");
bool tf = true;
while(tf) {
if(dna.find("A") != std::string::npos) {
dna.replace(trA, 1, "T");
dna.find("A");
} else if(dna.find("T") != std::string::npos) {
dna.replace(trT, 1, "A");
dna.find("T");
} else if(dna.find("C") != std::string::npos) {
dna.replace(trC, 1, "G");
dna.find("C");
} else if(dna.find("G") != std::string::npos) {
dna.replace(trG, 1, "C");
dna.find("G");
} else {
tf = false;
}
}
return dna;
}
int main() {
std::string word = "AAAA";
DNAStrand(word);
std::cout << word;
}