(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
C How to Program (8th Edition)
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
- (C++ language)arrow_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(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 function that receives an integer. The function must return a string containing thehexadecimal representation of the integer.arrow_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_forward4. (Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write a function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and prints all the prime numbers between 1 and 1000. Display 10 numbers per line.arrow_forward
- (Use Python) Use the Design Recipe to write a function called which_day that consumes an int representing the number of days that have passed since Friday and returns name of the current day of the week. For example, if 2 days have passed, then ‘Sunday’ should be returned. Include a docstring! For example: Test Result 2 Sunday 4 Tuesday 6 Thursday 8 Saturday 10 Monday Write 3 assert_equal statements to test your function.arrow_forward(Perfect Numbers) An integer number is said to be a perfect number if its factors, including1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 =1 + 2 + 3. Write a function isPerfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.Challenge the power of your computer by testing numbers much larger than 1000.arrow_forward(python) Speed of Sound is a constant 343 m/s Create a function that will return the Frequency Heard based on the above equation. The function should accept the following parameters: speed of sound, velocity of receiver, velocity of source and frequency of source. Make sure to prompt the user for the input data and call the function. Example Values/Results: Frequency of Source: 935 HzVelocity of receiver: 24 m/sVelocity of source 43 m/sHeard frequency is 888.977 Hzarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning