Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must be coded using C: Assume there is a sequence of integers, [a,, a2, a3, ...)

ID: 3591785 • Letter: M

Question

Must be coded using C:

Assume there is a sequence of integers, [a,, a2, a3, ...), the pattern of this sequence is that an - an-1-Cn-1) and al = 1. Here are the first five elements in the sequence: a1 = 1, a,-1+ 1-2, a,-2 + 2 4.4-4 +3-7, a,-74 4-11 } . Please write a program to ask the user to input an integer (larger than 1) to be the position index for this sequence and calculate the value of that position in the sequence. Use recursion function (a function calling itself) to achieve this purpo . 2 se. Name your file Hw3_q2_code.c

Explanation / Answer

#include <stdio.h>

int getValue(int n){
    if (n == 1){
        // first element of the sequence a(1) = 1
        return 1;
    }

    else{
        // nth element of the sequence a(n) = a(n-1) + (n-1)
        return getValue(n-1)+(n-1);
    }
}

int main(){
    int idx;
    printf("Enter index value(>1):");
    scanf("%d", &idx);
    if (idx > 1){
        printf("value at pisition %d in the sequence is :%d ", idx, getValue(idx));
    }
    else{
        printf("index should be > 1 ");
    }

    return 0;
}