1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
blondinia [14]
3 years ago
13

Write a function that receives an integer (n) argument and then computes the following based on the value of the integer: While

the value of n is greater than 1, replace the integer with half of its value (n/2) if the integer is even. Otherwise, replace the integer with three times its value, plus 1 (3*n + 1). Make provision to count the number of values in (or the length of) the sequence that results. Test your function: If n = 10, the sequence of integers is 5, 16, 8, 4, 2, 1 and so the length is 6. Make a plot of the length of the sequence that occurs as a function of the integers from 2 to 30. For example, when n = 10, the length is 6 while for n = 15, the length is 17.
Computers and Technology
1 answer:
777dan777 [17]3 years ago
5 0

Answer:

#include <iostream>

using namespace std;

int func(int n)//function..

{

   if(n>1)

   {

       if(n%2==0)//checking if the number is even.

       {

           return n/2;

       }

       else

       {

           return (3*n+1);

       }

   }

}

int main() {

   int num;//taking input of the n.

   cin>>num;

   while(num>1)//looping while the value of num is greater than 1.

   {

       int a=func(num);//calling function.

       cout<<a<<" ";//printing .

       num=a;//changing the value of num.

   }

return 0;

}

Input:-

10

Output:-

5 16 8 4 2 1

Explanation:

I have created the function named func. Then returning the values according to the problem.In the main function taking the input of integer num. Then calling a loop and calling the function till the value of n is greater than 1 and printing all the values returned by the function.

You might be interested in
Writa function to read content from "Zone. txt" and write it into another file "zone1. txt" by reversing each line if the line s
julia-pushkina [17]

Answer:

You have specified no language. Here's an algorithm to follow with most platforms, my example is in NodeJS

const fs = require('fs');

let zone = fs.readFile('Zone.txt', 'utf8', function (err, data) {

 if (err) throw new Error(err);

 data = data.toString(); //convert the buffer to a string

 let lines = data.split("\n"),

       content = [];

 for (i in lines) {

   let l = lines[i];

   if (l.toLowerCase().startsWith("a")) l = reverseString(l);

   content.push(l);

 }

 content = content.join("");

 fs.writeFile('zone1.txt', content, , function (err) {

   if (err) throw new Error(err);

 }

});

function reverseString (str) {

   return str.split("").reverse().join("");

}

5 0
4 years ago
The economist George gilder argued that
Vera_Pavlovna [14]

Answer: Lower taxes would result in greater economic growth.

Explanation: My teacher just gave me the answer on Apex

4 0
3 years ago
Plymouth Colony was originally founded by a group of people called Pilgrims. Since Plymouth did not have an official charter fro
quester [9]

Plymouth Colony was originally founded by a group of people called Pilgrims. Since Plymouth did not have an official charter from England, its government was based on which document? U.S. Constitution Fundamental Orders of Connecticut Articles of Confederation Mayflower Compact.

5 0
3 years ago
On my iphone there is system services, what are they.
rosijanka [135]
Maybe for what it can do
3 0
3 years ago
The main technology for setting up a wireless local area network is​
julsineya [31]

Answer:

llllllffl

Explanation:

nnñbh hi Luigi

6 0
3 years ago
Other questions:
  • What e-reader technology males a screen that is easy to read and extends battery life
    6·1 answer
  • Fill in the missing step in the following deadlock situation. Two users from the local board of education are each running a pro
    15·1 answer
  • Define the term Frame Rate.
    12·1 answer
  • The set of instructions that tells a computer
    12·2 answers
  • A bug was discovered in Canvas where the website crashes if 2 or more students are writing a discussion post at the same time. I
    10·1 answer
  • Which scenario shows the best example of intrinsic motivation?
    13·2 answers
  • Select each item that represents what the Internet may be used for:
    9·1 answer
  • For what reasons do readers use text-to-speech tools? Check all that apply.
    8·2 answers
  • For python how do I ask the user how many numbers they want to enter and then at the ending add those numbers up together?
    10·1 answer
  • What is denial service attack?
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!