Tải bản đầy đủ (.pdf) (28 trang)

btec level 5 hnd diploma in computing prog102 procedural programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.64 MB, 28 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b> ASSIGNMENT 1 FRONT SHEET </b>

<b>Unit number and title </b> Prog102: Procedural Programming

<b>Student declaration </b>

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice.

<b>Student’s signature Grading grid </b>

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<b> </b>

<b>Summative Feedback: </b>

<b> </b>

<b>Resubmission Feedback:</b>

<b>Lecturer Signature:</b>

Task 1:

- Computer programming languages:

A program is what instructs the computer to perform a task. in other words, the control of a computer program that uses the computer as a device to process data. The raw data is processed into the desired output format during program execution. These programs are written in high-level programming languages. It is similar to human language. It's too complicated for a computer to understand. Computers can only understand machine language and low level language. So we have to create simple and basic program. People have different languages to communicate with each other. computers also have different

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

languages to communicate with each other like C, C++, C#, Java, Python, etc. In conclusion, computers only understand machine language, also known as binary language (language of 0 and 1).But computers can also understand programs written in high-level languages by humans(C, C++, C#, Java, Python, etc).

-Procedural programming:

The first thing a programmer can learn is procedural programming. Procedure code is code that directly instructs devices to complete a task. It takes a top-down linear approach and treats data and procedures as two different things. According to the concept of procedure, procedural programming can be called

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

routine or function, it contains many steps to be performed. In short, procedural programming generates a list of instructions for the computer. tells the computer what to do to complete the task.

-Key features of procedural programming:

+ Predefined functions: a command identified by name. These functions are usually created in high-level programming languages. they are in the library or the registry.

+ Local Variable: a variable declared in the main struct and limited to the provided local scope. code will stop working if used outside the specified method.

+ Global Variable: a variable declared outside of a function every other function. So global variable can be used in all functions.

+ Modularity: Two systems with different missions work together to complete the most important task first. Once done, the two systems will do separate tasks.

+ Parameter Passing: used to pass parameters to functions, subroutines or procedures. Can be done via 'pass by name', 'pass by result', 'pass by value', 'pass by value-result' 'pass by reference'.

Task 2:

- The variables and data types required in the program : int n,id,a,maxid,minid;

char name; student sv,temp;

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

int n;

float grade,math,phys,chemis,max,min;

-Two different selection structures: 1) void max(student sv[100],int n); 2) void increase(student sv[100],int &n);

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

+Condition to check: input student’s grade (both structures). +The reason it’s necessary:

1) Finding max student’s grade.2) Sort increasing student’s grade.+It can be used in:

1) Outside the main function. In my assignment: After void output(student sv[100],int &n) and before void min(student sv[100],int n).

2) Outside the main function. In my assignment: After void hoanvi(student &x,student &y) and before void decrease(student sv[100],int &n).

+ Iteration constructs if max < sv[i] then max = sv[i]. Repeat in succession to find the max. : Code:

for(int i = 0;i < n;i++){ if(max < sv[i].grade){

max = sv[i].grade; maxid = sv[i].id; }

}

Task 3:

-Flow chart diagrams for finding max grade:

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

g g g

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

Start

<small>Max = [0]; sv i = 0 to n - 1 </small>

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

-Flow chart diagrams for finding m grade: in

<small>End </small>

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

Start

<small>Min = sv[0]; i = 0 to n - 1 </small>

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

-Review / evaluate my design:

+pros: The program looks easy to understand and full of requirements. +cons: The program is still too simple and lacks clarity.

<small>End </small>

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

+which needs to improve: Added more features to be more clear.

Test plan:

1 Validation of input Enter integer number to the menu input

Number from 1 to 6 and other numbers

from 1 to 6: excepted, other numbers will tell the user “no option”and “try again”

2 Validation of input name Enter character

student’name <sup>Any letters </sup> <sup>Show successfully </sup><sub>and correct student </sub>name

3 Accurate calculations of finding max grade

Enter float student’s grade

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

#include<stdio.h> #include<string.h> #include<math.h> using namespace std; struct student {

int id;

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

float grade,math,phys,chemis; char name[30];

};

void input(student sv[100],int &n); void output(student sv[100],int &n); void menu(student sv[100],int n); void max(student sv[100],int n); void min(student sv[100],int n); void increase(student sv[100],int &n); void decrease(student sv[100],int &n); int main()

{

student sv[100]; int n;

menu(sv,n); return 0; }

void menu(student sv[100],int n){

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

int a; do{

cout<<" --MENU--\n";

cout<<"1: input information student\n"; cout<<"2: output information student\n"; cout<<"3: max average grade\n"; cout<<"4: min average grade\n";

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

cout<<"5: average grade increase\n"; cout<<"6: average grade decrease\n"; cout<<"> 7: out of the menu\n"; cout<<" CHOSE OPTION: "; cin>>a;

switch (a) {

case 1:

input(sv,n); break;

case 2:

output(sv,n); break;

case 3:

max(sv,n);

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

break;

case 4:

min(sv,n); break; case 5:

increase(sv,n);

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

output(sv,n); break; case 6:

decrease(sv,n); output(sv,n);

break; default:

} while (a < 6); }

void input(student sv[100],int &n){ cout<<"\ninput number student: "; cin>>n;

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

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

cout<<"\nStudent "<<i+1; cout<<"\ninput id "; cin>>sv[i].id; cout<<"input name: "; fflush(stdin); gets(sv[i].name);

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

cout<<"physics grade: "; cin>>sv[i].phys; cout<<"math grade: "; cin>>sv[i].math; cout<<"chemistry grade: "; cin>>sv[i].chemis; cout<<"\n"; }

cout<<"\nName student: "<<sv[i].name; cout<<"\nphysics grade: "<<sv[i].phys; cout<<"\nmath grade: "<<sv[i].math; cout<<"\nchemistry grade: "<<sv[i].chemis;

</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">

sv[i].grade = (sv[i].phys + sv[i].math + sv[i].chemis) / 3; cout<<"\naverage grade: "<<sv[i].grade<<"\n\n"; }

}

void max(student sv[100],int n){ float max = sv[0].grade; int maxid = sv[0].id;

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

for(int i = 0;i < n;i++){ if(max < sv[i].grade){

max = sv[i].grade; maxid = sv[i].id; }

if(min > sv[i].grade){ min = sv[i].grade; minid = sv[i].id; }

}

cout<<"\n\nMIN AVERAGE GRADE: "<<min<<"\nID: "<<minid<<endl<<endl;

</div><span class="text_page_counter">Trang 24</span><div class="page_container" data-page="24">

}

void hoanvi(student &x,student &y){ student temp;

temp = x; x = y; y = temp; }

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

void increase(student sv[100],int &n){ for(int i = 0; i < n;i++){

for(int j = i+1;j < n;j++){

if(sv[i].grade > sv[j].grade){ hoanvi(sv[i],sv[j]); }

} }

cout<<"\n -GRADE INCREASE: "; }

void decrease(student sv[100],int &n){ for(int i = 0; i < n;i++){

for(int j = i+1;j < n;j++){

if(sv[i].grade < sv[j].grade){ hoanvi(sv[i],sv[j]); }

} }

</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">

cout<<"\n -GRADE DECREASE: "; }

</div>

×