I have some simple code in C++ (see below), that works good and can be successfully debugged in Microsoft Visual Studio.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <stdlib.h>
#include <iomanip>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
Rational() {
numerator = 0;
denominator = 1;
};
Rational(int p, int q) {
int divider = find_common_divider(abs(p), abs(q));
p = p / divider;
q = q / divider;
if ((p < 0) && (q < 0)) {
p = abs(p);
q = abs(q);
}
else if ((p > 0) && (q < 0)) {
p = -p;
q = abs(q);
}
if (p == 0)
q = 1;
numerator = p;
denominator = q;
};
int Numerator() const {
return numerator;
};
int Denominator() const {
return denominator;
};
int find_common_divider(int N1, int N2) {
while ((N1 > 0) && (N2 > 0)) {
if (N1 > N2)
N1 %= N2;
else
N2 %= N1;
}
return N1 + N2;
};
};
int main() {
{
const Rational r(3, 10);
if (r.Numerator() != 3 || r.Denominator() != 10) {
cout << "Rational(3, 10) != 3/10" << endl;
return 1;
}
}
{
const Rational r(8, 12);
if (r.Numerator() != 2 || r.Denominator() != 3) {
cout << "Rational(8, 12) != 2/3" << endl;
return 2;
}
}
{
const Rational r(-4, 6);
if (r.Numerator() != -2 || r.Denominator() != 3) {
cout << "Rational(-4, 6) != -2/3" << endl;
return 3;
}
}
{
const Rational r(4, -6);
if (r.Numerator() != -2 || r.Denominator() != 3) {
cout << "Rational(4, -6) != -2/3" << endl;
return 3;
}
}
{
const Rational r(0, 15);
if (r.Numerator() != 0 || r.Denominator() != 1) {
cout << "Rational(0, 15) != 0/1" << endl;
return 4;
}
}
{
const Rational defaultConstructed;
if (defaultConstructed.Numerator() != 0 || defaultConstructed.Denominator() != 1) {
cout << "Rational() != 0/1" << endl;
return 5;
}
}
cout << "OK" << endl;
return 0;
}
But when I start debugging in VSCode (UBUNTU 18.04) from line
const Rational r(3, 10) in main() with "Step into" up to line
int divider = find_common_divider(abs(p), abs(q));
debugger gives exception "Unable to open 'abs.c': Unable to read file '/build/glibc-OTsEL5/glibc-2.27/stdlib/abs.c' (Error: Unable to resolve non-existing file '/build/glibc-OTsEL5/glibc-2.27/stdlib/abs.c')."
That exception is absent in Microsoft Visual Studio and debugger goes to function "find_common_divider".
What is wrong with my VSCode ?
There is nothing wrong with your visual studio code, you simply don't have the source code for abs so can't step into it. Ignore the error, hit step out to get back to your code.
Related
I wrote this to find out which 10-digit numbers are prime but it stops after showing about 20 numbers at all. Would you help me figure out what's wrong? Or suggesting any other sources? It's important to me as it's my school project.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void temp()
{
static long long n1=9999999999 ;
long long n2,n3;
if (n1 <= 1 || n1%2==0|| n1%3==0|| n1%5==0|| n1%7==0|| n1%11==0|| n1%13==0|| n1%17==0|| n1%19==0|| n1%23==0|| n1%29==0|| n1%31==0|| n1%37==0|| n1%41==0|| n1%43==0|| n1%47==0|| n1%53==0|| n1%59==0|| n1%61==0|| n1%67==0|| n1%71==0|| n1%73==0|| n1%79==0|| n1%83==0|| n1%89==0|| n1%97==0)
{
std::cout<< n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2 = (n1 - 1)/2;
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout<< n1 << " not prime\n\n";
}
else
{
n2--;
}
}
std::cout<< n1 << " prime\n\n";
n1--;
temp();
}
}
int main(int argc, char** argv)
{
temp();
return 0;
}
When n1 is 9999999983 it gets stuck inside the while loop until n2 is less or equal to 1
You should look for a more efficient way
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout << n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2--;
}
}
EDIT:
This is what you could do in your case (Found it here and changed it to fit your needs (I found it here and changed it up a bit to fit your needs)
#include <iostream>
using namespace std;
void check() {
int n = 9999999999;
while (n != 0) {
int i, m = 0, flag = 0;
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
cout << "Number is not Prime." << endl;
flag = 1;
break;
}
}
if (flag == 0)
cout << "Number is Prime." << endl;
n--;
}
}
int main()
{
check();
return 0;
}
Im building an arithmetic calculator, I have error checking for everything, but multiple .'s as an example 12.34 is allowed, but 12.3.4 shouldn't be because of the multiple .'s how would I implement that into my code? I dont know how to stop my "if" statement that looks for .'s because it skips over multiple one when all I want it to do is let any numerical number that is a legal decimal through such as 12.34
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <errno.h>
using namespace std;
const double MAXRANGE = pow(2.0, 16.0); // 65536
const double MINRANGE = -pow(2.0, 16.0);
bool validDouble(const char* argument)
{
return atof(argument);
}
int main(int argc, char* argv[])
{
char z;
bool isARealDouble = true;
if (argc == 3)
{
z = 0;
}
else if (argc == 4)
{
z = argv[3][0];
}
else if (argc < 3)
{
cout << "P" << endl;
return 0;
}
else if (argc > 4)
{
cout << "P" << endl;
return 0;
}
if ((z == 'a') || (z == 's') || (z == 'm') || (z == 'd') || (z == 'p') || (z == 0))
{
if (z == 0)
{
float h;
float x = atof(argv[1]);
float y = atof(argv[2]);
if (x <= MINRANGE)
{
cout << "R" << endl;
return 0;
}
else if (x >= MAXRANGE)
{
cout << "R" << endl;
return 0;
}
if (y <= MINRANGE)
{
cout << "R" << endl;
return 0;
}
else if (y >= MAXRANGE)
{
cout << "R" << endl;
return 0;
}
for (int i = 1; i < argc; i++)
{
isARealDouble = true;
for (int j = 0; j < strlen(argv[i]); j++)
{
cout << argv[i][j] << " ";
if (isdigit(argv[i][j]) || argv[i][j] == '.')
{
isARealDouble = true;
}
else
{
isARealDouble = false;
cout << "X" << endl;
return 0;
}
}
}
if (isARealDouble)
{
h = x + y;
cout << h << endl;
return 0;
}
}
}
return 0;
}
Convert the C string to a std::string and use std::count:
#include <algorithm>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
auto args = std::vector<std::string>{argv, argv + argc};
for (std::size_t i = 1; i < args.size(); ++i) {
const auto count = std::count(args[i].begin(), args[i].end(), '.');
if (count > 1) {
return EXIT_FAILURE;
}
}
}
Keep in mind I've only been using CPP for around a week so the code is not very good. I wrote this to solve the problem 1362A on CodeForces. Long story short, the code works fine except for the fact that it says the memory used is 262100 KB. I've looked over it multiple times but can't find anything that would cause lots of memory to be used.
#include <iostream>
#include <cstdint>
using namespace std;
uint64_t makeq(uint64_t x, uint64_t y)
{
uint64_t q;
if (x >= y)
{
q = x / y;
}
else
{
q = y / x;
}
return q;
}
bool check(uint64_t x, uint64_t y, uint64_t q)
{
if (x >= y)
{
if (x % y != 0)
{
cout << -1 << "\n";
return false;
}
}
else
{
if (y % x != 0)
{
cout << -1 << "\n";
return false;
}
}
//checking float
if (q > 8 && q % 8 != 0)
{
cout << -1 << "\n";
return false;
}
if (q == 1)
{
cout << 0 << "\n";
return false;
}
if (q % 2 != 0)
{
cout << -1 << "\n";
return false;
}
return true;
}
int main()
{
uint64_t n, x, y, q, c;
cin >> n;
while (n--)
{
c = 0;
cin >> x >> y;
q = makeq(x, y);
if (check(x, y, q) == false)
{
continue;
}
while (q > 1)
{
if (q >= 8 && q % 8 == 0)
{
q /= 8;
c += 1;
}
else
{
if (q >= 4 && q % 4 == 0)
{
q /= 4;
c += 1;
}
else
{
q /= 2;
c += 1;
}
}
}
cout << c << endl
<< flush;
}
return 0;
}
**
So far I have this code. I'm trying to print prime factorization with exponents. For example, if my input is 20, the output should be 2^2, 5
#include <iostream>
#include <cmath>
using namespace std;
void get_divisors (int n);
bool prime( int n);
int main(int argc, char** argv) {
int n = 0 ;
cout << "Enter a number and press Enter: ";
cin >>n;
cout << " Number n is " << n << endl;
get_divisors(n);
cout << endl;
return 0;
}
void get_divisors(int n){
double sqrt_of_n = sqrt(n);
for (int i =2; i <= sqrt_of_n; ++i){
if (prime (i)){
if (n % i == 0){
cout << i << ", ";
get_divisors(n / i);
return;
}
}
}
cout << n;
}
bool prime (int n){
double sqrt_of_n = sqrt (n);
for (int i = 2; i <= sqrt_of_n; ++i){
if ( n % i == 0) return 0;
}
return 1;
}
I hope someone can help me with this.
You can use an std::unordered_map<int, int> to store two numbers (x and n for x^n). Basically, factorize the number normally by looping through prime numbers smaller than the number itself, dividing the number by the each prime as many times as possible, and recording each prime you divide by. Each time you divide by a prime number p, increment the counter at map[p].
I've put together a sample implementation, from some old code I had. It asks for a number and factorizes it, displaying everything in x^n.
#include <iostream>
#include <unordered_map>
#include <cmath>
bool isPrime(const int& x) {
if (x < 3 || x % 2 == 0) {
return x == 2;
} else {
for (int i = 3; i < (int) (std::pow(x, 0.5) + 2); i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
std::unordered_map<int, int> prime_factorize(const int &x) {
int currentX = abs(x);
if (isPrime(currentX) || currentX < 4) {
return {{currentX, 1}};
}
std::unordered_map<int, int> primeFactors = {};
while (currentX % 2 == 0) {
if (primeFactors.find(2) != primeFactors.end()) {
primeFactors[2]++;
} else {
primeFactors[2] = 1;
}
currentX /= 2;
}
for (int i = 3; i <= currentX; i += 2) {
if (isPrime(i)) {
while (currentX % i == 0) {
if (primeFactors.find(i) != primeFactors.end()) {
primeFactors[i]++;
} else {
primeFactors[i] = 1;
}
currentX /= i;
}
}
}
return primeFactors;
}
int main() {
int x;
std::cout << "Enter a number: ";
std::cin >> x;
auto factors = prime_factorize(x);
std::cout << x << " = ";
for (auto p : factors) {
std::cout << "(" << p.first << " ^ " << p.second << ")";
}
}
Sample output:
Enter a number: 1238
1238 = (619 ^ 1)(2 ^ 1)
To begin with, avoid using namespace std at the top of your program. Second, don't use function declarations when you can put your definitions before the use of those functions (but this may be a matter of preference).
When finding primes, I'd divide the number by 2, then by 3, and so on. I can also try with 4, but I'll never be able to divide by 4 if 2 was a divisor, so non primes are automatically skipped.
This is a possible solution:
#include <iostream>
int main(void)
{
int n = 3 * 5 * 5 * 262417;
bool first = true;
int i = 2;
int count = 0;
while (i > 1) {
if (n % i == 0) {
n /= i;
++count;
}
else {
if (count > 0) {
if (!first)
std::cout << ", ";
std::cout << i;
if (count > 1)
std::cout << "^" << count;
first = false;
count = 0;
}
i++;
if (i * i > n)
i = n;
}
}
std::cout << "\n";
return 0;
}
Note the i * i > n which is an alternative to the sqrt() you are using.
Sorry this is my first time use stackoverflow.
I dont kow where is the mistake in my code.
Output that i want:
-1+3-5+7-9+11-13+15
RESULT : 8
But Output that is shown
-1+3-5+7-9+11-13+15
RESULT : 10
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i, S, x, sign;
S = 0;
for (i = 1; i <= 8; i++) {
if ((pow(-1, i - 1) == 1) && (i > 1)) {
sign = -1;
}
if ((pow(-1, i - 1) != 1) && (i > 1)) {
sign = 1;
cout << "+";
}
if (i == 1) {
sign = 1;
cout << "-";
}
x = sign * (2 * i - 1);
cout << x;
S = S + x;
}
cout << "\n Result:" << S;
}
problem is in the if condition block where you check i==1
in that loop you are making sign=1 that should be sign=-1
How about improving the logic like following?
#include <iostream>
using namespace std;
int main()
{
int i;
bool sign = true; // signed/minus = true, non-signed/plus = false
int ans = 0;
for( i=1; i<=15; i=i+2){
if( sign == true){
cout << "-" << i;
ans = ans - i;
}
else {
cout << "+" << i;
ans = ans + i;
}
sign = !sign;
}
cout << endl << "RESULT : " << ans << endl;
return 0;
}
Try this code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i, S, x, sign;
S = 0;
for (i = 1; i <= 8; i++) {
if ((pow(-1, i - 1) == 1) && (i > 1)) {
sign = -1;
}
else
if ((pow(-1, i - 1) != 1) && (i > 1)) {
sign = 1;
// cout << "+";
}
//else
if (i == 1) {
sign = -1;
//cout << "-";
}
x = sign * (2 * i - 1);
cout <<"\n"<<x;
S = S + x;
//cout<<"S is \n"<<S;
}
cout << "\n Result:" << S;
}
You have put wrong sign when i==1
The problem is that you're starting the calculation with a positive sign (but you're lying to yourself by printing "-").
You can simplify the code and don't need to mess around with pow if you make the obervation that
pow(-1, k) == -1 * pow(-1, k-1)
Starting at pow(-1,0) (that is, 1), you can write:
int main(int argc, char* argv[])
{
int sign = 1; // sign will always hold pow(-1, i).
int sum = 0;
for (int i = 1; i <= 8; i++)
{
sign *= -1;
if (sign > 0) // Since sign starts at -1, we know that i > 1 here
{
std::cout << "+";
}
int term = sign * (2 * i - 1);
std::cout << term;
sum += term;
}
std::cout << " = " << sum << std::endl;
}