0
Array Storage
Good evening, I guess I'll result to asking questions bit by bit in regards to the program I'm working. There are no tutors available for computer science pass the first class for some reason. I hope to be able to bounce some ideas around on this site. Never worked with cpp before until this month. Back to the question, Is there a way to print off a list of values followed by a comma without an array? For example: 4, 89, 11, 30, 2 v/r Sherwin Slaughter
12 Respostas
+ 4
I don't really know C++ because my main is JavaScript. If you want to print a list of values of an array, I'm sure it is cout << A[0]. You can do a loop so it goes cout << A[1] and so on.
+ 3
Loop through the array elements to cout everything.
+ 3
Replace //Array function with cout<<n<<", "; if you just want to print as it goes along instead of saving it to an array.
+ 3
To find the largest number, you will need another variable, like x, called max which is updated every time the next n value is created.
+ 1
***Update***
/* The hailstone sequence takes 'n' that is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
// is reached.
*/
int hailstone_Sequence (int n)
{
int x = 0;
if (n > 1)
{
cout << n << " ";
while (n > 1)
{
if ((n % 2) == 0 )
{
n = n / 2;
}
else
{
n = (3 * n) + 1;
}
cout << n << " ";
}
return 0;
}
}
I was able to basically print out the starting number along the list to resemble the needed output required. Since trying to figure out the 0 showing up at the end of the list. I believe I may need to change the function to void...if that is possible since it is using the an int from the user.
0
@Jafca
Not sure what you mean by "already" if the array wasn't created.
0
@Jafaca
The values come from the code below:
/* The hailstone sequence takes 'n' that is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
// is reached.
*/
int hailstone_Sequence (int n)
{
int x = 0;
if (n > 1)
{
while (n != 1)
{
if ((n % 2) == 0 )
{
n = n / 2;
}
else
{
n = (3*n) + 1;
}
x++;//Will be used for the array's length
//Array function here;
}
return n;
}
}
0
@Cheeze
So you're suggestion (I believe) is the place the sequence within the array?
v/r
Sherwin Slaughter
0
@Jafca
Wow...I've been working on figuring out that portion of my code for days. Here is the output currently:
What number shall I start with? 99
The hailstone of sequence starting at 99 is:
298,149,448,224,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,1
The length of the sequence is ___.
The largest number in the sequence is:
The longest hailstone sequence with a number up to __ has length ___ The longest hailstone sequence with a number up to __ begins with ___
I'll try to fix the double "1"s at the end.
0
Well looking back at the output that is supposed to be show for example:
What number shall I start with? 7
The hailstone of sequence starting at 7 is:
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
The length of the sequence is 17.
The largest number in the sequence is: 52.
The longest hailstone sequence with a number up to 7 has length 17 The longest hailstone sequence with a number up to 7 begins with 7.
This seems to be way over a beginners head. Up to the challenge though. I have to figure out how to get my code to do the same.
0
***Changing hailstone_Sequence to void***
All that is shown in the output is compilation error. Below is my updated code:
//Function Variable Declaration.
int lengthof_Sequence(int x);
void hailstone_Sequence (int n);
int main(int argc, char** argv)
{
int user_Input;
cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << endl;
cout << "The hailstone of sequence starting at " << user_Input << " is: " << endl;
cout << hailstone_Sequence(user_Input) << endl;
cout << "The length of the sequence is ___. \n";
cout << "The largest number in the sequence is: \n";
cout << "The longest hailstone sequence with a number up to __ has length ___ ";
cout << "The longest hailstone sequence with a number up to __ begins with ___ ";
return 0;
}
/* The hailstone sequence takes 'n' that is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
// is reached.
*/
void hailstone_Sequence (int n)
{
int x = 0;
if (n > 1)
{
cout << n << " ";
while (n > 1)
{
if ((n % 2) == 0 )
{
n = n / 2;
}
else
{
n = (3 * n) + 1;
}
cout << n << " ";
}
// return " ";
}
}
Not sure how I went about this wrong. It's a function prototype and should be able to be called after the main. I'm trying to avoid a number appearing after 1 with the sequence.
0
I changed the while condition from n > 1 to n > 2, with a return of one for the following output:
What number shall I start with? 99
The hailstone of sequence starting at 99 is:
99 298 149 448 224 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is ___.
The largest number in the sequence is:
The longest hailstone sequence with a number up to __ has length ___ The longest hailstone sequence with a number up to __ begins with ___
@Jafca
I'll now look into your suggestion of max.
I noticed I needed to actually take out the commas for output requirement.
I'll return to this in the morning. Appreciate all the help so far.
v/r
Sherwin Slaughter