This question already has answers here:
C++. Dividing 1 by any number gives 0
(3 answers)
Closed 1 year ago.
I need to write a program to run this pattern in c++:
S=1/2+2/3+3/4+4/5+...+N-1/N
I have tried but my code is showing 0.
And its the code that I have written:
#include <iostream>
using namespace std;
int main()
{
unsigned int N;
float S=0;
cout << "Enter N:";
cin >> N;
for (int I = 2; I <= N; I++)
{
S = S + (I - 1) / I;
}
cout << S;
return 0;
}
I have to write it with for-loop, while and do-while
(I - 1) / I only contains integers, therefore any remainder is discarded.
You can avoid this by simply subtracting - 1.f off of I instead.
Related
This question already has answers here:
Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?
(11 answers)
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 2 days ago.
I'm new to C++ and this program that I wrote does not give the output, which is a binary form of an integer input.
It does give the result in Python Tutor. But in VSCode the result is always one less than the actual binary output.
Example-
5 = 100
6 = 109
17 = 10000
#include <iostream>
#include <cmath>
int main(){
int n;
std::cout << "Enter n:- ";
std::cin >> n;
int ans = 0;
int i = 0;
while (n != 0){
int bit = n & 1;
ans = (bit * pow(10, i)) + ans;
n = n >> 1;
i++;
}
std::cout << ans;
return 0;
}
What did I do wrong?
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 3 months ago.
i have been trying to make program that prints random string out of string array. But it is throwing nothing.
I have chaged random number generator algorythm, but it printed same string every time. So i made test aplication and it doesnt work anymore.
Here is my code:
#include <iostream>
using namespace std;
int randomNumber(int min, int max) {
int x = min + rand() % max;
return x;
}
int main() {
string lmao[] = {"xd", "hahaha",",","fjdskl", "fjdskl", "fjkdsljfkdsl","uuruur","fjdksl"};
string lastZ = "";
for (int i = 0; i <= 10; i++) {
int x = sizeof(lmao) / sizeof(int);
int y = randomNumber(0, x);
string z = lmao[y];
if (z == lastZ) {
cout << "Fail";
}
else {
lastZ = z;
cout << "Succes";
}
}
return 0;
}
Don't use rand, use the <random> header.
If you must use rand, seed it with srand before hand.
This question already has answers here:
How to check whether a system is big endian or little endian?
(18 answers)
How do I convert between big-endian and little-endian values in C++?
(35 answers)
Closed 5 months ago.
My teacher told me to:
Write the first M negative odd numbers in 16bit and little endian form
Write the first N positive even numbers in 32bit and big endian form
And this is my code thus far. I was able to do everything except for that endianess part
#include <iostream>
#include <fstream>
using namespace std;
int main() {
//writing to file section
int m, n;
ofstream bin("D://Ex02.bin", ios::binary | ios::out);
cin >> m >> n;
//Write the first M negative odd numbers in 16bit (2bytes)
for (int i = 0; i <= m; i++) {
int I = -2 * i - 1;
bin.write((char*)&I, 2);
}
//Write the first N positive even numbers in 32bit (4bytes)
for (int i = 0; i <= n; i++) {
int I = 2 * i;
bin.write((char*)&I, 4);
}
bin.close();
//test section
ifstream test("D://Ex02.bin", ios::binary | ios::in);
//Read 16 bit numbers
int x = -pow(2,16) + 1;
for (int i = 0; i <= m; i++) {
test.read((char*)&x, 2);
cout << x << endl;
}
//Read 32 bit numbers
x = 0;
for (int i = 0; i <= n; i++) {
test.read((char*)&x, 4);
cout << x << endl;
}
test.close();
return 0;
}
The problem is that I have no idea how to write integer in specific endian form.
Can someone help me pls
This question already has answers here:
Casting int to bool in C/C++
(3 answers)
Closed 2 years ago.
I was wondering if you could clarify me the difference between the conditions while(k) and while(k > 0) when do they differ?
#include <iostream>
int main() {
int k;
std::cin >> k;
while(k) {
std::cout << "Hello" << "\n";
k--;
}
}
#include <iostream>
int main() {
int k;
std::cin >> k;
while(k > 0) {
std::cout << "Hello" << "\n";
k--;
}
}
The two code snippets you present will give equivalent results if – and only if – the input value for k is not negative. Try it.
Specifically, for the first snippet, if a value of -1 is input for k, the --k; line inside the while loop will (maybe) never reduce k to zero, or (maybe) just take a long time (until k reaches INT_MIN – something like -2147483648), depending on how the platform you are using handles signed integer underflow.
The while (k) loop will run until k is zero.
However, in the second snippet, a negative input for k will mean that the loop never runs (k will not be greater than zero on the first test).
This question already has answers here:
Sorting std::strings with numbers in them?
(4 answers)
Closed 6 years ago.
I have problem solving this problem.
The task is simple at first line I enter how many examples I have.
On second line I need to enter how many numbers im going to read.
and then we enter all the numbers separate by space.
The task itselfs do , sorting the string array wtih numbers from smalles to the biggest one. After that if we have even numbers entered we print the middle number -1, if they are uneven we just print the middle number.
So far if I use the exact same code with long long it works perfectly , but it is limited only to 19 digit number and I want to expand the program so it can use bigger numbers.
Using that way the sort func , when I try to sort 16 elements from 160 to 10 , they all messed it start from 110 then in the midle is 160 and so one , which makes absolutly non sense, using 5 numbers or 8 works perfectly w/o any problem , using more numbers fails.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int examples;
cin >> examples;
for (size_t i = 0; i < examples; i++)
{
long long unsigned int n;
cin >> n;
string * numbers = new string[n];
for (size_t i = 0; i < n; i++)
{
cin >> numbers[i];
}
sort(numbers, numbers + n);
if (n % 2 == 0) {
cout << numbers[n / 2 - 1];
}
else
cout << numbers[n / 2];
}
system("pause");
return 0;
}
First, if you allocate memory with operator new, you must release it with operator delete[].
Second, when you sort strings instead of values, they are sorted just like strings would do, and here is where your problem lies. You see, 100 is alphabetically less than 2 or 20, that's why it would appear earlier.
Here's the output your program gives. Check this rule out, and you'll see that i'm right.
10 100 110 120 130 140 150 160 20 30 40 50 60 70 80 90
Third, using operator new is discouraged for pretty much anything. You have STL, and you seem to be using it extensively - why not vector?
Fourth, you don't check if anything we write into numbers[i] is actually a number. Think on that.
Fifth, for N being long enough(more than 2^sizeof(size_t)) your problem will NEVER stop due to integer overflow.
Sixth, you don't check for n == 0, and you will ultimately get memory access violation if you enter it.
A fast-right-off-the-bat fix for your problem:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main() {
int examples;
cin >> examples;
for (size_t i = 0; i < examples; i++)
{
size_t n;
cin >> n;
if (n <= 0)
break;
vector<string> numbers(n);
for (size_t i = 0; i < n; i++)
cin >> numbers[i];
//here we add a predicate for string checking,
//which evaluates the length of string
//before using the usual operator<.
sort(begin(numbers), end(numbers), [](const string& s1, const string& s2){
if (s1.length() < s2.length())
return true;
if (s2.length() < s1.length())
return false;
else
return (s1 < s2);
});
if (n % 2 == 0) {
cout << numbers[n / 2 - 1];
}
else
cout << numbers[n / 2];
}
system("pause");
return 0;
}
Still, it has a number of problems:
Checking if numbers[i] is actually a number
I'm not sure that
predicate I wrote doesn't have bugs - I'm just trying to give you
the idea of how it should work.