0% found this document useful (0 votes)
21 views

Programming Example

This document demonstrates the use of 2D arrays in C++ to store and process student mark data. It defines functions to input mark data, calculate grades, output results, and find highest and lowest marks. It also shows an example using structures to store student name and mark data and determine the best student.

Uploaded by

siti nur rahmah
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)
21 views

Programming Example

This document demonstrates the use of 2D arrays in C++ to store and process student mark data. It defines functions to input mark data, calculate grades, output results, and find highest and lowest marks. It also shows an example using structures to store student name and mark data and determine the best student.

Uploaded by

siti nur rahmah
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/ 11

2D ARRAY EXAMPLE

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int baris = 3;
const int lajur = 3;
string nama[] = { "Pelajar 1","Pelajar 2", "Pelajar 3","Pelajar 4" ,"Pelajar 5" };
string subjek[] = { "PR2","WEB","SAD" };

void inputA(float a[][lajur])


{
for (int i = 0; i < baris; i++)
{
cout << " Masukkan markah untuk " << nama[i] << " bagi :" << endl;
for (int j = 0; j < lajur; j++)
{
cout << subjek[j] << " : ";
cin >> a[i][j];
while (a[i][j] < 0 || a[i][j] > 100)
{
cout << " Error !!, Enter Again " << endl;
cout << subjek[j] << " : ";
cin >> a[i][j];

}}}}
void displayline(int a)
{
for (int i = 0; i < a; i++)
{
cout << "-";
}
cout << endl;
}
void paparGred(float& markah, float& jumlah, float& jum, float& A, float& A1, float& B, float& C, float& D, float& E)
{
float cg = 0;
jumlah += markah;
if (markah >= 0 && markah <= 39)
{
cout << " E ";
cg = 1.0;
E += 1;
}
else if (markah >= 40 && markah <= 46)
{
cout << " D ";
cg = 1.7;
D += 1;
}
else if (markah >= 47 && markah <= 59)
{
cout << " C ";
cg = 2.3;
C += 1;
}
else if (markah >= 60 && markah <= 74)
{
cout << " B ";
cg = 3.3;
B += 1;
}
else if (markah >= 75 && markah <= 100)
{
cout << " A ";
cg = 4.0;
A++;
A1 += 1;
}
jum += cg;
}

void outputA(float a[][lajur])


{
float A = 0;
float A1[baris];
float bcde[4];
float total[baris];
A1[0] = 0; A1[1] = 0; A1[2] = 0;
cout << "\t\t\t";
for (int j = 0; j < lajur; j++)
{
cout << setw(10) << subjek[j];
}
cout << setw(10) << "Jumlah";
cout << setw(13) << "Purata";
cout << setw(10) << "GPA";
cout << setw(27) << "Status Akademik";
cout << setw(27) << "Jumlah Grade";
cout << endl;
cout << "\t\t\t";
displayline(151);

for (int i = 0; i < baris; i++)


{
A = 0;

bcde[0] = 0; bcde[1] = 0; bcde[2] = 0; bcde[3] = 0;


total[0] = 0; total[1] = 0; total[2] = 0;
cout << nama[i] << " : \t\t|";
for (int j = 0; j < lajur; j++)
{
if (j == 0)
{
cout << setw(5) << a[i][j];
paparGred(a[i][j], total[0], total[1], A1[0], A, bcde[0], bcde[1], bcde[2], bcde[3]);
cout << " | ";
}
if (j == 1)
{
cout << setw(5) << a[i][j];
paparGred(a[i][j], total[0], total[1], A1[1], A, bcde[0], bcde[1], bcde[2], bcde[3]);
cout << " | ";
}
if (j == 2)
{
cout << setw(5) << a[i][j];
paparGred(a[i][j], total[0], total[1], A1[2], A, bcde[0], bcde[1], bcde[2], bcde[3]);
cout << " | ";
}

total[2] = total[1] / lajur;


cout << total[0] << " \t| " << total[0] / lajur << setw(9) << " \t| " << total[2] << setw(9) << " \t| ";
if (total[2] >= 2.0 && total[2] <= 4.0)
{
cout << " Kedudukan Baik (KB) " << setw(9) << " \t| ";
}
else if (total[2] >= 1.7 && total[2] < 2.0)
{
cout << " Kedudukan Syarat (KS) " << setw(9) << " \t| ";
}
else if (total[2] < 1.7)
{
cout << " Kedudukan Gagal (KG) " << setw(9) << " \t| ";
}

cout << " A = " << A << " | B = " << bcde[0] << " | C = " << bcde[1] << " | D = " << bcde[2] << " | E = " << bcde[3] << " | ";
cout << endl;
cout << "\t\t\t";
displayline(151);

}
cout << " Total A for " << subjek[0] << " = " << A1[0] << endl;
cout << " Total A for " << subjek[1] << " = " << A1[1] << endl;
cout << " Total A for " << subjek[2] << " = " << A1[2] << endl;
cout << endl;
float w[3];
w[0] = (A1[0] / baris) * 100;
w[1] = (A1[1] / baris) * 100;
w[2] = (A1[2] / baris) * 100;
for (int j = 0; j < lajur; j++)
{
cout << " The Percentage student A for " << subjek[j] << " = " << w[j] << " % " << endl;
if (w[j] >= 0 && w[j] < 40)
{
cout << endl;
cout << " Conclusion : Need to be Improve " << endl;
cout << endl;
}
else if (w[j] >= 40 && w[j] < 70)
{
cout << endl;
cout << " Conclusion : Good result!! " << endl;
cout << endl;
}
else if (w[j] >= 70 && w[j] <= 100)
{
cout << endl;
cout << " Conclusion : Excellent!! " << endl;
cout << endl;
}}}

void outputB(float a[][lajur])


{
float tinggi[baris];
float rendah[baris];
rendah[0] = a[0][0]; rendah[1] = a[0][1]; rendah[2] = a[0][2];
int who[6];
for (int i = 0; i < baris; i++)
{
for (int j = 0; j < lajur; j++)
{
if (j == 0)//subjek programming 2
{
if (tinggi[j] <= a[i][j])
{
tinggi[j] = a[i][j];
who[0] = i;
}
if (rendah[j] >= a[i][j])
{
rendah[j] = a[i][j];
who[1] = i;
}
}
if (j == 1)//subjek web
{
if (tinggi[j] <= a[i][j])
{
tinggi[j] = a[i][j];
who[2] = i;
}
if (rendah[j] >= a[i][j])
{
rendah[j] = a[i][j];
who[3] = i;
}
}
if (j == 2)//subjek sad
{
if (tinggi[j] <= a[i][j])
{
tinggi[j] = a[i][j];
who[4] = i;
}
if (rendah[j] >= a[i][j])
{
rendah[j] = a[i][j];
who[5] = i;
}}}}
cout << endl;
displayline(15);
cout << " Summary " << endl;
displayline(15);

cout << " Markah Paling TINGGI Untuk " << subjek[0] << " : " << tinggi[0] << endl;
cout << " " << nama[who[0]] << " memperolehi markah tertinggi untuk " << subjek[0] << endl;
cout << endl;
cout << " Markah Paling RENDAH Untuk " << subjek[0] << " : " << rendah[0] << endl;
cout << " " << nama[who[1]] << " memperolehi markah rendah untuk " << subjek[0] << endl;
cout << endl;
cout << " Markah Paling TINGGI Untuk " << subjek[1] << " : " << tinggi[1] << endl;
cout << " " << nama[who[2]] << " memperolehi markah tertinggi untuk " << subjek[1] << endl;
cout << endl;
cout << " Markah Paling RENDAH Untuk " << subjek[1] << " : " << rendah[1] << endl;
cout << " " << nama[who[3]] << " memperolehi markah rendah untuk " << subjek[1] << endl;
cout << endl;
cout << " Markah Paling TINGGI Untuk " << subjek[2] << " : " << tinggi[2] << endl;
cout << " " << nama[who[4]] << " memperolehi markah tertinggi untuk " << subjek[2] << endl;
cout << endl;
cout << " Markah Paling RENDAH Untuk " << subjek[2] << " : " << rendah[2] << endl;
cout << " " << nama[who[5]] << " memperolehi markah rendah untuk " << subjek[2] << endl;

void determineByStudent(float b[][lajur], float a[], int siapa)


{
for (int i = 0; i < lajur; i++)
{
a[i] = b[siapa - 1][i];
}
cout << "Markah untuk pelajar " << siapa << endl;
for (int i = 0; i < lajur; i++)
{
cout << a[i] << "\t";
}
cout << endl;
}
void determineBestLow(float c[], int siapa)
{
float low = 100, high = 0, total = 0;
int subjekLow = 0, subjekHigh = 0;
for (int i = 0; i < lajur; i++)
{
if (low >= c[i])
{
low = c[i];
subjekLow = i;
}
if (high <= c[i])
{
high = c[i];
subjekHigh = i;
}
total = total + c[i];
}
cout << "Keputusan Analisa untuk : " << nama[siapa - 1] << endl;
cout << "Jumlah markah untuk : " << nama[siapa - 1] << " " << total << endl;
cout << "Markah Tertinggi " << high << " untuk subjek " << subjek[subjekHigh] << endl;
cout << "Markah Terendah " << low << " untuk subjek " << subjek[subjekLow] << endl;
}

int main()
{

float marks[baris][lajur];
int id_pelajar;
float markahP[lajur];
inputA(marks);
cout << "Pilih pelajar untuk di analisa : ";
cin >> id_pelajar;
determineByStudent(marks, markahP, id_pelajar);
determineBestLow(markahP, id_pelajar);
/*outputA(marks);
outputB(marks);*/
}
STRUCT EXAMPLE
#include <iostream>
#include<string>

using namespace std;


struct {
string myString;
float markah;
}mystructure;

struct Student{
string myString;
float markah;
}
void determineBest(Student b[])
{
float high = 0;
string namaTinggi = "y";
for (int i = 0; i < 2; i++)
{
if (high <= b[i].markah)
{
high = b[i].markah;
namaTinggi = b[i].myString;
}
}
cout << " The best student is " << namaTinggi << " with " << high << " marks "<< endl;
}
int main()
{
Student a[2];
for (int i = 0; i < 2; i++)
{
cout << " Please enter your name " << i + 1 << " : ";
cin >> a[i].myString;
cout << " Please enter your marks " << i + 1 << " : ";
cin >> a[i].markah;
}
cout<< " Display output :" << endl;
for (int i = 0; i < 2; i++)
{
cout << " Name " << i + 1 << " : ";
cout << a[i].myString << endl;
cout << " Marks " << i + 1 << " : ";
cout << a[i].markah << endl;
}
determineBest(a);

}
LAB TEST
#include <iostream>
#include <iomanip>
using namespace std;

const int x = 6; //the size of the array

void input(float bil[])


{
for (int i = 0;i < x;i++) //to loop and input the value in the array
{
cout << "Phone Bill for Month "<< i+1 <<" : ";
cin >> bil[i];
while (bil[i] < 0) //cannot input negative value, loop again until get positive value
{
cout << "Phone Bill for Month " << i + 1 << " : ";
cin >> bil[i];
}
}
cout << endl;
}

void displayline(int a)
{
for (int i = 0;i < a;i++) //to display line
{
cout << "-";
}
cout << endl;
}

void displayH(float bil[],float& total)


{
total = 0; //give a value to total
cout << "Display Input from User in Horizontal Format" << endl;
displayline(57); //57 - to display a line
cout << "|" << setw(6) << "Month" << "|";
for (int i = 0;i < x;i++)
{
cout << setw(6) << i << "|"; //to display month in horizontal
}
cout <<" Total|"<< endl;
displayline(57);
cout << "|Amount|";
for (int i = 0;i < x;i++)
{
cout << setw(6) << bil[i] << "|";//to display amount in horizontal
total += bil[i]; //to calculate total
}
cout << setw(6) << total << "|" <<endl; //display total
displayline(57);
cout << endl;
}

void displayV(float bil[], float total)


{
cout << "Display Input from User in Vertical Format" << endl; //fixed prompt
displayline(15); //15 - to display a line
cout << "| Month|Amount|" << endl;
for (int i = 0;i < x;i++)
{
cout << "|" << setw(6) << i + 1 << "|" << setw(6) << bil[i] << "|" << endl; //to display month
and amount in vertical
displayline(15);
}
cout << "| Total|" << setw(6) << total << "|" << endl; //to display total in vertical
displayline(15);
}

void inputP(float total,float& pay)


{
cout << "\nTotal need to pay is RM " << total <<endl <<"Please key-in the amount of payment : RM ";
//state the total need to pay and let user to key-in the payment
cin >> pay;
}

void output(float total, float& pay)


{
inputP(total, pay); //call the function inputP
if (pay > total) //if the payment was enough
{
cout << "Payment Successful! Your balance is RM " << pay - total <<endl;
}
else //if the payment was not enough
{
cout << "Payment Unsuccessful! Your payment is not enough (RM " << total - pay << " more
needed)" << endl;
output(total, pay); //run again this function to let user input again the payment
}
}

void output2()
{
cout << "Thank you for using this system!\n"; //to make the programme got 8 function
}

int main()
{
float bil[x], total,pay;
input(bil);
displayH(bil, total);
displayV(bil, total);
output(total, pay);
output2();
}
2D ARRAY EXAMPLE

#include <iostream>
#include <string>
using namespace std;

string name[] = { "Zikri","Shanan","Daniel","Shaar" };


string subject[] = { "SAD" , "P2" ,"OS","WEB" };

const int x = 4;

void input(float mark[][x], float total[])


{
for (int i = 0; i < x; i++)
{
total[i] = 0;
for (int j = 0; j < x; j++)
{
cout << "Please key-in marks of " << subject[j] << " for " << name[i] << " : ";
cin >> mark[i][j];
while (mark[i][j] < 0 || mark[i][j] >100)
{
cout << "Please key-in marks of " << subject[j] << " for " << name[i] << " : ";
cin >> mark[i][j];
}
total[i] += mark[i][j];
}
}

void output(float mark[][x], float total[],int& a)


{
cout << "Sila masukkan pelajar yang ingin analisis (1 = Zikri, 2 = Shanan, 3 = Daniel, 4 = Shaar) : ";
cin >> a;
while (a < 0 || a > x)
{
cout << "Sila masukkan pelajar yang ingin analisis (1 = Zikri, 2 = Shanan, 3 = Daniel, 4 = Shaar) : ";
cin >> a;
}
int y = 0;
int z = 0;
for (int i = 0; i < x; i++)
{
if (y < mark[a-1][i])
{
y = mark[a-1][i];
z = i;
}
}
cout << "Markah tertinggi bagi " << name[a-1] << " adalah " << mark[a-1][z] << " dalam subject " << subject[z] <<
endl;
for (int i = 0; i < x; i++)
{
if (y > mark[a-1][i])
{
y = mark[a-1][i];
z = i;
}
}
cout << "Markah terendah bagi " << name[a-1] << " adalah " << mark[a-1][z] << " dalam subject " << subject[z]
<< endl;
}

void output2(float mark[][x],int a)


{
a -= 1;
int temp = 0;
string hold = " ";
for (int i = 0; i < x - 1; i++)
{
for (int j = 0; j < x - 1; j++)
{
if (mark[a][j] < mark[a][j + 1])
{
temp = mark[a][j];
mark[a][j] = mark[a][j+1];
mark[a][j + 1] = temp;
hold = subject[j];
subject[j] = subject[j + 1];
subject[j + 1] = hold;
}
}
}
cout << "Ranking marks by subject for "<< name[a] <<" : " << endl;
for (int i = 0; i < x; i++)
{
cout << "No." << i + 1 << " : " << subject[i] << " with total marks " << mark[a][i] << endl;
}
cout << endl;
}

int main()
{
float mark[x][x];
float total[x];
int a;
input(mark, total);
output(mark, total,a);
output2(mark, a);
}

MIDTERM
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int x = 2;
const int y = 4;

string name[] = { "ALI","MUN","MIN","MAN" };


string subject[] = { "C++","OSK" };

void input(float markah[][x],float total[])


{
cout << "Masukkan markah untuk pelajar dan subjek yang berkaitan." << endl;
for (int i = 0; i < 4; i++)
{
float z = 0;
for (int j = 0; j < x; j++)
{
cout << "Sila masukkan markah untuk " << name[i] << " bagi subjek " << subject[j]
<< " :";
cin >> markah[i][j];
while (markah[i][j] < 0 || markah[i][j] > 100)
{
cout << "Sila masukkan markah untuk " << name[i] << " bagi subjek " <<
subject[j] << " :";
cin >> markah[i][j];
}
z += markah[i][j];
}
total[i] = z;
}
cout << endl;
}

void line(int l)
{
for (int i = 0; i < l; i++)
{
cout << "-";
}
cout << endl;
}

void display(float markah[][x], float total[])


{
line(41);
cout << "Pelajar/Subjek|";
for (int j = 0; j < x; j++)
{
cout << subject[j] << "|";
}
cout << "Jumlah|Purata|GPA|"<<endl;
line(41);
for (int i = 0; i < y; i++)
{
float a = total[i] / x;
int b;
if (a >= 80 && a <= 100)
{
b = 4;
}
else if (a >= 60 && a <= 79)
{
b = 3;
}
else if (a >= 40 && a <= 59)
{
b = 2;
}
else
{
b = 1;
}
cout << setw(14) << name[i] << "|";
for (int j = 0; j < x; j++)
{
cout << setw(3) << markah[i][j] << "|";
}
cout << setw(6) << total[i] << "|" << setw(6) << a << "|" <<setw(3) << b << "|" <<endl;
line(41);
}
cout << endl;
for (int j = 0; j < x; j++)
{
float highest = 0;
int n = 0;
for (int i = 0; i < y; i++)
{
if (markah[i][j] > highest)
{
highest = markah[i][j]; //to find highest in every subject
n = i;
}
}
cout << "Pelajar terbaik untuk subjek " << subject[j] << " adalah " << name[n] <<" dengan "
<< highest <<" markah" <<endl;
}
float hi4to = 0;
int m = 0;
for (int i = 0; i < y; i++)
{
if (total[i] > hi4to)
{
hi4to = total[i]; //to find highest total
m = i;
}
}
float a = hi4to/x; //to calculate the average for highest total
int b;
if (a >= 80 && a <= 100)
{
b = 4;
}
else if (a >= 60 && a <= 79)
{
b = 3;
}
else if (a >= 40 && a <= 59)
{
b = 2;
}
else
{
b = 1;
}
cout << "Pelajar mendapat jumlah markah tertinggi adalah " << name[m] << " dengan " << total[m] <<
" markah dan GPA :" << b << endl;
}

int main()
{
float markah[y][x];
float total[y];
input(markah, total);
display(markah, total);
}

You might also like