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
Answer the following.  The correct one in function if the code.
Is there a different way to write the same function?
What’s the output of the following programs?

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
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning