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
6. What is Race condition? How to prevent it? [2 marks] 7. How many synchronization methods do you know and compare the differences. [2 marks] 8. Explain what are the “mutual exclusion”, “deadlock”, “livelock”, and “eventual entry”, with the traffic intersection as an example like dinning philosophy. [2 marks] 9. For memory allocation, what are the difference between internal fragmentation and external fragmentation. Explain with an example. [2 marks] 10. How can the virtual memory map to the physical memory. Explain with an example. [2 marks]
Your answers normally have 50 words. Less than 50 words will not get marks. 1. What is context switch between multiple processes? [2 marks] 2. Draw the memory layout for a C program. [2 marks] 3. How many states does a process has? [2 marks] 4. Compare the non-preemptitve scheduling and preemptive scheduling. [2 marks] 5. Given 4 process and their arrival times and next CPU burst times, what are the average times and average Turnaround time, for different scheduling algorithms including: a. First Come, First-Served (FCFS) Scheduling [2 marks] b. Shortest-Job-First (SJF) Scheduling [2 marks] c. Shortest-remaining-time-first [2 marks] d. Priority Scheduling [2 marks] e. Round Robin (RR) [2 marks] Process Arrival Time Burst Time P1 0 8 P2 1 9 P3 3 2 P4 5 4
a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]

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,