project_6 - Homework Market

advertisement
Project 6
Due Sunday 3/22/2015 by 11:59 PM
Do not delete anything from this file. Just write your
answer under the word: Answer.
Note the boldfaced numbers in the sample dialogs are the ones entered by the user.
1(30 points): 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.
Answer:
#include <stdio.h>
#include <conio.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 (int x1,int x2, int (*para)(int,int)){
int temp;
temp = (*para)(x1,x2);
printf("%d\n",temp);
}
void main (){
// This function is complete. Do not change it.
int x, y;
clrscr();
printf("Enter two positive integers: ");
scanf("%d%d", &x, &y);
func(x, y, pow);
func(x, y, max);
func(x, y, min);
getch();
}
2 (30 points): 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.
Answer:
#include<stdio.h>
#include<stdarg.h>
int total(int arg, ...)
{
int sum=0,i;
va_list ap;
va_start(ap,arg);
for(i=0;i<arg;i++)
{
sum+=va_arg(ap,int);
}
va_end(ap);
return sum;
}
void main()
{
int a,b,c,d,e;
clrscr();
printf("Enter 5 non-zero integers: \n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("%d\n",total(5,a,b,c,d,e));
printf("%d\n",total(4,a,b,c,d));
printf("%d\n",total(3,a,b,c));
printf("%d\n",total(2,a,b));
getch();
}
3 (40 points): 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
33
After pushing: 33
-15
33
After pushing: -15
45
-15
33
After pushing: 45
-9
45
-15
33
After pushing: 45
88
-9
45
-15
33
After pushing: 88
-9
45
-15
33
After popping
45
-15
33
After popping
100
45
-15
33
After pushing:100
ANSWER:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int num;
struct Node *next;
}Rec;
void main()
{
Rec *top, *freeNode;
int x, ch,i;
top=NULL;
clrscr();
do
{
printf("\nEnter 1 to push a number, 2 to pop, 3 to quit :");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(top==NULL)
{
printf("Enter an interger to push: ");
scanf("%d",&x);
freeNode=(Rec*)malloc(sizeof(Rec*));
freeNode->next=NULL;
freeNode->num=x;
top=freeNode;
}
else
{
printf("Enter an interger to push: ");
scanf("%d",&x);
freeNode=(Rec*)malloc(sizeof(Rec*));
freeNode->next=top;
freeNode->num=x;
top=freeNode;
}
}
break;
case 2:
{
if(top==NULL)
{
printf("The stack is Empty");
}
else
{
printf("Poped Value :%d ",top->num);
top=top->next;
free(top->next);
}
}
}
}while(ch!=3);
getch();
}
Answer:
Download