Reference no: EM13763670
1: Complete the function: func in the following program. This function has three parameters: The first and second parameters are of type: int. The third one is a function that has two parameters of type: int and returns a value of type: int. In the body of function call its third parameter (which is a function) passing the first and second parameter of: func.
#include <stdio.h>
int pow(int m, int n){
int i, ans = 1;
for(i = 1; i <= n; i++)
ans = ans * m;
return ans;
}
int max(int m, int n){
if(m > n)
return m;
return n;
}
int min(int m, int n){
if(m < n)
return m;
return n;
}
//Complete the following function
void func (//Complete){
//Complete
}
void main (){
// This function is complete. Do not change it.
int x, y;
printf("Enter two positive integers: ");
scanf("%d%d", &x, &y);
func(x, y, pow);
func(x, y, max);
func(x, y, min);
}
Sample dialog:
Enter two positive integers: 2 3
8
3
2
Note: Your program should work for any positive integer.
2: Complete the function: total. This function has one or more parameters of type: int. This function returns the sum of all its parameters.
Note: Always we add argument zero as the last argument. This argument acts as a flag.
#include <stdio.h>
#include <stdarg.h>
int total(int arg, ...){
// Complete the body of this function. Do not change:
// int total(int arg, ...)
}
void main(){
// This function is complete. Do not change it.
int a, b, c, d, e;
printf("Enter 5 non-zero integers: \n");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
printf("%d\n", total(a, b, c, d, e, 0));
printf("%d\n", total(a, b, c, d, 0));
printf("%d\n", total(a, b, c, 0));
printf("%d\n", total(a, b, 0));
}
Sample dialog:
Enter 5 non-zero integers:
22 -2 30 5 -10
45
55
50
20
Note: Your program should work for any non-zero integer.
3: Complete the following program using linked list of nodes of type the struct below to simulate a stack of integers. In a stack an integer goes on the top and removed from the top of the stack.
Note:
Each node in the stuck (linked list) must be of type: Rec.
The variable: top must refer to top of the stack.
Use the variable: freeNode to either create a new node or to take a node from top of the stack.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
// This struct is complete. Do not change it.
int num;
struct Node *next;
} Rec;
void main(){
// Complete this function
Rec *top, *freeNode;
int x;
top = NULL;
}
Sample dialog:
Enter 1 to push a number, 2 to pop, and 3 to quit: 2
The stack is empty.
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: 33
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: -15
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: 45
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: -9
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: 88
Enter 1 to push a number, 2 to pop, 3 to quit: 2
88
Enter 1 to push a number, 2 to pop, 3 to quit: 2
-9
Enter 1 to push a number, 2 to pop, 3 to quit: 1
Enter an integer to push: 100
Enter 1 to push a number, 2 to pop, 3 to quit: 3
Note: Your program should work for any integer.
Below I show the stack for the above dialog sample.
Empty stack
After pushing: 33
After pushing: -15
After pushing: 45
After pushing: 45
After pushing: 88
After popping
After popping
After pushing:100