creating an upwards series in c++ - c++

Screenshot of my code
Hey, I have just started learning C++ and I am trying to get it to sum the series:
K+N−1∑n=K [-1^(n)/(n+1)2]
I have managed to get it to tell me the nth term previously, but now I would like to for each term in the series, starting with the kth and going in sequence to the last (k+n-1st), add this term to the running sum.
I need to use a function direct_up() which uses the function term(). I defined initially and test it in the main.
I know I am missing something and am a bit confused about how to use the functions and that I may have made a few mistakes. So I would be very grateful for some advice or help. I have attached a picture of what I have done so far, as well as typed it below.
using namespace std;
double term(int n) {
double y;
if(n%2==1) {
y = -1.0/((n+1.0)*(n+1.0));
} else {
y = 1.0/((n+1.0)*(n+1.0));
}
return y;
}
double direct_up(int k, int n) {
int startingnumber = k;
for (int i = startingnumber; i <= k+n; i++) {
cout << n << term(n) << endl;
}
return n;
}
int main() {
double n;
int k;
cout.precision(16);
cout << "enter number of terms";
cin >> n;
cout << "enter a value for k";
cin >> k;
cout << "here is the value" << direct_up(k,n);
return 0;
}

This is what you want to do:
double direct_up(int k, int n)
{
int startingnumber = k;
double sum = 0;
for (int i = startingnumber; i <= k+n; i++) {
sum += term(i);
}
return sum;
}
Here's how to do it without keeping your own running sum as you asked in your comment:
#include <vector>
#include <numeric>
double direct_up(int k, int n)
{
int startingnumber = k;
std::vector<double> terms;
for (int i = startingnumber; i <= k+n; i++) {
terms.push_back(term(i));
}
return accumulate(terms.begin(), terms.end(), 0.0);
}

Related

Trying to implement Willans' formula for the n-th prime, what's the problem with my code?

The formula is listed in the following article: https://en.wikipedia.org/wiki/Formula_for_primes. I am trying to implement it but to no success, for whatever reason the code is producing number which seem to be nth power of two + 1, which is obviously not what I want to achieve.
#include <iostream>
#include <cmath>
using namespace std;
int nth_prime(int n) {
double s = 1;
for (int i = 1; i <= pow(2, n); i++) {
double c = 0;
for (int j = 1; j <= i; j++) {
double f = (tgamma(j)+1)/j;
c+=floor(pow(cos(M_PI*f), 2));
}
s+=floor(pow(n/c, 1/n));
}
return s;
}
int main() {
int n;
while (cin >> n) {
cout << nth_prime(n) << endl;
}
return 0;
}

Pick a number say 3 so you can enter 3 numbers then substract the maximum from the min

I tried changing a lot of integers and stuff but it didn't work and it gives me a random number like 53289432 even tho lets say i put in 3:5,1,2 it should output 3 since 5-2 is 3.
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int x[n];
int mn;
int mx;
for(int i=1;i<n;i++)
{
cin>>x[i];
for(int j=1;j<n;j++)
{
if(x[i]>x[j]);
}
{
x[i]=mn;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(x[i]<x[j]);
}
{
x[i]=mx;
}
}
cout<<mx-mn;
}
You don't need an array:
int smallest = 0;
int largest = 0;
std::cout << "Enter quantity: ";
int quantity;
std::cin >> quantity;
if (quantity < 1)
{
std::cerr << "Invalid quantity.\n";
return 1;
}
std::cout << "Enter number: ";
std::cin >> smallest;
largest = smallest;
for (int i = 1; i < quantity; ++i)
{
std::cout << "Enter number: ";
int number;
std::cin >> number;
if (number < smallest) smallest = number;
if (number > largest) largest = number;
}
std::cout << "maximum from minimum: " << (smallest - largest) << "\n";
std::cout << "minimum from maximum: " << (largest - smallest) << "\n";
The above code uses a running minimum/maximum, so no arrays are needed.
No need for variable length arrays or having to figure out the array capacity at compile time.
Your code carries several holes,
Firstly,
for(int i=1;i<n;i++)
{
cin>>x[i];
This won't take n integers, it will only take (n-1) integers as input. You need to initialise i=0, for n integers;
Secondly,
for(int i=1;i<n;i++)
{
cin>>x[i];
for(int j=1;j<n;j++)
{
if(x[i]>x[j]); //This will compare with garbage when i=1 and j>1
}
{
x[i]=mn;
}
}
Your comparison, will only be valid for first iteration of i=1 and j=1, after j>1, it will pick garbage value, since, you haven't taken any input yet.
It is suggested, to first take all the inputs, then do the comparison, or other operations.
Here is my solution
I think this is what you are trying to do!
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int n;
cin>>n;
int x[n];
int mn;
int mx;
//First take the inputs in array x
for(int i=0;i<n;i++)
{
cin>>x[i];
}
//Find maximum and store it in mx
mx = INT_MIN; //This stores minimum in mx variable (climits)
for(int j=0;j<n;j++)
{
if(mx<x[j])
mx=x[j];
}
//Find minimum and store it in mn
mn = INT_MAX; //This stores maximum in mn variable (climits)
for(int j=0;j<n;j++)
{
if(mn>x[j])
mn=x[j];
}
int ans = mx - mn;
cout<<ans<<endl;
}
There is a better solution where you don't use extra space(array), and by using only 2 variables, you can find the difference. But I would recommend you to first understand this concept, and take a look, how array works, before moving towards any optimised solution.

why is the answer not showing up?

I am doing a code in c++ where I am supposed to be finding the series and I build the function for the series myself yet and I call the function I don't find my answer
here is my code
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
harmonicSeries(n);
}
double harmonicSeries(int n) {
for (int i = 1; i <= n; i++) {
float s;
float sum = 0.0;
s = 1 / n;
sum += s;
return sum;
}
}
I will be thankful for any help
See I have made the changes in your code,this works fine in this finding numbers and adding to get their sum.You should use return outside the function and basically harmonic series is of form 1/n which can be any float number or double number so I use s as double and i has float(which by this).
s=1/i(double=1/float,gets converted to double)
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
cout<<harmonicSeries(n);
}
double harmonicSeries(int n) {
double sum=0.00;
double s;
for (float i = 1; i <= n; i++) {
s = 1 / i;
sum += s;
}
return sum;
}
If you find anything wrong do ask for sure:)

Stack Overflow for Fractional Knapsack

I am fairly new to C++. I have implemented the fractional knapsack problem in c++ for the course "Algorithmic Toolbox" on Coursera:
#include <iostream>
#include <iomanip>
using namespace std;
int get_max_index(double A[], double B[],int l)
{
/*
int A = array of value
int B = array of weights
int l = length of the array
*/
int p,Max{0};
for(int j=0;j<l;j++)
{
if((A[j]/B[j]) > Max){Max = A[j]/B[j];p = j;}
}
return p;
}
int main()
{
int n,W,q,Max{0},W1{0};
cin >> n >> W;
double values[n],weights[n],loot{0};
for(int i=0;i<n;i++)
{
cin >> values[i] >> weights[i];
}
for(int j=0;j<n;j++)
{
if(W==0){break;}
else
{
q = get_max_index(values,weights,n);
if(weights[q] <= W){W1 = weights[q];}
else{W1 = W;}
loot += W1 * (values[q]/weights[q]);
W -= W1;
weights[q] -= W1;
if(weights[q] == 0){values[q] = 0;}
}
}
cout << setprecision(4) << fixed;
cout << loot << endl;
}
After submitting this code I got a stack overflow error (unknown signal 11). Please help me understand why this happens and solution to this problem.
EDIT :
I have changed the code. I am not using the get_max_index function with dynamically sized arrays. Here is the new code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long long int n,W,q,p,Max{0},W1{0};
cin >> n >> W;
long double values[n],weights[n],loot{0},VPU[n];
for(long long int i=0;i<n;i++)
{
cin >> values[i] >> weights[i];
VPU[i] = values[i] / weights[i];
}
for(long long int j=0;j<n;j++)
{
if(W==0){break;}
else
{
for(long long int k=0;k<n;k++)
{
if(VPU[k] > Max){Max = VPU[k];p=k;}
}
Max = 0;
q = p;
if(weights[q] <= W){W1 = weights[q];}
else{W1 = W;}
loot += W1 * (values[q]/weights[q]);
W -= W1;
weights[q] -= W1;
if(weights[q] == 0){VPU[q] = 0;}
}
}
cout << setprecision(4) << fixed;
cout << loot << endl;
}
C++ standard doesn't allow variable-length arrays. So that it is not legal(even though some compilers might support it) to create a static array(allocated in stack) double values[n],weights[n]... with a size that is not known in compile time. The stack overflow error is most probably because of that(n is not known at compile time and a junk value that breaks your stack might be read). Instead try allocating them in heap with new double[n] syntax. Don't forget to free the array at the end.

Using Lambda Expression To Code An Exponent In C++

How can I write an "X to the power of k" procedure in C++? (k is a positive integer)
I did the same thing in python, and it was a breeze, but in C++, I don't even know where to begin.
How can I write an "X to the power of k" procedure in C++? (k is a positive integer)
Write a short loop in a function like
int pow(int X, int k) {
int result = 1;
for(int i = 0; i < k; ++i) result *= X;
return result;
}
It's easy to express this in a lambda as well:
auto pow = [] (int X, int k) {
int result = 1;
for(int i = 0; i < k; ++i) result *= X;
return result;
};
cout << pow(5,3);
See a working sample please.
Ummm, maby try this:
#include <iostream>
#include<cmath> //adds math functions: power, square root etc.
using namespace std;
int main(){
int x;
int k;
cin >> x;
cin >> k;
x = pow(x, k);
cout << "\nX to the power of k: " << x << endl << endl;
return 0;
}