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;
}
//==================================================================//

I suggest you make a much much shorter example (20 lines instead of 200). The inputs should be hardcoded. You should be able to clearly demonstrate and show us the input, the current erroneous output, and the expected output.

Also tell us the purpose of the program, what its supposed to do. AFAIK the program is supposed to count the number of cats that fall from the sky when "its raining cats and dogs".

I suggest you make a much much shorter example (20 lines instead of 200). The inputs should be hardcoded. You should be able to clearly demonstrate and show us the input, the current erroneous output, and the expected output.

How would I fix it, if I put in 1 or 2, it gives me the wrong numbers.
*Output

=================================

Programmer Name: Peter Langlands
Program 3 Description: Analyzes sequences
CS 150 Spring 2011 Lab CRN: 26682
Date:
=================================

Enter an positive number: 3

a0 = 3
a1 = 10
a2 = 5
a3 = 16
a4 = 8
a5 = 4
a6 = 2
a7 = 1

The integer k such that a_k = 1, is: 7
The sum of the series equals: 49
The largest number is: 16
The largest number int the sequence is in position: a3
Number of odd values in the sequence: 2
Number of even values in the sequence: 6
=================================

Would you like to run this program again?
Enter Y or y to continue, or any other key to exit: y
=================================

Thanks for using the super sequence analyzer!
This progam has ran 1 times.
=================================

=================================

Programmer Name: Peter Langlands
Program 3 Description: Analyzes sequences
CS 150 Spring 2011 Lab CRN: 26682
Date:
=================================

Enter an positive number: 2

a0 = 2
a1 = 1

The integer k such that a_k = 1, is: 1
The sum of the series equals: 3
The largest number is: 1
The largest number int the sequence is in position: a1
Number of odd values in the sequence: 1
Number of even values in the sequence: 1
=================================

Would you like to run this program again?
Enter Y or y to continue, or any other key to exit: y
=================================

Thanks for using the super sequence analyzer!
This progam has ran 2 times.
=================================

=================================

Programmer Name: Peter Langlands
Program 3 Description: Analyzes sequences
CS 150 Spring 2011 Lab CRN: 26682
Date:
=================================

Enter an positive number: 1

a0 = 1

The integer k such that a_k = 1, is: 0
The sum of the series equals: 1
The largest number is: 0
The largest number int the sequence is in position: a0
Number of odd values in the sequence: 0
Number of even values in the sequence: 1
=================================

Would you like to run this program again?
Enter Y or y to continue, or any other key to exit: n
=================================

Thanks for using the super sequence analyzer!
This progam has ran 3 times.
=================================

Press any key to continue . . .

Also tell us the purpose of the program, what its supposed to do. AFAIK the program is supposed to count the number of cats that fall from the sky when "its raining cats and dogs".

The program asks you to input a number and it will perform the sequence like down below in my output.

I'm trying to get you to tell us what the program is supposed to do. Don't tell me to look at the code because the code must be wrong. If the code were right you would not be asking questions about it.

I'm trying to get you to tell us what the program is supposed to do. Don't tell me to look at the code because the code must be wrong. If the code were right you would not be asking questions about it.

2. The output for each run should contain the labeled echo of the input data and the additional items illustrated in the output example below
3. Your program should run for as long as the user chooses to continue using it.
4. Your solution should utilize functions. For full credit, you must have at least three meaningful functions. One of these functions can be a void function that prints out the programmer and program information for your project. Another function should include a static variable to count the number of times that the program has been run by user (this info should be output upon exit).
5. Do not use global variables. Pass parameters.
6. Write the output for all data items to the screen ; in addition, create an output file that contains the output for each run of the program. Name the output file with yourlastname.txt
7. Output the count of the number of values in the series processed.
8. Output the total sum of all of the values in the sequence.
9. Output the largest value, and the position of the largest value in the sequence.
10. Output the number of even values, and number of odd values in the sequence.
11. Your output must include a proper heading and programmer information.
12. Print an error message if the input value is out of range, or the output data file is not available.

Hello plang007,
In your calculator function you are passing the variable 'a' which is altered in the if else block above it. You are not passing the positive value which the user entered. Thats why you get 1 as the largest number when the user enters 2.

if ( a%2 == 0 )
       {
            a = a / 2;
            sum = a + sum;
            even ++;
       }

Edited 5 Years Ago by Arbus: n/a

This article has been dead for over six months. Start a new discussion instead.