(Variable-Length Argument List: Calculating Products) Write a
Program Plan:
This header file <stdarg.h>is used for object ap of type va_list is used by macros va_startva_arg and va_end to process the variable-length argument list of function product.
product( int x, ) :This function definition product is using a variable length argument list and calculate the product of a series of integers.
Printf (): used to print the data onto output screen.
Variables i, j, l, m, and n are of integer type which are passed as argument to function.
Total variable of integer type is used to store product of integer value.
Program Description: Purpose of the program is to calculates the series of integers that are passed to function product with several calls, by using a variable length list of arguments list and display result.
Explanation of Solution
Program: Following is Cprogram that calculates the series of integers that are passed to function product with several calls, by using a variable length list of arguments list and display result.
#include<stdio.h>//header file #include<stdarg.h>///header-file for variable-length argument lists //Function prototype int product( int x, ... ); //Start of main intmain( void ) { //Initialize the integers inti = 5; int j = 4; int k = 3; int l = 2; int m = 1; //display user the values of integers printf( "%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n","i = ", i, "j = ", j,"k = ", k,"l = ", l, "m = ", m ); //display the result of product using the function call to different series of integers printf( "%s%d\n%s%d\n%s%d\n%s%d\n", "The Product of i and j is: ", product( 2,i,j ), "The Product of i, j and k is: ",product(3,i,j,k ), "The Product of i, j, k and 1 is: ", product(4,i,j,k,l ), "The Product of i, j, k, 1, and m is: ",product(5,i,j,k,l,m) ); //terminate program succesfully return0; }//End of main //function product in which product of integers is passed as arguments int product( int x, ... ) { //Declare and initialize variable total int total = 1; //variable to counter loop intz; //Stores information needed by va_start and va_end. /*initialize variable Length argument List*/ va_listarg; //Stores information needed by va_start. /*invoke the macros to access the arguments*/ va_start( arg, x ); //Process is running for variable length argument list. /*evaluate the total using for loop*/ for( z = 1; z <= x ; z++ ) { total *= va_arg( arg, int); }/*end of for loop*/ //Clean up variable-length argument list. /*Perform the housekeeping termination*/ va_end( arg); /*return the arguments of product*/ returntotal; }/*end of function Product*/
Explanation:
- Use header file notation
for variable-length list of arguments. - Read different series of integers in function call.
- Assign the value of total equals to 1 and use the argument list including va_list of variable list, next initialize va_start ( ) to invoke the macros so as to access the arguments in function definition.
- Loop for to evaluate the value of product.
- Display the product of a series of integers.
Sample Output:
Want to see more full solutions like this?
Chapter 14 Solutions
MYPROGRAMMINGLAB WITH PEARSON ETEXT
Additional Engineering Textbook Solutions
Starting Out with Python (3rd Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Database Concepts (7th Edition)
Starting Out with Java: Early Objects (6th Edition)
- (Rounding Numbers) Function floor may be used to round a number to a specific decimalplace. The statementy = floor(x * 10 + .5) / 10;rounds x to the tenths position (the first position to the right of the decimal point). The statementy = floor(x * 100 + .5) / 100;rounds x to the hundredths position (the second position to the right of the decimal point). Writea program that defines four functions to round a number x in various waysa) roundToInteger(number)b) roundToTenths(number)c) roundToHundreths(number)d) roundToThousandths(number)For each value read, your program should print the original value, the number rounded to thenearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth, and the number rounded to the nearest thousandth.arrow_forward(Count the letters in a string) Write a function that counts the number of letters in a string using the following header: def countLetters(s) : Write a test program that prompts the user to enter a string and displays the number of letters in the string. the answer should be in python.arrow_forward(Rounding Numbers) An application of function floor is rounding a value to the nearestinteger. The statementy = floor(x + .5);will round the number x to the nearest integer and assign the result to y. Write a program that readsseveral numbers and uses the preceding statement to round each of these numbers to the nearestinteger. For each number processed, print both the original number and the rounded number.arrow_forward
- (Fraction calculator) | Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which _a_ and _b_ are integers and b ≠ 0. Your program must be menu driven, allowing the user to select the operation (+, –, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions: Function menu: This function informs the user about the program’s purpose, explains how to enter data, and allows the user to select the operation. Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.) Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the…arrow_forward(Rounding Numbers) Function floor may be used to round a number to a specific decimal place. The statement y = floor(x * 10 + .5) / 10; rounds x to the tenths position (the first position to the right of the decimal point). The statement y = floor(x * 100 + .5) / 100; rounds x to the hundredths position (the second position to the right of the decimal point). Write a program that defines four functions to round a number x in various ways a) roundToInteger(number) b) roundToTenths(number) c) roundToHundreths(number) d) roundToThousandths(number) For each value read, your program should print the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hun- dredth, and the number rounded to the nearest thousandth. IN C PROGRAMMING LANGUAGE PLEASEarrow_forward(Written in C) Create a payroll program to store and calculate the payroll for a small company as follows:Create an array of floats that is 4 rows and 50 columns, with the columns being the number of employeerecords stored in the array.Program parameters are as follows:Create a menu in a function menu and call it with the following options (use a do-while loop):A or a to add employee infoD or d to display employee infoT or t to display total payrollS or s to display the info of all employeesC or c to display the count of employees present in the arrayF or F to delete a recordZ or z to exit programThe information for each employee is: employee number, hours worked, pay rate per hour, taxdeduction.Option A or a:Call the function possible to check and if the index returned is 50 then output a msg saying that thearray is full. If the index returned is less than 50 the call the function add, pass to it the available index asthe third argument and the function will ask the user for one…arrow_forward
- (Python)Write a function that receives an integer. The function must return a string containing thehexadecimal representation of the integer.arrow_forward(Use Python) The function course_average should calculate and return the average of the three values pass to it. The function main should ask the user to enter three grades and then pass these values to the course_average function. This function should also display a message to the user in the format below. For example, if the user entered 100, 90 and 95 the message would be:The average of 100 , 90 and 95 is 95arrow_forward(PYTHON) Write a program with a function that takes a string; if the length of a string is even, the functionshould add an asterisk at the beginning of a string; else, the function should add asterisks at the end ofa string. The function, then, capitalizes all letters and returns the new string The program should let the user enter a string and pass it to the function.arrow_forward
- (Time in Seconds) Write a function that takes the time as three integer arguments (forhours, minutes, and seconds) and returns the number of seconds since the last time the clock “struck12.” Use this function to calculate the amount of time in seconds between two times, both of whichare within one 12-hour cycle of the clockarrow_forward(Distance Between Points) Write a function distance that calculates the distance betweentwo points (x1, y1) and (x2, y2). All numbers and return values should be of type double.arrow_forward(Intro to Python) Write complete python program that defines and calls a function isEvenNumber which takes one parameter and prints whether the number is even or odd. Prompt the user to enter a number and call the function passing the number as an argument to generate the following output. Zero can be classified as even. Sample output is: Enter a number: -5 -5 is odd number or Enter a number: 12 12 is even numberarrow_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