
Concept explainers
Question #2:
Study the following C
Hint: you might run it in Dev C++ before starting answering the questions!
- Show a sample output of the program.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Extract a function declaration (prototype) for:
- void functions with void argument
………………………………………………………………………………………………………………… |
- void function with a single input argument
………………………………………………………………………………………………………………… |
- function with a single input argument and return
………………………………………………………………………………………………………………… |
- a recursive function
………………………………………………………………………………………………………………… |
- What is the difference between normal functions and recursive functions?
|
|
|
- How many times are the function print_stars() called in the program? (mention the line number of each call).
|
|
|
#include<stdio.h>
void print_stars();
void option_list();
void divisors(int n);
int summation(int s);
int factorial (int n) ;
int main(void) {
int number , result , op ;
char flag;
printf("Enter an integer number > ");
scanf( "%d" , &number );
do{
option_list();
scanf("%d" , &op);
switch(op) {
case 1:
divisors(number);
break;
case 2:
result = summation(number) ;
printf("Summation = %d\n" , result);
break;
case 3:
result = factorial(number) ;
printf("Factorial = %d\n" , result);
break;
default:
printf("\nERROR: %d is unrecognized option!!! \n" , op);
}
print_stars();
printf("Do you want to check another function? (Y/N)> ");
scanf(" %c" , &flag);
} while (flag == 'Y' || flag == 'y');
return 0;
}
void option_list(){
print_stars();
printf("1: Find the divisors \n");
printf("2: Compute the summuation\n");
printf("3: Compute the factorial \n");
print_stars();
printf("Please enter your choice: ");
}
void print_stars(){
int i;
for(i = 1 ; i <= 25 ; i++)
printf("*");
printf("\n");
}
void divisors(int n) {
int i;
printf("Divisors of %d are\n",n);
for(i=1;i<=n;i++)
if(n%i==0)
printf(" %d",i);
printf("\n");
}
int factorial (int n){
int i , fact = 1 ;
for (i = 2 ; i <= n ; i++)
fact *= i;
return fact;
}
int summation(int s){
if (s == 0)
return 0;
else
return s + summation(s-1);
}
- What does each function do?
Hint: Write a simple description for each of the user-defined functions.
void print_stars(): |
|
|
|
void option_list() |
|
|
|
void divisors(int n) |
Example: |
This function takes one argument of type int and prints its divisors on the screen, including, 1 and itself. |
|
int summation(int s) |
|
|
|
|
|
int factorial (int n) |
|
|
|
|
|
|
|
|

Step by stepSolved in 2 steps

- Implement in C Programming 7.6.1: LAB: Simple car Given two integers that represent the miles to drive forward and the miles to drive in reverse as user inputs, create a SimpleCar variable that performs the following operations: Drives input number of miles forward Drives input number of miles in reverse Honks the horn Reports car status SimpleCar.h contains the struct definition and related function declarations. SimpleCar.c contains related function definitions. Ex: If the input is: 100 4 the output is: beep beep Car has driven: 96 miles main.c #include <stdio.h> #include "SimpleCar.h" int main() { /* Type your code here. */ return 0;} SimpleCar.h #ifndef SIMPLE_CAR_H#define SIMPLE_CAR_H typedef struct SimpleCar_struct { int miles;} SimpleCar; SimpleCar InitCar();SimpleCar Drive(int dist, SimpleCar car);SimpleCar Reverse(int dist, SimpleCar car);int GetOdometer(SimpleCar car);void HonkHorn(SimpleCar car);void Report(SimpleCar car); #endif SimpleCar.c #include…arrow_forwardUsing C++ Using your own creativity, make a set of function templates that have these features: This function must return a value. A function template with 1 template parameter, T. And, any other parameters you want. A function template with 2 template parameters, T1 and T2. And, any other parameters you want Within main (): Call your template functions and demonstrate how they work. Construct objects and show off your amazing programming skills!arrow_forwardConsider the following function definition.void mystery(int* x){// function body } Inside the definition of mystery, the C++ language provides a way to distinguish if x points to a lone int value or to an int allocated as part of a larger array.A. True B. Falsearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





