1

I just switched from nursing to being a computer science major and I have no Idea what is going on with the later assignments we have been doing. I just end up getting lost. I have attempted to look from how to go about approaching this particular program, but, I still don't understand how.

Basically what is happening here, is I need to create a c++ program that generates to the screen the largest value of a sequence, the position that largest value is in, how many steps it takes for the value to equal 1, and the x value.

By the way, all of this data is supposed to be read form a file.

for example, to make it more clear about what I am talking about,

There are two equations being used for this random value of x to eventually get to 1.

Odd numbered values= 3 * number + 1 Even numbered values= num/2

example. 75 is in the data file. 75 is odd. multiply 75 by 3 and add 1 you get 226. 226 is even. divide by 2. you get 113. And so on and so forth until you eventually reach 1. Here is what that looks like:

x=75,then k = 14 and the numbers in the sequence are: 75, 226, 113, 340, 170,
85, 256, 128, 64, 32, 16, 8, 4, 2, 1.

k would be reference to the steps it took to reach 1. 4 would be the position of where the largest value is WITHIN that sequence.

I have some really bad code... Please disregard how bad it is. I just can't think of how to write with the information I have been given.

#include <iostream>
using namespace std;


int main (){

  int x;//The integer in the file                                                
  int count=0; // Counting how many integers there are in the file               
  int steps; // Number of steps it takes to reach 1                              
  int largestValue; // Largest Value in the sequence                             
  int position; // The position of where the largest value is in the sequence    
  int a;// Number that needs to be constantly changing in a sequence             
  int counter = 0; // Counting the values in a file                              

 cin >> x;

 while(!cin.eof()){

   cin >> x;
   counter++;
 if (x % 2 == 0){
  a = x/2
   else
 a = (3 * x) + 1
    }


  cout << "Starting at "<< x << " it takes " << steps  << "to reach 1" << endl;
  cout << "The largest number in the series is" << largestValue << "at position"
 << position << endl;

  return 0;
}
Lou Osman
  • 11
  • 1
  • Looks like you have your outer loop to read numbers from a file. You need an inner loop to reduce the number to 1 which you haven't got yet. Also, you are reading in x twice which will cause it to ignore the first value. – David Jones Dec 05 '17 at 03:15
  • cin.eof() flag is set only after read attempt. The value returned by this function depends on the last operation performed on the stream (and not on the next). You must check it after cin >> x. – 273K Dec 05 '17 at 03:23

0 Answers0