Question

**IN C***

*In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab

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

Source Code:

#include<stdio.h> /*main function*/ int main(void) { /*variable*/ int n, i, a[100], sum=0, oddcount; /*function prototypes*/sum=calculateSum(a, n-1); /*print sum*/ printf(\nThe sum of the elements is: %d, sum); oddcount=countOdds(a,n-1); /*print oprintf(Beginning of array); /*function definition*/ int calculateSum(int a[], int n) { /*check for valid index*/ if(n>=0) {{ /*check for odd number*/ if(a[n]%2!=0) { c=1; /*recursive call*/ return c+countOdds(a, n-1); } /*if even then call recursiv

Output:

C:\Users\Kumar Reddi\Desktop\trail.exe Enter the size of the array: 5 Enter the elements: 35 25 20 15 10 array in reverse ord

Code in text format (See above images of code for indentation)

#include<stdio.h>
/*main function*/
int main(void)
{
   /*variable*/
   int n,i,a[100],sum=0,oddcount;
   /*function prototypes*/
   void printReverse(int[],int);
   int calculateSum(int[],int);
   int countOdds(int[],int);
   /*read size of the array from the user*/
   printf("Enter the size of the array: ");
   scanf("%d",&n);
   /*read elements of array from the user*/
   printf("Enter the elements:\n");
   for(i=0;i<n;i++)
       scanf("%d",&a[i]);
   /*function calls*/
   printf("array in reverse order:\n");
   printReverse(a,n-1);
   sum=calculateSum(a,n-1);
   /*print sum*/
   printf("\nThe sum of the elements is: %d",sum);
   oddcount=countOdds(a,n-1);
   /*print oddCount*/
   printf("\nThe count of odd numbers is: %d",oddcount);
   return 0;
}
/*function definition*/
void printReverse(int a[],int n)
{
   /*check for valid index*/
   if(n>=0)
   {
       /*print element*/
       printf("%d: %d\n",n,a[n]);
       /*recursive call*/
       printReverse(a,n-1);
   }
   else
       printf("Beginning of array");
}
/*function definition*/
int calculateSum(int a[],int n)
{
   /*check for valid index*/
   if(n>=0)
   {
       /*calculate sum and recursive call*/
       return a[n]+calculateSum(a,n-1);
   }
   else
       return 0;
}
/*function definition*/
int countOdds(int a[],int n)
{
   int c=0;
   /*check for valid index*/
   if(n>=0)
   {
       /*check for odd number*/
       if(a[n]%2!=0)
       {
           c=1;
           /*recursive call*/
           return c+countOdds(a,n-1);
       }
       /*if even then call recursively*/
       else
           countOdds(a,n-1);
   }
   else
       return 0;
}

Add a comment
Know the answer?
Add Answer to:
**IN C*** * In this lab, you will write a program with three recursive functions you...
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
  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2...

    Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2 so that it is modular. Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a subroutine called print_array that accepts an integer array and prints it out in this format: The array is [01 2 3456 7 8 9] Use this function...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters...

    USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters printed should be in White ► Call the first function from main().Print "I'm in main" (as shown in example) > Print "I'm in function 1" in the first function. Call the second function. ► Print "I'm in function 2” in the second function and then ask the user to enter a float value. Enter 25 Call a third function and pass the value to...

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