In: Computer Science
using c language:
create an array of the values of a sine wave. Include the math.h header to use the sin floating point function. The function sin takes an argument of radians (not degrees). Make your array721 elements and initialize each element with 10 * sin(2*3.1416* (i/360.0) where i is the array index from 0 - 720. When done properly, the array should contain approximately 2 complete sine wave cycles. Similarly, create an array of 721 elements only this time initialize it with a cosine function with amplitude 5 Have the program process and compute the following (in the order given); display the title of the action and answers on the screen: The maximum value of the cos and sin array added together. The mean value of the sin array The mean value of the element by element product of the sin and cos. The median value of the cos array. The dot product of the cos and sin array. The dot product of a reversed cos array with a sin array.
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 721
void sort(double *array , int size)
{
// declare some local variables
int i=0 , j=0;
double temp=0;
for(i=0 ; i<size ; i++)
{
for(j=0 ; j<size-1 ; j++)
{
if(array[j]>array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
double findMean(double arr[],int size){
double sum=0;
int i;
for(i=0;i<size;i++){
sum=sum+arr[i];
}
sum=sum/size;
return sum;
}
void printArray(double arr[],int size){
int i;
for(i=0;i<size;i++){
printf("%lf ",arr[i]);
}
}
double findMedian(double arr[],int size){
double median=0;
double* temp=arr;
sort(temp,size);
// if number of elements are even
if(size%2 == 0)
median = (temp[(size-1)/2] + temp[size/2])/2.0;
// if number of elements are odd
else
median = temp[size/2];
return median;
}
double findMax(double arr[],double size){
double max=arr[0];
int i;
for(i=0;i<size;i++){
if(max<arr[i])
max=arr[i];
}
return max;
}
double findDotProduct(double arr1[],double arr2[],double
size){
double product=0;
int i;
for(i=0;i<size;i++){
product=product+arr1[i]*arr2[i];
}
return product;
}
void main(){
double max[SIZE];
double cosArr[SIZE];
double sinArr[SIZE];
double elementByElementProductArr[SIZE];
double addedValues[SIZE];
double reversedCosArr[SIZE];
int i=0;
for(i=0;i<SIZE;i++){
sinArr[i]=(10 *
sin(2*3.1416*(i/360.0)));
cosArr[i]=(10 *
cos(2*3.1416*(i/360.0)));
addedValues[i]=sinArr[i]+cosArr[i];
elementByElementProductArr[i]=sinArr[i]*cosArr[i];
}
for(i=0;i<SIZE;i++){
reversedCosArr[i]=cosArr[SIZE-i-1];
}
//The maximum value of the cos and sin array added
together.
printf("The maximum value of the cos and sin array
added together : %lf \n",findMax(addedValues,SIZE));
//The mean value of the sin array
printf("The mean value of the sin array :
%lf\n",findMean(sinArr,SIZE));
//The mean value of the element by element product of
the sin and cos.
printf("The mean value of the element by element
product of the sin and cos. :
%lf\n",findMean(elementByElementProductArr,SIZE));
//The median value of the cos array.
printf("The median value of the cos array. :
%lf\n",findMedian(cosArr,SIZE));
//The dot product of the cos and sin array.
printf("The dot product of the cos and sin array. :
%lf\n",findDotProduct(cosArr,sinArr,SIZE));
//The dot product of a reversed cos array with a sin
array.
printf("The dot product of a reversed cos array with a
sin array. :
%lf\n",findDotProduct(reversedCosArr,sinArr,SIZE));
getch();
}