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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,