Lab 8.1 Define 6 functions for the following tasks (a through e) using pointer p
ID: 3841287 • Letter: L
Question
Lab 8.1 Define 6 functions for the following tasks (a through e) using pointer parameters. In the main function, Create an array of int that can store 20 numbers. Test your functions in the main.
Input 20 numbers to the array
Output the array and the address of each element.
Find the average of the array
Find and output the largest element and its index
Find and output the smallest element and its index.
Lab 8.2 Define an int function that takes 6 parameters. 2 of them, a and b, are of type int; 4 other parameters are pointers to int. The function returns a mod b (a%b); The 4 pointers point to the sum, the difference, the product, and the quotient of a and b, respectively. In the main function, enter 2 numbers and call your function to find the 5 results (sum, difference, product, quotient, and remainder). Note, you must bring the results to the main to print.
I need the programs to be in C code, if possible can you provide a screen shot of the solution running please.
Lab 8.1 Define 6 functions for the following tasks (a through e) using pointer parameters. In the main function, Create an array of int that can store 20 numbers. Test your functions in the main.
Input 20 numbers to the array
Output the array and the address of each element.
Find the average of the array
Find and output the largest element and its index
Find and output the smallest element and its index.
Lab 8.2 Define an int function that takes 6 parameters. 2 of them, a and b, are of type int; 4 other parameters are pointers to int. The function returns a mod b (a%b); The 4 pointers point to the sum, the difference, the product, and the quotient of a and b, respectively. In the main function, enter 2 numbers and call your function to find the 5 results (sum, difference, product, quotient, and remainder). Note, you must bring the results to the main to print.
Explanation / Answer
Lab 8.1
#include <stdio.h>
#include <stdlib.h>
void getInput(int num[]);
void printArray(int num[]);
double findAverage(int num[]);
int findMax(int num[]);
int findMin(int num[]);
int main(int argc, char const *argv[])
{
int numbers[20];
getInput(numbers);
printArray(numbers);
printf("Average : %f ",findAverage(numbers));
printf("Max : %d ",findMax(numbers));
printf("Min : %d ",findMin(numbers));
}
void getInput(int num[]){
int i;
for(i=0; i<20; i++){
printf("Enter Number[%d] :",(i+1));
scanf("%d",&num[i]);
}
}
void printArray(int num[]){
int i;
for(i=0; i<20; i++){
printf("Element[%d]: %d , Address %d ",(i+1),num[i],&num[i]);
}
}
double findAverage(int num[]){
int i,sum=0;
for(i=0; i<20; i++){
sum += num[i];
}
return (double)sum/20.0;
}
int findMax(int num[]){
int i,max=0;
for(i=0; i<20; i++){
if(num[i]>max){
max = num[i];
}
}
return max;
}
int findMin(int num[]){
int i,min=9999999;
for(i=0; i<20; i++){
if(num[i]<min){
min = num[i];
}
}
return min;
}
Lab 8.2:
#include <stdio.h>
#include <stdlib.h>
int arthemetic(int a,int b, int *add,int *sub,int *mul, int *div );
int main(int argc, char const *argv[])
{
printf("Enter Number a b :");
int a,b;
int p,q,r,s;
scanf(" %d %d",&a,&b);
int z = arthemetic(a,b,&p,&q,&r,&s);
printf("a =%d ",a);
printf("b =%d ",b);
printf("Add =%d ",p);
printf("Sub =%d ",q);
printf("Mul =%d ",r);
printf("Div =%d ",s);
printf("Mod =%d ",z);
}
int arthemetic(int a,int b, int *add,int *sub,int *mul, int *div ){
*add = a+b;
*sub = a-b;
*mul = a*b;
*div = a/b;
return a%b;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.