ASSIGNMENT 1 FRONT SHEET Unit number and title Prog102: Procedural Programming Student declaration I certify that the assignment submission is entirely my own work and I fully understan
Trang 1ASSIGNMENT 1 FRONT SHEET
Unit number and title Prog102: Procedural Programming
Student declaration
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
Student’s signature Grading grid
Trang 3 Summative Feedback: Resubmission Feedback:
Lecturer Signature:
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
Trang 4languages 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
Trang 5routine 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'
Trang 6int 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);
Trang 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++){
Trang 8g g g
Trang 10-Flow chart diagrams for finding m grade: in
End
Trang 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
End
Trang 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 Any letters Show successfully and correct student
name
3 Accurate calculations of
finding max grade
Enter float student’s grade
Trang 14#include<stdio.h>
#include<string.h>
#include<math.h> using namespace std; struct student {
int id;
Trang 15float 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()
Trang 17cout<<"5: average grade increase\n"; cout<<"6: average grade decrease\n"; cout<<"> 7: out of the menu\n"; cout<<" CHOSE OPTION: "; cin>>a;
Trang 18break;
case 4:
min(sv,n); break; case 5:
increase(sv,n);
Trang 19} while (a < 6);
}
void input(student sv[100],int &n){
cout<<"\ninput number student: ";
cin>>n;
Trang 20for(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);
Trang 22sv[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;
Trang 23for(int i = 0;i < n;i++){
void min(student sv[100],int n){
float min = sv[0].grade;
int minid = sv[0].id;
for(int i = 0;i < n;i++){
Trang 25void 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]); }
}
}
Trang 26cout<<"\n -GRADE DECREASE: "; }