C :: Algorithm To Find Kth Term Of A Series
Dec 8, 2014
I want to write an algorithm to find the kth term of a series given by the following recursive formula:
a_0=1 and a_(k+1)=a_k+1/k!
My code (part of it responsible for calculating the value of a series) does not want to compile:
Code:
float series(int n)
{
float F0 = 1;
float F;
unsigned int i;
if (n == 0)
return(1);
[Code]...
thus my question is why it does not want to work properly and what should i change (for certain factorial function is OK)?
View 7 Replies
ADVERTISEMENT
Oct 26, 2014
Design an algorithm using flowchart or pseudo-code to prompt the user for a series of positive integer values. Calculate the average of these values and display the average at the end of the program. Assume that the user types the sentinel value -1 to indicate end of data entry.
Sample input-output:
Enter a positive integer (-1 to end) : 5
Enter a positive integer (-1 to end) : 7
Enter a positive integer (-1 to end) : 6
Enter a positive integer (-1 to end) : -1
The average value is: 6
I searched online and found out this solution however it is only for three numbers.Is there any way of modifying this to include the sum of x numbers / number of x(integers) to find the average?
View 5 Replies
View Related
Mar 6, 2015
Example input: 2 4 6 -1 7 3 -2 1
Output: 3 3 (longest series of even is 3, longest series of positive is 3)
Here is the code:
Code:
#include <stdio.h>
int even(int x) {
return x % 2 == 0;
}
int positive(int x) {
return x>0;
[Code]...
My question is how to write this code if the prototype of function is:
Code: void series(int *array, int n, int (*s)(int), int **begining, int *lenght);
View 1 Replies
View Related
Mar 16, 2015
program:
Example input: 2 4 6 -1 7 3 -2 1
Output: 3 3 (longest series of even is 3, longest series of positive is 3)
Here is the code:
#include <stdio.h>
int even(int x) {
return x % 2 == 0;
}
[Code].....
My question is how to write this code if the prototype of function is:
void series(int *array, int n, int (*s)(int), int **beginning, int *length);
View 1 Replies
View Related
Feb 5, 2015
I'm trying to write a program to find values for arctan of x by using taylor series. An initial value of x is given by the user and then it should print solutions from arctan(x) to arctan(1) in increments of x+0.1. It prints correctly but gives incorrect values after the initial x. I'm new to c and need some way to 'reset' the functions f1 and f2 for each increment of x (I think...)
Here's the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
/* Define the beginning of the program and each variable. Also opens a text file to be written to */
FILE *f = fopen("arctan.txt", "w");
[Code] ....
View 10 Replies
View Related
Mar 10, 2015
I need to be able to test this algorithm 50 times including extremes and invalid inputs.
I keep getting invalid input
#include <stdio.h>
#include <math.h>
void main() {
double x, i, y, z;
int N, n;
printf("Enter limit polynomial N
[code]....
View 3 Replies
View Related
Jan 20, 2013
write algorith to display first n odd numbers ?
View 8 Replies
View Related
Mar 27, 2014
I have to made a programme which will search for given number and it must work in O(log(n)). The problem is that this programme beside finding this number have to find how many times this given number is used in this sequence.
Sequence is from lowest to highest only one possibility to use binary search algorithm
For example when we have squence
-1 -2 3 3 3 3 4 5 6 7 7 8 9 9 9 9 10
The numbers we need to search are
1 , 3 , 7 , 9 , 11 , 12
The answer is
0 , 4 , 2 , 4 , 0 , 0
So we need to find the sum of used number in sequence.
I have written algorithm Code: int start = 0;
int end = sequencelenght - 1;
int mid = 0;
/// Binary serach
while (start<=end) {
int mid=(start+end)/2;
if (sequence[mid]==givennumber) {
[Code] .....
As u see i search for given numer with binary with O(log(n)) but when i have to sum the duplicates the only good way i see is using loop to right and left but this have got log(n) specification (because when sequence would be for example 7 7 7 7 7 7 7 7 7 and given number to search will be 7 this will be O(n) loop).
How would look the most optimal algorithm look for this exercise? I mean O(log(n)) the fastest algorithm....
View 6 Replies
View Related
Apr 10, 2014
I am trying to implement Boruvka's algorithm to find the minimum spanning tree of a graph but my program crashes in the find function when i run it.
[URL] .....
sample input : [URL] .....
View 1 Replies
View Related
Apr 19, 2012
Code that finds a shortest path in a dense graph, using Dijkstra's algorithm in the simple array version. You write a function
struct listnode shortest path(int n, int s, int t, int *dist)
with
struct listnode f struct listnode * next; int vertexnumber; g ;
Being used to return the list of vertices on the shortest path. Your function has the following arguments:
- n: the number of vertices of the graph,
- s: the start vertex,
- t: the target vertex
- dist: the matrix of edgelengths.
The vertices are numbered from 0 to n -1, so s and t are numbers in that range. dist is a pointer to the n * n matrix of edgelengths; vertices which are not connected will be joined by an edge of length 9999. To access the array element dist[i][j], we can use *(dist + i*n + j). Your function should return the list of vertices on the shortest path from s to t, starting with s and ending with t.
View 4 Replies
View Related
Nov 7, 2014
I know how to sort an array but how I can sort an array regard to nth term.
Example: 2 --> nth digit 4 45 62 1 900 105 -->inputs
Output: 001 004 105 900 045 065
View 1 Replies
View Related
Nov 7, 2014
I know how to sort an array but how I can sort an array regard to nth term.
Example:
2 --> nth digit
4 45 62 1 900 105 -->inputs
Output:
001
004
105
900
045
065
View 1 Replies
View Related
Apr 21, 2013
in this area and I have this error Invalid expression term 'out' and Identifier expected; 'out' is a keyword
public static void userDell(string msi,String filename, String DB_CONN_STRING)
{
String temp_msisdn = "+" + msi;
bool ima = DatabaseConnection.checkExist(temp_msi);
[Code].....
View 18 Replies
View Related
Jul 30, 2012
A class called 'sample' with the data a, b and c and two methods as defined below. Define a new member method 'term()' which calculates the value of the term (2b - 4ac) and displays it.
Define the method outside of the class definition. Write the main method to create an array of 2 objects called D of class 'sample' and display the values and find the solution to the term (2b - 4ac)
My code so far:
#include <iostream>
using namespace std;
int term(int a, int b, int c) {
int result;
result = (2*b - 4*a*c);
[Code] ....
Problem: I keep getting error messages, here is a look at the ones i'm getting.
error: ISO C++ forbids declaration of 'cout' with no type.
error: expected ';' before '<<' token.
error: new types may not be defined in a return type
error: expected unqualified-id before '{' token.
View 5 Replies
View Related
Aug 7, 2014
i'm implementing a templated function callback.
I get this error:
"error C2064: term does not evaluate to a function taking 1 arguments"
I'm not sure why.
Here's the code for the template function in the timer class header:
template<class T>
void TimerEvent(DateAndTime DateTime, std::function<void(T)> Callback);
Now the class body:
template<class T>
void GameTimer::TimerEvent(DateAndTime DateTime, std::function<void(T)> Callback)
{
DateAndTime time;
time.Now(false);
if (time.Compare(DateTime) == 1)
Callback(T);
}
and calling the class in the Engine:
int test1(int a)
{
}
void Main()
{
gameTime.TimerEvent<int>(gh, test1(1));
}
View 6 Replies
View Related
Dec 8, 2014
The first call of my function works with empty vectors but fails when dimensions are introduced to the vectors.
When "uncommented, the commented out function call fails with the error above.
These links might be useful for those unfamiliar to QuantLib:
[URL]
QuantLib: GeneralLinearLeastSquares Class Reference
Code:
#include "stdafx.h"
#include "boost/multi_array.hpp"
#include <cassert>
#include <qlmathgenerallinearleastsquares.hpp>
#include <vector>
#include <list>
#include <iostream>
#include "boostlambdalambda.hpp"
[code]....
View 8 Replies
View Related
Feb 26, 2013
Why isn't this program working?
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int n;
float x, t, sum=0;
[Code] ......
View 15 Replies
View Related
Feb 25, 2015
Write a function that computes the Taylor series expansion of ex using the following formula, where x is the exponent, and n is the number of discrete trials to run:
𝑒𝑥=1+𝑥+𝑥2+𝑥3+𝑥4+⋯+𝑥𝑛 =∑𝑛 𝑥𝑖 2! 3! 4! 𝑛! 𝑖=0 𝑖!
NOTE: This formula will NOT work if you calculate the numerator and denominator separately and then add the division result. You must use your knowledge of algebra to make sure you’re not calculating the entire exponent and factorial, but rather their factors per loop iteration.
Input prompt
Enter the values for x and n:
Output Prompt
Taylor series estimation is XXXX.XXXX
This is what I have so far for the function:
void getTaylor() {
double estimation
cout << "Enter the values for x and n:" << endl;
cin >> x >> n;
cout << "Taylor series estimation is" <<
}
I now need to create the loop so that I can enter in for my function.
View 2 Replies
View Related
Oct 1, 2014
Write a program that approximates the value of π using the Leibniz series and compares the Leibniz series approximation against the known value of π and two other common approximations of π (22/7 and 355/113). The program must prompt the user for the desired number of terms (n) to use in the approximation (as an integer) and calculate the approximate value of π to the given number of terms. The program must display each approximation of π and display the difference between each approximation.
I understand how to prompt for the number, what I don't even get how to start is to use it to approximate the value of pie in that given number. Also how to tell the difference between each approximation...
View 1 Replies
View Related
Oct 11, 2014
I can't seem to get the math portion right. It is supposed to approximate pi using (sigma,i=-1 to infinity)(-1)^(i+1)(4/(2i-1))=4(terms of pi). And what I have does some math but it is incorrect because I get a negative value for pi and one that is entirely too large at that. The precision is also to be at 20 decimal places. I also need it to end immediately after if I get an invalid input, but I can't seem to get it to end after trying a few different things it will say that it is an invalid number, but will continue to run the math loop. I also need the final cout to print all the terms that is multiplied by 4.
[code]#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
int nterm, numerator;
double sum = 0.0;
const int upperBound = nterm * 2;
cout<<"This program approximates pi using an n-term series expansion. "<<endl;
[code]....
View 11 Replies
View Related
Mar 3, 2013
in code my prompt said :
(computing Π (PI) ) You can approximate (PI) value by using the following
series:
Π = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... - 1/(2i -1) + 1/(2i + 1)
Write a method that calculate value of π by using the above series, using
the following header:
public static double pi()
assume i = 1000000
ok so I have been testing it but I really did get any far :
for ( double i = 1; i< 1000000; i+=2) {
// with these I try to increment the value of i to get to the series. I did these after and test it and the output is all wrong and I know it
sum = (1/((2*i)+1));
sum = sum + (1/((2*i)-1));
sum ++;
double pi = 4*(sum);
// I know is wrong I but I can't figure it out what to do.
View 6 Replies
View Related
Mar 30, 2014
create a program that trigonometric series ∑(k-3)sin(π/k-3) with k = 4 until 13..
View 1 Replies
View Related
Sep 29, 2014
I'm attempting to print a series of text from this loop, but for some reason all I am getting is the sum total and the number 1, like this
1 5175.11
When I want to get it for each iteration of the loop. I've cycled over this and compared it to my other loops I've used and I'm lost as to why this isn't working. I also tried using a do while but that didn't work as well.
for (double total1 = 0; total1 < goal; total1 + save_amt) {
total2 = ((factor - one) * save_amt);
total3 = (total2 / fr_interest);
total1 = total1 + total3;
months_dis = (months_dis + one);
cout << months_dis << " " << total1 << endl;
}
View 14 Replies
View Related
Feb 26, 2013
I'm having trouble, I want the inner loop to display the lowest and highest score. If I assign initial values for the high and low score, it won't always work (because if no/ all scores are above/ below my initial values...)
But without assigning initial values for highscore and lowscore, One of them will always be undefined during the first runthrough.
#include <iostream>
using namespace std;
const int AGRADE = 90;
const int BGRADE = 80;
const int CGRADE = 70;
const int DGRADE = 60;
int main() {
[code]....
how do i set this up so it stores a low and high score, and then compares those to each next number in the series?
View 6 Replies
View Related
Dec 6, 2013
I need to write a program that not only calculates the minimum and maximum, but also calculates the average and the difference between the largest and the smallest.
Here's the code:
#include <iostream.h>
#include <conio.h>
#inlcude <math.h>
#include <windows.h>
[Code].....
I know the problem is the "sdt::cin" but I don't know what else to do
View 1 Replies
View Related
Oct 23, 2014
How to remake the code that i`ve written to have : a recursive function to evaluate the first n terms in series specified y= 1 - x + x^2/2 - x^3/6 + x^4/24 +....+(-1)^n x^n/n!
And this is my code:
#include <iostream>
using namespace std;
double power(int n, double x) {
double d =1;
for (int i = 1 ; i<=n ; i++)
[Code] ....
View 12 Replies
View Related