0% found this document useful (0 votes)
25 views5 pages

A. Passing Actual Parameter by Value: Attachment

This C++ program uses pointers and references to calculate the linear regression solution (y=Ax+B) of sample data points and find the root of a polynomial equation. It takes user input for x and y values, calculates totals, averages, and the regression coefficients A and B. It also uses the Newton-Raphson method to iteratively find the root of an equation, taking an initial guess and tolerance as input. Pointers are demonstrated by printing array elements using subscript and pointer notation, and traversing a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

A. Passing Actual Parameter by Value: Attachment

This C++ program uses pointers and references to calculate the linear regression solution (y=Ax+B) of sample data points and find the root of a polynomial equation. It takes user input for x and y values, calculates totals, averages, and the regression coefficients A and B. It also uses the Newton-Raphson method to iteratively find the root of an equation, taking an initial guess and tolerance as input. Pointers are demonstrated by printing array elements using subscript and pointer notation, and traversing a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Attachment

listing program C++


Function
A. Passing Actual parameter by Value
#include <iostream>
using namespace std;
float A,B,y_rata2,y[7],x_kali_y[7],total_y=0,total_x_kali_y =0;
int i,m,x_rata2,x[7],x_kuadrat[7],total_x=0,total_x_kuadrat=0;
int sigma_x(int m);
float sigma_y(int m);
void Penjelasan_Program();
int main()
{
m = 7;
Penjelasan_Program();
return 0;
system("PAUSE");
}
void Penjelasan_Program()
{
cout<<"===================================================================
========"<<endl;
cout<<"Program Menghitung Solusi Regresi dari suatu Permasalahan Linier Y = AX + B"<<endl;
cout<<"Dengan menggunakan Metode Numerik "<<endl;
cout<<"===================================================================
========"<<endl;
cout<<endl<<endl;
cout<<"Masukkan Nilai-nilai x :"<<endl;
sigma_x(m);
cout<<endl;
cout<<"Masukkan Nilai-nilai y :"<<endl;
sigma_y(m);
cout<<endl;
cout<<"Tabulansi Data : "<<endl;
cout<<"|---------------------------------------|"<<endl;
cout<<"| x | y | x2 | x,y |"<<endl;
cout<<"|---------------------------------------|"<<endl;
for(int i =0; i<m;i++)
{
cout.setf(ios::left|ios::showpoint);
cout<<"|";
cout.width(9);
cout<<x[i];
cout.width(9);
cout<<y[i];
cout<<"|";
cout.width(9);
cout<<x_kuadrat[i];

cout<<"|";
cout.width(9);
cout<<x_kali_y[i];
cout<<"|";
cout<<endl;
}
cout<<"|--------------------------------------|"<<endl;
cout<<"|";
cout.width(7);
cout<<"sigma";
cout<<total_x;
cout<<"|";
cout.width(9);
cout<<total_y;
cout<<"|";
cout.width(9);
cout<<total_x_kuadrat;
cout<<"|";
cout.width(9);
cout<<total_x_kali_y;
cout<<"|";
cout<<endl;
cout<<"|--------------------------------------|"<<endl;
x_rata2 = total_x/m;
y_rata2=total_y/m;
A = ((m*total_x_kali_y) - (total_x*total_y))/((m*total_x_kuadrat)-(total_x*total_x));
B = y_rata2-A*x_rata2;
cout<<"Maka solusi persamaan liniernya adalah : "<<endl;
cout<<" A :"<<A<<endl;
cout<<" B :"<<B<<endl;
}
int sigma_x(int m)
{
for (int i = 0; i<m; i++)
{
cin>>x[i];
total_x = total_x + x[i];
x_kuadrat[i] = x[i]*x[i];
total_x_kuadrat = total_x_kuadrat + x_kuadrat[i];
}
return total_x;
return total_x_kuadrat;
}
float sigma_y(int m)
{
for (int i =0; i<m; i++)
{
cin>>y[i];
total_y = total_y + y[i];
x_kali_y[i]=x[i]*y[i];
total_x_kali_y = total_x_kali_y + x_kali_y[i];
}
return total_y;
return total_x_kali_y;

}
B. Passing Actual Parameter by reference
#include<iostream>
#include<cmath>
using namespace std;
void X_baru(float&x)
{
x = x-((x*x*x + x*x - 3*x-3)/(3*x*x+2*x-3));
}
int main ()
{
float galat,e,a,as;
cout<<"|----------------------------------------|"<<endl;
cout<<" Program Mencari Akar Persamaan dengan "<<endl;
cout<<" Metodr Newton - Rhapson Y = X^3+X^2-3X-3"<<endl;
cout<<"|----------------------------------------|"<<endl;
cout<<endl<<endl;
cout<<"Masukkan nilai awal (Xo) = 2"<<endl;
cin>>a;
cout<<endl;
cout<<"Masukkan bataas toleransi e = 0,00000001"<<endl;
cin>>e;
cout<<endl;
cout<<"|------------|"<<endl;
cout<<"| i | Xi |"<<endl;

cout<<"|------------|"<<endl;

do {

static i = 1;

as = a;

X_baru(a);

galat = fabs(a-as);

cout.setf(ios::left|ios::showpoint);
cout<<"|";

cout.width(3);

cout<<i;

cout<<"|";

cout.width(9);

cout<<a;

cout<<"|";

i=i+1;

cout<<endl;}

while (galat>e);

cout<<"|------------|"<<endl<<endl;

cout<<"Maka solusi akar persamaan adalah :"<<a<<endl;

return 0;

Pointer
#include<iostream>
using namespace std;
void main()
{
double array[6] = {11.11,22.2,33.3,44.4,55.5,66.6};
char *kata = "pemograman komputer\n";
char k;
int ctr;
cout<<"here is the array using subscropts:"<<endl;

for (ctr = 0;ctr<6;ctr++)


{
cout<<array[ctr]<<"";
}
cout<<endl;
cout<<"here is the array using pointer:"<<endl;
for (ctr = 0;ctr<6;ctr++)
{
cout<<*(array + ctr)<<"";
}
cout<<endl;
cout<<endl;
cout<<endl;
cout<<"here is the array using pointer string :"<<endl;
cout<<kata;
ctr=0;
while (k != '\n')
{
k = *(kata + ctr);
cout<<k<<"";
ctr +=1;
}
cout<<endl;
}

You might also like