I need to do this exercise, with struct and include the function in the struct to do the calculations, but for some reason, it's not working.
I recommended you to check the image for a better idea.
#include <iostream>
using namespace std;
struct Alfa{
double h,x,n,a;
void shuma(){
cout << "enter n: "; cin >> n;
cout << "enter a: "; cin >> a;
for (int i = 1; i >= n; i++){
x = 2 * i + a;
}
};
};
int main() {
Alfa alf;
alf.shuma();
alf.h = (alf.x / 2) + 3;
cout << alf.h;
return 0;
}
You are not following the formula in the image. Use this instead:
double sum = 0;
for (int i = 1; i <= n + 1; i++) {
if (i != 4) {
sum += 2 * i + a;
}
}
h = x / 2 + 3 * sum;
for (int i = 1; i >= n; i++) means i initiated to 1, while i>=n do the loop, then inc i. So when n is bigger then 1, it will never enter the loop. Maybe you want for (int i = 1; i <= n+1; i++) ?
Related
I tried to solve the problem but my code still contains some bugs. Why isn't it running?
Here is the link of the question website: https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/pair-sums/?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int n = 1e7 + 10;
int hsh[n];
int main()
{
int n, k;
cin >> n >> k;
int A[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
for (int i = 0; i < n; i++)
{
hsh[A[i]] = k - A[i];
}
int t = 0;
for (int i = 0; i < n; i++)
{
if (hsh[A[i]] == k - hsh[hsh[A[i]]])
{
cout << "YES";
t = 1;
break;
}
}
if (t == 0)
{
cout << "NO";
}
return 0;
}
The problem is that while hsh[A[i]] is always valid, hsh[hsh[A[i]] is not.
Consider the following input:
1 1
10000
This does the following:
A[0] = 10000;
...
hsh[10000] = 1 - 10000; // = -99999
...
if (hsh[10000] == 1 - hsh[-99999]) {...}
So your code is reading out of bounds of the array hsh[]. Make sure you check first if hsh[A[i]] >= 0.
Note that your code is more complicated than necessary; you can do a single loop over the input to check if there is a matching pair:
#include <iostream>
static constexpr int max_k = 2e6;
static bool seen[max_k + 1];
int main()
{
int n, k;
std::cin >> n >> k;
for (int i = 0; i < n; ++i)
{
int A;
std::cin >> A;
if (A <= k && seen[k - A]) {
std::cout << "YES\n";
return 0;
}
seen[A] = true;
}
std::cout << "NO\n";
}
The user must enter a number n (1; infinity). Then the program does this:
cos1/sin1 * (cos1+cos2)/(sin1+sin2) * … * (cos1+cos2+...+cos n)/(sin1+sin2+...+sin n )
I tried to calculate that:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
double res;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++)
{
res = cos(i) / sin(i);
}
cout << res;
}
But I don't know which algorithm would do it correctly.
Since you need to divide the two sums in each step, you need to store those sums, and multiply an accumulating product with the result of dividing them.
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
double cos_sum = 0.0;
double sin_sum = 0.0;
double res = 1.0;
for (int i = 1; i <= n; i++)
{
cos_sum += cos(i);
sin_sum += sin(i);
res *= cos_sum / sin_sum;
}
cout << res;
}
There are basically three quantities that accumulate during the iteration. They are:
C[N] = cos1+cos2+cos3+cos3+ .... cosN
S[N] = sin1+sin2+sin3+sin3+ .... sinN
X[N] = C[1] / S[1] * C[2] / S[2] * ... C[N] / S[N]
The recursive relations are:
C[0] = 0
C[N + 1] = C[N] + cosN+1
S[0] = 0
S[N + 1] = S[N] + sinN+1
X[0] = 1
X[N + 1] = X[N] * C[N+1] / S[N+1]
Using this it should be straightforward to write the loop:
#include <iostream>
int main() {
int n;
double X = 1;
double C = 0;
double S = 0;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++) {
C += cos(i);
S += sin(i);
X *= C / S;
}
cout << X;
}
I'm trying to create a program in c++ in which takes the users input and displays that specific row of Pascal's triangle.
i.e. inputting 3 should result in 1 3 3 1.
However when I input 3 in my program it displays "1 1 1 1". I think I'm fairly close but I can't quite work out what's wrong.
#include <iostream>
using namespace std;
int n, k, fact1, fact2, fact3, number1, number2, number3, nTakeAwayK, nChooseK;
void CalculateNChooseK();
int main()
{
cout << "Enter a row number for Pascal's Triangle: ";
cin >> n;
int x = 1;
for (int k = 0; k <= n; k++)
{
cout << x << '\t';
CalculateNChooseK();
x = nChooseK;
}
cout << endl;
system("PAUSE");
return 0;
}
void CalculateNChooseK()
{
fact1 = 1;
for (number1 = 1; number1 <= n; number1++)
{
fact1 = (fact1 * number1);
}
fact2 = 1;
for (number2 = 1; number2 <= k; number2++)
{
fact2 = (fact2 * number2);
}
nTakeAwayK = (n - k);
fact3 = 1;
for (number3 = 1; number3 <= nTakeAwayK; number3++)
{
fact3 = (fact3 * number3);
}
nChooseK = fact1 / (fact2*fact3);
}
Modify your code to this.. Hope this will help
for (k = 1; k <= n+1; k++)
{
cout << x << '\t';
CalculateNChooseK();
x = nChooseK;
}
As you are declaring k in for loop, This value is not working as global.
for (int k = 0; k <= n; k++) // don't declare k here.. and also loop from 1 to n+1 inclusive
So I'm trying to use a for loop to fill the array with the numbers 1-8. Then add:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + numb = x
And then save it to an variable called x. I've done filling the array, but I don't know how to calculate the summary of the array + the number you enter. Would appreciate some help a lot.
#include <iostream>
using namespace std;
int main()
{
int array[8];
int numb;
int x; // x = summary of array + numb;
cin >> numb; // insert an number
for (int i = 0; i <= 8; i++)
{
array[i]=i+1;
}
for (int i = 0; i < 8; i++)
{
cout << array[i] << " + ";
}
}
Change the last part to:
x = numb;
for (int i = 0; i < 8; i++)
{
x = x + array[i];
}
cout<<x<<endl;
Realistically though, if you wanted to add the first n whole numbers, there's a formula:
(n*(n+1))/2;
so your whole program would be:
#include <iostream>
using namespace std;
int main()
{
int n = 8;
int numb;
cin >> numb; // insert an number
int x = n*(n+1)/2+numb;
cout<<x<<endl;
}
For the initial loop, remove the =:
for (int i=0; i<8; i++) { array[i]=i+1; }
For adding all the elements of an array, then adding numb:
var x=0;
for (int i=0; i<8; i++) { x += array[i]; }
x+=numb;
Then you can cout you x variable.
Unless you're required to use for loops and arrays (e.g., this is homework), you might want to consider code more like:
std::vector<int> array(8);
// fill array:
std::iota(begin(array), end(array), 1);
int numb;
std::cin >> numb;
int total = std::accumulate(begin(array), end(array), numb);
std::cout << "Result: " << total;
I am trying to form a heap using the following code ,But not sure why its not showing the correct output.
#include <iostream>
using namespace std;
int h[10], n;
void heapbottom()
{
int i, j;
for (i = n / 2; i >= 1; i--) {
int k = i;
int v = h[k];
bool heap = false;
while (!heap && 2 * k <= n) {
cout << "\n i value is :" << i;
j = 2 * k;
if (j < n) //there sre 2 children
{
if (h[j] < h[j + 1])
j++;
}
if (v >= h[j])
heap = true;
else {
h[k] = h[j];
k = j;
}
h[k] = v;
} //end of while
}
cout << "\n HEAP GENERATED \n";
for (int i = 0; i < n; i++)
cout << "\n ELEMENT IS:" << h[i];
}
int main()
{
cout << "\n Enter the maximum number of array elements \n";
cin >> n;
cout << "\n Enter the array to perform heap sort \n";
for (int i = 0; i < n; i++)
cin >> h[i];
heapbottom();
return 0;
}
If I change the outer loop to be
for (i = n / 2; i >= 0; i--)
I get 9 8 7 6 5 2 as a result, which I believe is a valid heap.