Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A Name the file lab2dynamic.cpp Write a new version of the program in Lab 2

ID: 3723715 • Letter: P

Question

Part A Name the file lab2dynamic.cpp Write a new version of the program in Lab 2 using pointers. main function will create the ar of radi and pass it to the function calcArea. calcArea will dynamically create a new array to store areas of circles and return a pointer to the new array to main function. main will then pass the pointers of the two arrays to printArrays to display both. Coding details: 1. main function: Declare an array of 10 ints called radi and initialize it with values multiples of 2. Define a pointer of type double to store the returned pointer. Call the function calcArea passing a pointer to radii array and the size. The returned pointer will be stored in the double pointer you defined. - - Call the function printArrays. 2. Function calcArea: accepts as it's arguments a pointer to the array radi and the size. The function will return a pointer of type double. The prototype of the function: double calcArea(int* Coded as follows: , int -Dynamically allocate an array of "doubles" using the received size. - Calculate the areas of the circles using radii array and store them in the created array. Return the pointer of the new array to main function. 3. Function printArrays will accept the pointers of the radi and areas array and prints their values

Explanation / Answer

For Part A:

/*
============================================================================
Name        : Covarience.c
Author      :
Version     :
Copyright   : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
//calculate the Area of circle
double* calcArea(int* num,int size){
   int i;
   double* areas=malloc(sizeof(double)*size);
   for(i=0;i<size;i++){
       areas[i]=3.141*num[i]*num[i];
   }
   return areas;
}
//print the array value
void printArrays(int* num,double* ptr,int size){
   int i;
   printf("Radius Area ");
   for(i=0;i<size;i++){
       printf("%d %f ",num[i],ptr[i]);
   }

}
int main(void) {
   printf("!!!Hello Programmer!!! ");
   int radii[10]={2,4,6,8,10,12,14,16,18,20};
   double* ptr;
   ptr=calcArea(radii,10);
   printArrays(radii,ptr,10);
   free(ptr);
   return EXIT_SUCCESS;
}


/*****************/output

!!!Hello Programmer!!!
Radius   Area
2   12.564000
4   50.256000
6   113.076000
8   201.024000
10   314.100000
12   452.304000
14   615.636000
16   804.096000
18   1017.684000
20   1256.400000


/***************************/

For part B

/*
============================================================================
Name        : Covarience.c
Author      :
Version     :
Copyright   : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
double getAverage(int* arr,int size){
   int i;
   double sum=0.0;
   for(i=0;i<size;i++)
       sum=sum+arr[i];
   return sum/size;
}
int getMedian(int* arr,int size){
   return arr[size/2];   //middle value of the sorted array is median
}
int getLargest(int* arr,int size){
   return arr[size-1];
}
int getSecondLargest(int* arr,int size){
   return arr[size-2];
}
void sort(int* arr,int size){
   int i,j;
   for(i=0;i<size;i++){
       for(j=i+1;j<size;j++){
           if(arr[i]>arr[j]){
               int tmp=arr[i];
               arr[i]=arr[j];
               arr[j]=tmp;
           }
       }
   }
}
int main(void) {
   int i;
   int n;
   int choice;
   printf("Enter number of values to process? ");
   scanf("%d",&n);
   while(n<=0){
       printf("Invalid Input! Enter number of values to process? ");
       scanf("%d",&n);
   }
   int* arr=(int*)malloc(sizeof(int)*n);
   printf("Enter the values ");
   for(i=0;i<n;i++)
       scanf("%d",&arr[i]);
   sort(arr,n);   //sort the array in ascending order
   while(1){
   printf("Enter choice: "
           "1. Average "
           "2. Median "
           "3. Largest "
           "4. Second Largest "
           "5. To Quit ");
   scanf("%d",&choice);
   switch(choice){
   case 1:
       printf("The average is: %f ",getAverage(arr,n));
       break;
   case 2:
       printf("The median is: %d ",getMedian(arr,n));
       break;
   case 3:
       printf("The Largest value: %d ",getLargest(arr,n));
       break;
   case 4:
       printf("The second largest value: %d ",getSecondLargest(arr,n));
       break;
   case 5:
       return EXIT_SUCCESS;
       break;
   default:
       printf("Invalid Choice! ");
       break;
   }
   }
   free(arr);
   return EXIT_SUCCESS;
}


/**************/output

Enter number of values to process? 0
Invalid Input! Enter number of values to process? 5
Enter the values 4 7 42 30 19
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
1
The average is: 20.400000
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
2
The median is: 19
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
3
The Largest value: 42
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
4
The second largest value: 30
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
8
Invalid Choice!
Enter choice:
1. Average
2. Median
3. Largest
4. Second Largest
5. To Quit
5