C How to Program (8th Edition)
C How to Program (8th Edition)
8th Edition
ISBN: 9780133976892
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
Textbook Question
Book Icon
Chapter 14, Problem 14.2E

(Variable-Length Argument List: Calculating Products) Write a program that calculates the product of a series of integers that are passed to function product using a variable-length argument list. Test your function with several calls, each with a different number of arguments.

Expert Solution & Answer
Check Mark
Program Plan Intro

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:

  C How to Program (8th Edition), Chapter 14, Problem 14.2E

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
05:38
Students have asked these similar questions
(Rounding Numbers) Function floor can be used to round a number to a specific decimal place. The statementy = floor(x * 10 + 0.5) / 10;rounds x to the tenths position (the first position to the right of the decimal point). The statementy                                        = floor(x * 100 + 0.5) / 100;rounds x to the hundredths position (the second position to the right of the decimal point). Write a program that defines fourfunctions to round a number x in various ways:A. roundToInteger(number)B. roundToTenths(number)C. roundToHundredths(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 hundredth and the number rounded to the nearest thousandth.
(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) roundTolnteger(number) b) roundToTenths(number) c) roundToHundreths(number) d) roundToThousandths(number) For each value %3D 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 PLEASE
(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 PLEASE

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
What part of the computer actually runs programs?

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

Describe the purpose of the access key attribute and how it supports accessibility.

Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning