fibonacci sequence sum total error - c++

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<" FIBONACCI "<<endl;
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int b= 0, c=1;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=b+c;
c=b-c;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
whenever i want to repeat it by giving 0 as the last input the value of sum doesn't reset itself and the new sum gets added to the previous one and a wrong value is displayed as the total.

your variable sum is not initialized before you access it.
You should change
int a,sum;
to
int a;
int sum = 0;

You have declared sum incorrectly it should be the same type as b and c
long long unsigned int sum;
and sum was uninitialised too, but: your calculation is very strange
sum+=b;
b=b+c;
c=b-c;
So I recommend
sum = c + b;
b = c;
c = sum;

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<"FIBONACCI \n\n";
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int sum=0,b=0,c=1,d=0;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=d+c;
c=d;
d=b;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
the change was made in calculation and sum is initialised to zero in beg of the do..while loop and sorry for bad punctuation

Related

how to find summation of all elements of array c++

I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
int array[100];
int sum;
for(int i=0; i<100; i++){
cout<<"Insert element "<<i<<": ";
cin>>array[i];
sum = array[i]+ //summ with the next array input;
cout<<sum;
}
return 0;
}
means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input
C++ standard library already has a function to do this which is std::accumulate:
#include <iostream>
#include <numeric>
int main() {
int array[5] = {1,2,3,4,5};
int total = std::accumulate(std::begin(array), std::end(array), 0);
return 0;
}
If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.
initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum.
Hope this will help.
You can use a do while loop:
#include<iostream>
#include<string>
#include<math.h>
int main()
{
int array[100];
int sum=0;
int i=0;
std::cout<<"Insert element"<<" "<<i<<": ";
std::cin>>array[i];
do
{
sum=sum+array[i];
std::cout<<sum<<std::endl;
i++;
std::cout<<"Insert element"<<" "<<i<<": ";
}while(std::cin>>array[i]);
return 0;
}
If all you want is the sum then just compute the sum. No need to store anything:
#include <iostream>
#include <string>
#include <math.h>
int main() {
int sum = 0;
for(int i=0; i<100; i++){
std::cout << "Insert element " << i << ": ";
int t;
std::cin >> t;
sum += t
std::cout << sum;
}
}

Return value always 0 unless I use cout

The function should return 156 with a "Z" input but it outputs 0.
If the letter's U-Z, the function finds the largest factor of the letters numeric value other than the value itself and multiplies it by 12.
Tried putting cout in the function and it works somehow. Don't know where's the problem. Someone help.
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int uz(int n);
int main(){
string letter, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphSize=alphabet.size(), l=0, total=0;
cin>>letter;
int letterSize=letter.size();
for(int i=0;i<letterSize;i++){
for(int j=0;j<alphSize;j++){
if(toupper(letter[i])==alphabet[j])
l=j+1;
}
if(l>20&&l<=26){
l=uz(l);}
total+=l;
}
cout<<total<<endl;
return 0;
}
int uz(int n){
int size=1;
int arr[size];
for(int i=1;i<=n;i++){
if(n%i==0){
size++;
arr[size]+=i;
}
}
n=12*arr[size-1];
//cout<<n;
return n;
}

Getting wrong output for my program in C++

#include <iostream>
using namespace std;
int num(int n){
for(int i= 1 ; i<=n ; i++){
int sum=0;
sum += i;
cout<<sum;
}
}
i guess this part is clear.
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}
ive tried looking it up but no results found....
ive always had some trouble with the loops.
You are throwing away what is calculated by previous iterations.
You are not returning what is calculated.
To fix, get the declaration and initialization of i out of the loop and return the result.
#include <iostream>
using namespace std;
int num(int n){
int sum=0;
for(int i= 1 ; i<=n ; i++){
sum += i;
cout<<sum;
}
return sum;
}
Try moving the definition and initialization of the variable sum to outside of the for loop, preferably before it. Do not forget your return value from the function.
The function num() should return the variable sum and variable Sum should be defined outside for loop.
Your code should be
#include <iostream>
using namespace std;
int num(int n){
int sum=0; //sum should be declared outside for loop
for(int i= 1 ; i<=n ; i++){
sum += i;
// cout<<sum; //dont print sum here it will be printed in main()
}
return sum; //you missed this
}
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}

Find the biggest sum out of the all, it containing numbers and

I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.

Issue with `int` vs. `double` - why isn't my program working?

I have just started teaching myself C++ after 2+ years of working with MATLAB and wanting something a little more robust. I am currently using code::blocks with MinGW I am trying to get the hang of using int and double, but for some reason my little test program won't work.
The program was a way for me to practice using functions. The program takes in numbers from the user and stops when a negative number is inserted. Then, it spits out the sum of the input numbers, product of input numbers, and a count of the numbers processed.
When I use only int, the program works as intended, but can't handle any "decimal" numbers thrown at it - it freaks out.
#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;
int X;
int sum;
int product;
int SUM()
{
int A = sum+X;
sum = A;
}
int PRODUCT()
{
int B=product*X;
product = B;
}
int main()
{
cout<<"Welcome to my counting program! "<<endl;
sum = 0;
product = 1;
int counter = 0;
for (;;)
{
cout<<"Give me a number: ";
cin>>X;
if (X<0)
{
break;
}
sum = SUM();
product = PRODUCT();
counter = counter+1;
}
cout<<"SUM: ";
cout<< sum <<endl;
cout<<"PRODUCT: ";
cout<< product <<endl;
cout<<"NUMBERS INPUT: ";
cout<< counter <<endl;
system ("PAUSE");
}
However, when I use double where applicable, the program only gives me zeroes for the product and sum.
#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;
double X;
double sum;
double product;
int SUM()
{
double A = sum+X;
sum = A;
}
int PRODUCT()
{
double B=product*X;
product = B;
}
int main()
{
cout<<"Welcome to my counting program! "<<endl;
sum = 0.0;
product = 1.0;
double counter = 0.0;
for (;;)
{
cout<<"Give me a number: ";
cin>>X;
if (X<0)
{
break;
}
sum = SUM();
product = PRODUCT();
counter = counter+1;
}
cout<<"SUM: ";
cout<< sum <<endl;
cout<<"PRODUCT: ";
cout<< product <<endl;
cout<<"NUMBERS INPUT: ";
cout<< counter <<endl;
system ("PAUSE");
}
What am I doing wrong? what do I need to change to make it work?
Also, I think I am calling my functions incorrectly and trying to pass values between them incorrectly - what is the correct way to do so?
You're still returning int from your functions. Also, since you're assigning the results, you need to return an appropriate value (sum = SUM();).
Try the following changes:
double /*int*/ SUM()
{
double A = sum+X;
sum = A;
return sum; // Return a value!
}
double /*int*/ PRODUCT()
{
double B=product*X;
product = B;
return product; // Return a value!
}
I would also recommend avoiding the globals for sum/product/X, and instead declare them local to your functions, and pass them as arguments to your methods.