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
  • Fix this C++ below so does not use any loops at all. Use recursion instead. here...

    Fix this C++ below so does not use any loops at all. Use recursion instead. here is some guides given. Create a directory to hold assignment 3. Copy files hailstone.cpp, Makefile and dotestassignment 2 to the assignment 3 directory. Edit the comments at the top of hailstone.cppto say that this is assignment 3. You should be able to keep the same contracts, 'next' function and 'main' function from assignment 2, unless they contained errors that needed to be fixed. If...

  • 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...

  • Please help with this question (the two functions). I've attempted it but it's not working. We...

    Please help with this question (the two functions). I've attempted it but it's not working. We have to complete the 2 functions int largest(int * x); and void display(int *arr); (one prints the values in array and the other gets max value in array). But we can only use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the functions. C PROGRAMMING: ----------------- /* Passing an array to a function....

  • 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...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • 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...

  • 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 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...

  • 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...

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