Question

Need help in my C language assignment... all I need to do is

1.Use recursion instead

2. NO LOOPS ALLOWED

Output _______________________________________

What number shall I start with?  16
  sequence: 16 8 4 2 1
  length: 5
  largest: 16

  For start values from 1 to 16:

  longest: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
  length: 20

  containing largest: 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
  largest: 160

_____________________________________________________________

// Creates the next input in a sequence int next(int n) int x = n; if (x % 2 == 0) x = x/ 2; return x; else + 1; x = 3 * x re

// Prints the whole sequence for your input void PrintSequence(int n) for(int x = n; x > 1; x = next(x)) printf(%i, x); pri

// Creates a count for the sequence Hint length(int n) int count = 1; int x = n; while(x > 1) X = next(); count++; return cou

// Creates the largest number in the first sequence int largest(int n) int max = n; int X = n; while(x != 1) if(max <= x) max

I'll write the another functions after I get an idea on what to do

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
int next(int n)
{
    int x = n;
    if(x%2 == 0)
    {
        x = x/2;
        return x;
    }
    else
    {
        x = 3 * x + 1;
        return x;
    }
}
void PrintSequence(int n)
{
    int x = n;
    if(x <=1)
    {
        printf("1");
        return 0;
    }
    printf("%i ",x);
    x = next(x);
    //recursive calling of PrintSequence
    //This will print the number every which will return by next(x)
    PrintSequence(x);
   
}
int length(int n)
{
   
    static int count = 1;
    int x = n;
    if(x<=1)
    {
        return 0;
    }
    x = next(x);
    count++;
    //recursive calling of function length()
    length(x);
    return count;
}
int largest(int n)
{
    //static varibale not intialized again once it executed
    static int max = 0;
    int x = n;
    if(x<=1)
    {
        return 0;
    }
    if(max<=x)
    {
        max = x;
    }
    x = next(x);
    //recursive calling of function largest
    largest(x);
    return max;
}
int main()
{
    int n,count=0,max = 0;
    printf("Enter n value: ");
    scanf("%d",&n);
    printf("Sequence: ");
    PrintSequence(n);
    count = length(n);
    printf("\nLength: %d\n",count);
    max = largest(n);
    printf("Largest: %d",max);
    return 0;
}

#include<stdio.h> 2 int next(int n) int x = n; if(x%2 == 0) X = x/2; return x; else + 1; X = 3 * x return x; 15 } 16 void Pri//recursive calling of PrintSequence 27 | //This will print the number every which will return by next(x) 28 PrintSequence(x)int x = n; if(x<=1) return 0; if(max<=x) max = x; X = next(x); //recursive calling of function largest largest(x); return maxEnter n value: 15 Sequence: 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Length: 18 Largest: 160Enter n value: 9 Sequence: 9 28 14 7 22 11 34 17 52 26 134 0 20 10 5_16 8 4 2 1 Length: 20 Largest: 52Enter n value: 16 Sequence: 16 8 4 2 1 Length: 5 Largest: 16

Add a comment
Know the answer?
Add Answer to:
Need help in my C language assignment... all I need to do is 1.Use recursion instead...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • the coding language is just the basic c language and here is my first attempt at...

    the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...

  • My C program is supposed to make a random key the same length of clear_text string...

    My C program is supposed to make a random key the same length of clear_text string and then use the random key to encrypt clear_text, but I can’t get it to work. A wΝΗ 1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 char* make_rand_key(int length, char* key); 5 void encrypt(); 7- int main() { 8 encrypt(); 9 } 10 char* make_rand_key(int length, char* key){ int range=78; int max=48; char c='a'; int i; 'srand(time(NULL)); for(i=0;i<length;i++){ C=rand()%range_max; key[i]=c; key[i]='\0'; return key; 23...

  • Programming language: C NOT c++ Problem 2 (20 points) Both the for loop and the do-while...

    Programming language: C NOT c++ Problem 2 (20 points) Both the for loop and the do-while loop can be transformed into a simple while loop. For each of the following examples a) and b), write equivalent code using a while loop instead. double rand_double)( double ret - (double)rand(); return ret/ (RAND MAX 1); int factorial (int n) int for i, ret =1; (i=2; i <= ret *= 1; n; i++) int sample_geometric_rv(double p)l double q return ret; int ne; do...

  • any help! my program not working , there is an error but cannot solve it i...

    any help! my program not working , there is an error but cannot solve it i did this program after editing a previous program  and i tried to follow the following ; 1. Instead of printing the prime numbers from 2 to n, your program will print the first n prime numbers. 2. It will be an error if n is less than 1. Exampl: $ ./a 1 2 3 #include<stdio.h> 4 int p; // p is the global variable. 5...

  • In C language 5. What is the OUTPUT of each of these programs? If you aren't...

    In C language 5. What is the OUTPUT of each of these programs? If you aren't confident of an answer, type mpile and run the program to test it. (a) <stdio.h> #include int main main int count; int sum0; for (count 1; count10; count++) sum sum+ count; for count printf("sum= %d\n", return 0; sum); )main (b) #include int main ) <stdio.h> main/ int count; int sum -0 for (count 1 count10; count 2) sum sum + count; for count print...

  • Please use induction Consider the following code fragment: int i -1; int s=1; while (i <-...

    Please use induction Consider the following code fragment: int i -1; int s=1; while (i <- n) Show that at the start of every iteration holds using induction

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • What is the output of the following? int sum=3, z=1; do { printf("The sum:%d*", sum+z); }...

    What is the output of the following? int sum=3, z=1; do { printf("The sum:%d*", sum+z); } while (sum<= 1); printf("0"); * 1 (2 Points) 40 4

  • Question 9 Each repetition in a loop is referred to as a(n) statement steration selection condition...

    Question 9 Each repetition in a loop is referred to as a(n) statement steration selection condition Question 10 In the following statements, the printf statement will be executed times Count 10; while (count <= 10) printf ("td , Count) 1 an infinite number of o 10 Question 11 The statement that terminates a loop and continues with the statement immediately following the loop is O break continue goto Ocomma recursion Question 12 The statement always creates a post-test loop. repeat...until...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT