Write code and show result for each: 1) A function that reverses a list Example:
ID: 3536947 • Letter: W
Question
Write code and show result for each:
1) A function that reverses a list Example: ['a','b','c','d','e'] would be ['e','d','c','b','a']
2) Write a function that splits a list based on a number n [1,2,3,4,5,6,7] with n = 3 gives [1,2,3]
3) Write a function that removes all the numbers divisible by 3 in a list. Example [23,24,30,35,36,40,42,44,54] would yield [24,30,36,42, 54]
4) Write a function that takes a weight for a package and the distance it should be shipped and displays the charges for the package based on the following:
Weight per 500 miles shipped
2kg or less $1.10
Over 2kg but less than 6kg $2.20
Over 6kg but less than 10kg $3.40
Over 10Kg but not more than 20kg $4.60
5) Write a recursive Haskell function that finds the greatest common divisor of 2 values: eg. Gcd(10, 15) is 5
Explanation / Answer
#include<stdio.h>
#include<conio.h>
void function1(char *a,int n)
{
int i;
char temp;
for(i=0;i<n/2;i++)
{temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
printf(" Reversed elements: ");
for(i=0;i<n;i++)
printf("%c ",a[i]);
}
void function2(int *b,int n)
{
int i,c;
printf("Enter the limits:");
scanf("%d",&c);
printf(" Elements are: ");
for(i=0;i<c;i++)
printf("%d ",b[i]);
}
void function3(int *b,int n)
{
int i,j=0;
int *x;
x=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{if(b[i]%3==0)
x[j++]=b[i];
}
printf(" Elements are: ");
for(i=0;i<j;i++)
printf("%d ",x[i]);
}
void function4(float w,float d)
{
float total;
if(w<=2)
total=(d/500)*1.1;
else
{
if(w<6)
total=(d/500)*2.2;
else
{if(w<10)
total=(d/500)*3.4;
else
if(w<20)
total=(d/500)*4.6;
}}
printf(" Total cost: $%f",total);
}
int main()
{
int i,n,*b;
char *a;
printf("Question 1------ Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements: ");
a=(char *)malloc(sizeof(char)*n);
for(i=0;i<n;i++)
scanf("%s",&a[i]);
function1(a,n);
free(a);
printf(" Question2--------- Enter the number of Elements:");
scanf("%d",&n);
printf("Enter the elements: ");
b=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
function2(b,n);
free(b);
printf(" Question3--------- Enter the number of Elements:");
scanf("%d",&n);
printf("Enter the elements: ");
b=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
function3(b,n);
free(b);
printf(" Question4--------- Enter the number of Elements:");
scanf("%d",&n);
float w,d;
for(i=0;i<n;i++)
{
printf("Enter the weight(in kg) and distance(in miles) for package %d: ",i+1);
scanf("%f%f",&w,&d);
function4(w,d);
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.