In the function largest, it works correctly for numbers greater than 2 such as 3, but does not give me the correct results for numbers 1 and 2 when entered. How would I fix this.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int nexta(int a);
int enter(int& a, string filename);
int evenodd(int h, int& even, int& odd);
int calculator(int a, string filename);
int largest(int a, int& j, int& y, int k);
int countSequence(int n);
int continueCalc();
void howMany();
void printInfo();
int main ()
{
//static int count = 0;
string filename;
bool redo = false;
int n;
int k = 0;
int even = 0;
int odd = 0;
int j = 0;
int y = 0;
int h = 0;
char choice = 'Y';
ofstream outFile;
outFile.open("Langlands.txt");
while(choice == 'Y' || choice == 'y')
{
system("CLS");
printInfo();
int a = 1;
enter (a, filename);
calculator(a, filename);
nexta(a);
largest(a, j, y, k);
evenodd(h, even, odd);
cout << "=================================\n" << endl;
cout << "Would you like to run this program again? " << endl;
cout << "Enter Y or y to continue, or any other key to exit: ";
cin >> choice;
howMany();
}
outFile.close();
system ("pause");
return 0;
}
//==================================================================//
void printInfo()
{
cout << "=================================\n" << endl;
cout << "Programmer Name: Peter Langlands\n"
<< "Program 3 Description: Analyzes sequences\n"
<< "CS 150 Spring 2011 Lab CRN: 26682\n"
<< "Date:" << endl;
cout << "=================================\n" << endl;
}
//==================================================================//
int enter(int& a, string filename)
{
bool confirm = false;
ofstream outFile;
while (!confirm)
{
cout << "Enter an positive number: ";
cin >> a;
outFile << "Enter an positive number: ";
cout << endl;
if ( a <= 0 )
{
cout << "This number is invalid. Please try again!" << endl;
cout << "=================================\n" << endl;
outFile << "This number is invalid. Please try again!" << endl;
outFile << "=================================\n" << endl;
confirm = false;
}
else
confirm = true;
}
}
//==================================================================//
int calculator(int a, string filename)
{
int k = 0;
int sum = a;
int even = 0;
int odd = 0;
int h = a;
int j = 0;
int y = 0;
int n;
ofstream outFile;
cout << "a0 = " << a << endl;
while ( a > 1 )
{
if ( a%2 == 0 )
{
a = a / 2;
sum = a + sum;
even ++;
}
else
{
a = 3 * a + 1;
sum = a + sum;
odd ++;
}
k ++;
cout << "a" << k << " = " << a << endl;
largest(a, j, y, k);
}
evenodd(h, even, odd);
cout << "\nThe integer k such that a_k = 1, is: " << k << endl;
cout << "The sum of the series equals: " << sum << endl;
cout << "The largest number is: " << j << endl;
cout << "The largest number int the sequence is in position: a" << y << endl;
cout << "Number of odd values in the sequence: " << odd << endl;
cout << "Number of even values in the sequence: " << even << endl;
return a;
}
//======================================================================
// nextNo(n) returns half of the input if the input is even, and returns
// the 3 times the input plus 1, if the input is odd.
//======================================================================
int nexta(int a)
{
int sum = a;
int even = 0;
int odd = 0;
if(a%2==0) //if even
{
a = a / 2;
sum = a + sum;
even ++;
}
else //if odd
{
a = 3 * a +1;
sum = a + sum;
odd ++;
}
return a;
}
//==================================================================//
int largest(int a, int& j, int& y, int k)
{
if ( a >= j )
{
j = a;
y = k;
}
}
//==================================================================//
int evenodd(int h, int& even, int& odd)
{
int i;
i = h % 2;
if ( i == 0 )
odd ++;
else
even ++;
}
//==================================================================//
void howMany()
{
static int count = 0;
count++;
cout << "=================================" << endl;
cout << "\nThanks for using the super sequence analyzer!" << endl;
cout << "This progam has ran " << count << " times." << endl;
cout << "=================================\n" << endl;
}
//==================================================================//