C# :: Fibonacci Series - Adding Value In Array Inside While Loop
May 27, 2014
I am making a program to run Fibonacci series. I have created 2 array.
1)- 1st array holds only 0, 1
2)- 2nd array holds other values eg: 1, 2, 3, 5..........etc
I am using while loop to get the febonacci series and storing it in a 2nd array called int[] numbers.
After getting value using while loop, I am joing both the arrays using int[] final = arr.Concat(number).ToArray();
At last, I have used foreach loop to add the febonacci series into the listbox.
The problem I have is that, I cannot able to concat both the arrays. I tried to assign number array at the top of the while loop. so that number variable will be accessible outside the while loop. But I am getting a error.
See the code below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
[Code] .....
View 4 Replies
ADVERTISEMENT
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
Sep 3, 2014
When ever I enter a value higher than 10, my out put looks like this
How many numbers are in the Fibonacci Series? 20
0 0 0 0 0 0
0 0 0 0 34 21
13 8 5 3 2 1
1 0
Press any key to continue . . .
This is what I got when I entered the value 20.
As you can see, it will only display up to the 10 term of the Fibonacci sequence
I'm trying to get the output to look like this:
4181 2584 1597 987 610 377
233 144 89 55 34 21
13 8 5 3 2 1
1 0
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 20
void Fibonacci( int );
int main( ) {
cout << "How many numbers are in the Fibonacci Series? ";
[code].....
View 2 Replies
View Related
Mar 15, 2013
I was asked to write a code that has the user input a number and then the computer calculates it for the Fibonacci series. The output should be separated by commas and a period should follow the last number. Ex. 1,2,3,4,5. <---period
I can't seem to get the period at the end. I have the commas and everything else. Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
double num;
cout << "How many Fibonacci numbers do you want to display?";
[Code] ....
View 3 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
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
Apr 11, 2014
10101
01010
10101
01010
I want print above series using for loop.
View 2 Replies
View Related
Mar 7, 2013
Create a program that display all the fibonacci numbers and display the 21st digit using array.
Here's my code:
#include <iostream>
using namespace std;
int main () {
cout<<" This program shows a series of fibonacci numbers
"<<endl;
[Code] ....
View 2 Replies
View Related
Apr 2, 2014
I'm trying to create a simple calc program that does all the elementary calculations. How to get it to add and multiply continuously without crashing. but now i need to figure out how to ask the user if they want to continue or not. I'm having trouble here because in the while statement i have a scanf which asks for the operator symbol. and then asks for number in the second scanf..
So basically a user would have to enter 'R' twice for the message to pop up! Also i'm not sure how i would quite the program if they put in a 'N' for no. would return 0; work? What if they press 'Y' for yes, what would the return have to be then?
Here's a snippet of the code
int main () {
double result;
double new_number;
char symbol;
result = 0;
while(1==scanf(" %c", &symbol) && symbol == 'R' && symbol =='r')
[Code] .....
View 9 Replies
View Related
Aug 5, 2013
I have two files like original.txt and replace.txt which has equal nbr of lines in both of them.
I need to loop through these two files line by line and replace the line in original.txt to the line in replace.txt
eg :
output.txt :
1|raj|65.4|Y|
1|ramesh|65.4|Y|
replace.txt :
1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|
What I need here is when reading of output.txt reaches second line of file "1|ramesh|65.4|Y|", it has to be replaced with the second line in replace.txt "Cannot be processed|1|ramesh|65.4|Y|".
1|ramesh|65.4|Y| --> Cannot be processed|1|ramesh|65.4|Y|
After the end of loop the contents of two files should be like :
original.txt :
1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|
replace.txt :
1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|
The files can have variable number of lines but both will have same number of lines each.
View 1 Replies
View Related
Oct 26, 2013
I need to do make a loop inside a condition. Can it be done? I don't want to call another function to do it. Any way at all without calling separate function inside the if? I just want to do:
if (
for (int i = 0; i<=10; i++)
{
//stuff related to the for loop
} )
{
//stuff related to the initial if condition
};
View 7 Replies
View Related
Mar 8, 2013
Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
[Code] ....
I'm confused with this last [for] loop; How is ptr++ applied for non-integer value? Ptr is clearly a char, it comes from str, which reads string line from file.
I come from C# background, I have never met for...loop which irritates by using [somechar]++, not [someinteger]++;
What is actually going on there?
Some other similar example might be:
Code:
(iColor+(_parts[j]%length)*3),
where iColor is static unsigned char iColor[] array;
I would expect to see iColor[somevalue] + (_parts[j]%length)*3), but here in C++ I sometimes see that integer is being added directly to the array. What does it mean, what happens?
View 8 Replies
View Related
Nov 15, 2013
I've been working on a program on and off for around a week now and I've been struggling towards the end of the program.First of all, the program is a maths quiz which generates two random numbers per question.I'll give you one part of my code:
Code:
srand ( time(NULL) ); //seeds the random number generator
int score = 0;
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d;
}
[code]....
I've basically copied the above code 10 times and changed the variables by going through the alphabet e.g.
Code:
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d; all the way to
Code:
int an = rand()%12 +1;
int ao = rand()%12 +1;
int ap = rand()%12 +1;
int aq = an+ao-ap;
int ar;
Now what I'm going to do is remove all the declared variables and create a loop. But my problem is; If I wanted to declare four variables for e.g.
Code:
int a = rand()%12 +1;
int b = rand()%12 +1;
int c = rand()%12 +1;
int d = a+b-c;
Would I place the srand( time(NULL)); inside the loop? it's confusing because I know an example of a basic loop with an array would be:
Code:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int test[5]={21,18,47,21,4};
int I;
int total=0;
for (I=0;i<5;i++)
total += test[I];
}
[code]....
how or where to include the random number generator in the loop and to make it ask 10 questions at random.
View 1 Replies
View Related
Aug 25, 2014
I know that if I erase a value inside an iterator, it becomes invalidated, and this is how you would normally fix it:
for(std::vector<int>::iterator it = list.begin(); it<list.end(); ) {
if((*it) == 5)
list.erase(it);
else
it++;
}
But what about a double loop? like this one:
This would delete all duplicates
for(std::vector<int>::iterator it = list.begin(); it<list.end(); it++) {
for(std::vector<int>::iterator it2 = list.begin(); it2<list.end(); it2++) {
if((*it) == (*it2))
list.erase(it2);
}
}
View 6 Replies
View Related
Sep 10, 2013
What is difference (memory allocation or any) between declaring a variable inside or outside the variable
program1:
#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {
[Code] .....
Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.
View 1 Replies
View Related
Feb 22, 2013
I am writing a program where all work is done in the class methods. Main is used to call the methods. I need to know how to get a loop to work without any variables in main that can be used outside the methods. This is what I have in main:
#include <iostream>
#include <string>
using namespace std;
#include "FerryBoat.h"
int main() {
//create a constructor for a ferry boat
FerryBoat myBoat('B', 20, 'A');
[Code] ....
View 3 Replies
View Related
Nov 25, 2014
I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....
Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;
[Code] ....
View 6 Replies
View Related
Feb 24, 2015
The requirement was to set the txt files information equal to a variable before and not to put it directly into the array.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX = 24;
struct applicant_info {
[Code] .....
the txt doc looks something like this: file name: jobs4.txt
View 2 Replies
View Related
May 18, 2014
I'm expected to get a starting minimum input, and also an ending maximum output (for example: 21, and 25). From here, i have to give output using all the numbers (in a row) between the min and max numbers.
(for the same example:
21
22
23
24
25)
I assumed I would want to create an array using a variable, but i'm not sure of that either.
View 4 Replies
View Related
May 2, 2014
How to do the problem below using loop?
Using loop...
Output the total of the reciprocals of the numbers from 1 to 10:
1/1 + 1/2 + 1/3 + 1/4 ... + 1/10 = 2.928968
View 3 Replies
View Related
Feb 10, 2013
The problem deals with writing a program to geta series of integers from a user and storing those values into an array. Then use a function called selection_sort, when given an array with n elements, the function must sort the array from smallest to largest values.
I have code, and im trying to get it to compile but im getting these implicit declaration errors and conflicting types. Here is my code and the compiler errors.
Code:
Compilation started at Sun Feb 10 20:14:48
gcc -Wall -o ex9-1 ex9-1.c
ex9-1.c: In function 'main':
ex9-1.c:16:5: warning: implicit declaration of function 'selection_sort' [-Wimplicit-function-declaration]
ex9-1.c:20:2: warning: implicit declaration of function 'prinf' [-Wimplicit-function-declaration]
ex9-1.c: At top level:
[Code] ...
Compilation exited abnormally with code 1 at Sun Feb 10 20:14:49
Code:
#include <stdio.h>
int main(void) {
int a[100], i, j, N;
printf("How many numbers will you be entering: ");
scanf("%d", &N);
[Code] .....
View 4 Replies
View Related
Feb 19, 2013
I'm having a problem adding up the values of the dice rolls in a while loop. The program will only store the last value so how would I get it to store multiples values? Here is my program thus far. I would try an array but we haven't gotten to that yet.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int die_1=0, die_2=0, total_score1, total_score2, player1score;
char enter;
[Code] ....
What operators or equation I should use to accomplish this?
View 7 Replies
View Related
Aug 23, 2013
This is my code for a digital clock. It only refreshes when i type in a letter.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
//char dateStr [9];
char timeStr [9];
char almtimestr[9];
[code]....
View 3 Replies
View Related
Jun 7, 2013
Alright, so I have a code that's not giving me errors, but it doesn't seem to retain what I put into an array. Not sure If I'm missing something...
Code:
#include <stdio.h>
int main (void)
{
const char *pointer;
const char alphabet[] = "ABCDEFG";
pointer = &alphabet[5];
printf("pointing to %c of the alphabet
", pointer);
return 0;
}
Trying to get my pointer to return the letter in the [5] spot or "F". Not receiving any errors when compiling, but I seem to get different answers every time I run it.
View 5 Replies
View Related
Apr 6, 2014
Ive recently got into function pointers, i find that they can be quite handy for making your program very 'dynamic'.
However, the syntax is very confusing for what i want to do
This is what i want to do
I want to hold function pointers inside an array, but this array is dynamically allocated ( malloc, realloc, etc )
This is the current syntax ive come up with, but i dont think it is correct
void ( **drawFunc ) ( void*, SDL_Surface* );
View 2 Replies
View Related
Apr 18, 2013
class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------
int numLights;
int lights[MAX_LIGHTS];
[Code] .....
I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.
How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.
View 6 Replies
View Related