0% found this document useful (0 votes)
3 views6 pages

High Performance Computing

The document contains a series of C++ code examples demonstrating various calculations such as Mean Absolute Error (MAE), array manipulation, power calculations, and statistical measures including mean, median, standard deviation, and variance. Each example includes the code, a brief description of its function, and the corresponding output. The assignment is authored by A.M.Y.S.U Perera with a focus on high-performance computing concepts.

Uploaded by

davidfrank10123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

High Performance Computing

The document contains a series of C++ code examples demonstrating various calculations such as Mean Absolute Error (MAE), array manipulation, power calculations, and statistical measures including mean, median, standard deviation, and variance. Each example includes the code, a brief description of its function, and the corresponding output. The assignment is authored by A.M.Y.S.U Perera with a focus on high-performance computing concepts.

Uploaded by

davidfrank10123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

High Performance Computing

Assignment 01

Name- A.M.Y.S.U Perera


Reg No- 2021s18929
Index No-16113
Question 01-
Example 03-

C++ code

#include <cstdio>
#include <iostream>
#include <math.h>

int main(int argc,const char * argv[]){


float Ytrue[] = {6.000,6.500,7.000,7.500,8.000,8.500,9.000,9.500};
float Yexp[] = {6.4935,6.9935,7.4935,7.9935,8.4935,8.9935,9.4935,9.9935};
float mae=0.0;
for(int i=0;i<8;i++){
mae=mae + fabs(Ytrue[i]-Yexp[i]);
}

mae=mae/8;
printf("MAE = %f \n",mae);

return 0;
}

Output-
MAE = 0.493500

Example 04-
C++ Code-
#include <cstdio>
#include <iostream>
#include <math.h>

int main(int argc,const char * argv[]) {


int arr[]={1,2,3,4,5,6,7,8,9};
for(int i=0;i<9;i++){
int a=arr[i];
if(fmod(a,2)==1){
arr[i]=a+1;
}
}
printf("New array is: {");
for(int i=0;i<9;i++){
printf("%d ",arr[i]);
}
printf("} \n");
return 0;

Output-
New array is: {2 2 4 4 6 6 8 8 10 }

Example 05-
C++ Code-
#include <cstdio>
#include <iostream>
#include <math.h>

int main(int argc,const char * argv[]){


double p[]={100,50,45,20,10};
double r[]={0.3,0.5,0.7,1.3,2.3};
double v;
for(int i=0;i<5;i++){
v=p[i]*r[i];
v=sqrt(v);
printf("%f ",v);
}

printf("\n");
return 0;
}

Output-
5.477226 5.000000 5.612486 5.099020 4.795832
Example 06-
C++ Code-
#include <cstdio>
#include <iostream>
#include <math.h>

int main(int argc,const char * argv[]){


double I[] = {0.045,0.0225,0.0114,0.0096,0.0081}; // Current I
double r[] ={100,200,300,400,500}; // Resistance
double p;
for (int i=0;i<5;i++){
p=pow(I[i],2)*r[i]; // Power p=i^2*r

printf("%f ",p);
}
printf("\n");
return 0;
}

Output-
0.202500 0.101250 0.038988 0.036864 0.032805
Question 02-
C++ Code-
#include <cstdio>
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
#include <vector>

int main(int argc,char * argv[]){


double stp[] =
{12.5,14.6,18.4,12.7,14.8,16.8,17.4,10.3,11.4,17.3,9.3,16.4,15.2,14.7};
// finding the mean
int sizeofarray=sizeof(stp)/sizeof(stp[0]);
double total=0;
for (int i=0;i<sizeofarray;i++){
total=total+stp[i];
}
double mean=total/sizeofarray;
printf("Mean of the data set: %f \n",mean);
// finding the median
double median=0;
int pos1=0;
int pos2=0;
// sort(stp,stp+sizeofarray);
for (int i = 0; i < sizeofarray - 1; i++) {
for (int j = 0; j < sizeofarray - i - 1; j++) {

if (stp[j] > stp[j + 1])


swap(stp[j],stp[j+1]);
}
}

if(fmod(sizeofarray,2)==1){
median=ceil(sizeofarray/2);
}else{
pos1= sizeofarray/2;
pos2=pos1-1;
median=(stp[pos1] + stp[pos2])/2;
}
printf("median is: %f \n",median);

// Standard deviation
double variance=0;
for(int i=0;i<sizeofarray;i++){
variance=variance+pow((mean-stp[i]),2);
}
variance=variance/sizeofarray;
double std=sqrt(variance);
printf("standard deviation is: %f \n",std);

// variance
printf("variance is: %f \n",variance);

// finding the maximum


double max=stp[sizeofarray-1];
printf("maximum is: %f \n",max);

// finding the minimum


double min=stp[0];
printf("The minimum is: %f \n",min);
return 0;
}

Output-
Mean of the data set: 14.414286
median is: 14.750000
standard deviation is: 2.704720
variance is: 7.315510
maximum is: 18.400000
The minimum is: 9.300000

You might also like