0% found this document useful (0 votes)
54 views7 pages

09 - I3A - Algoritma Pemrograman - 41.19.0009 - Hanif Ardyansyah

The document contains 3 code examples demonstrating pointers, dynamic memory allocation, and classes in C++. The first example calculates the sum of elements in 2D arrays using pointers. The second example allocates 2D arrays dynamically. The third example defines a student class with attributes like name, ID, gender, scores etc and a method to calculate final grade.

Uploaded by

Hanif Ardyansyah
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)
54 views7 pages

09 - I3A - Algoritma Pemrograman - 41.19.0009 - Hanif Ardyansyah

The document contains 3 code examples demonstrating pointers, dynamic memory allocation, and classes in C++. The first example calculates the sum of elements in 2D arrays using pointers. The second example allocates 2D arrays dynamically. The third example defines a student class with attributes like name, ID, gender, scores etc and a method to calculate final grade.

Uploaded by

Hanif Ardyansyah
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/ 7

Nama : Hanif Ardyansyah

NPT : 41.19.0009
Kelas : Instrumentasi 3A

1. A. Pointer

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){
int A[2][2]={{2,3},
{1,5}};
int B[2][2]={{-1,2},
{4,7}};
int C, *pC = &C;

C =A[1][0] + B[1][1];

cout<<"Hasil Penjumlahan A[1][0] dan B[1][1] adalah "<< *pC;


}
B. Memory dinamis

#include <iostream>
using namespace std;
int main(){
//int A[2][2]={{2,3}, {1, 5}};
//int B[2][2]={{-1,2}, {4, 7}};

int *A[2];
A[0]= new int[2]{2, 3};
A[1]= new int[2]{1, 5};

int *B[2];
B[0]= new int[2]{-1, 2};
B[1]= new int[2]{4, 7};

int C = A[1][0] + B[1][1];


cout<<C;

delete A[0];
delete A[1];

delete B[0];
delete B[1];

return 0;

}
2. #include<iostream>
using namespace std;

class matrix{
public:
int arr[10][10];
};

int main(){
matrix A, B, C;

A.arr[0][0] = 2;
A.arr[0][1] = 3;
A.arr[1][0] = 1;
A.arr[1][1] = 5;

B.arr[0][0] = -1;
B.arr[0][1] = 2;
B.arr[1][0] = 4;
B.arr[1][1] = 7;

C.arr[0][0] = A.arr[0][0] + B.arr[0][0];


C.arr[0][1] = A.arr[0][1] + B.arr[0][1];
C.arr[1][0] = A.arr[1][0] + B.arr[1][0];
C.arr[1][1] = A.arr[1][1] + B.arr[1][1];

cout<<"Matrix C :"<<endl;

for(int i=0; i<2; i++){


for(int j=0; j<2; j++)
cout<<C.arr[i][j]<<' ';
cout<<endl;
}
}
3. #include <iostream>
#include <string>
using namespace std;

struct mahasiswa{
string nama;
string Nim;
string jenis_kelamin;
float tugas;
float uts;
float uas;
mahasiswa()
{
}
mahasiswa(string nm, string nim, string jk, float tg, float ut, float us)
{
nama = nm;
Nim = nim;
jenis_kelamin = jk;
tugas = tg;
uts = ut;
uas = us;

char getNilaiAkhir()
{
float nilai = 0.2*tugas + 0.4*uts + 0.4*uas;
if(nilai>=90)return 'A';
if(nilai>=70)return 'B';
if(nilai>=50)return 'C';
return 'D';
};

};

int main()
{
mahasiswa arr[5];

arr[0] = mahasiswa("SI_A", "189.01", "L", 90, 70, 80);


arr[1] = mahasiswa("SI_B" ,"189.02", "P", 85, 80, 70);
arr[2] = mahasiswa("SI_C", "189.03", "P", 95, 81, 100);
arr[3] = mahasiswa("SI_D" ,"189.04", "L", 90, 50, 70);
arr[4] = mahasiswa("SI_E" ,"189.05", "L", 95, 40, 90);

for(int i=0;i<5;i++)
{

cout<<arr[i].nama<<" "<<arr[i].Nim<<" "<<arr[i].jenis_kelamin<<"


NILAI AKHIR :"<<arr[i].getNilaiAkhir()<<endl;
}

You might also like