In the questions below you are asked to write a function/procedure in Scheme to
ID: 3887982 • Letter: I
Question
In the questions below you are asked to write a function/procedure in Scheme to implement some behavior. To do so you may need to include auxillary functions/procedures. Include these with your answers. You may only use Scheme constructs and the EOPL extensions that have been presented in class. Put the answers to these questions in a single file. Tar or zip the file and submit the tar file he submission system. Put a comment header in the file with your name, course number, and date. 1. (10 points) Given a list of integers, write a function that returns the sum of these inte- gers. We define the sum of the empty list to be 0 (zero). length(lst) down to 1. For example Given: (10 20 30 40 50) 2. (10 points) Given a list of integers 1st, write a function that returns a list with entries Return: (5 432 1) The empty list returns the empty list. 3. (30 points) Consider the grammar that represents a stack on integers: () :(num stack) Non-empty stack stack : ;;Empty stack (a) (10 points) Create an abstract syntax for stack using the define-datatype macro. (b) (10 points) Write the following procedures for stack in Scheme where stack is represented by the abstract syntax of part (a): empty-stack?; determines whether a stack is empty top(stack) ;; returns the val on top of a stack report stack underflow error if stack is empty pop(stack) ;; returns the stack with the top value popped off ; report stack underflow error if stack is empty push(val stack);; returns the stack vith val pushed on top of stack Here is the error code (define report-stack-underflow (lambda ) (eopl:error 'empty-stack "Stack underflow")))Explanation / Answer
1) Below is the program in C (as you have not mention any language) for addition of given list of integers. Here int addition(int k, int arr[]) is the required funtion which returns addition of the numbers given by user. This function returns zero for zero numbers to be added.
#include<stdio.h>
#include<conio.h>
int addition(int k,int arr[]);
void main()
{
int array[100],n,add,i;
printf("How many numbers do you want to be added: ");
scanf("%d",&n);
if(n!=0)
{
printf("Enter %d numbers to be added: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}
}
add = addition(n,array);
printf("Addition of given %d numbers is: %d",n,add);
getch();
}
int addition(int k,int arr[])
{
int sum=0,j;
if(k==0)
return sum;
else
{
for(j=1;j<=k;j++)
{
sum=sum+arr[j];
}
return sum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.