Tải bản đầy đủ (.doc) (34 trang)

Assignment 2 PROG102 Greenwich

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.77 MB, 34 trang )

ASSIGNMENT 2 FRONT SHEET
Qualification

BTEC Level 5 HND Diploma in Computing

Unit number and title

PROG102: Procedural Programming

Submission date

Date Received 1st submission

Re-submission Date

Date Received 2nd submission

Student Name

Student ID

Class

Assessor name

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
P4



P5

M3

M4

D2






Summative Feedback:

Resubmission Feedback:

2.1

Grade:
Lecturer Signature:

Assessor Signature:

Date:


ASSIGNMENT 2
Analysis and design a solution for

procedural programming problem

1


Contents
1. Introduction ............................................................................................................................ 3
1.1. Scenario .......................................................................................................................... 3
1.2. Problem ........................................................................................................................... 3
1.3. Solution ........................................................................................................................... 3
2. Implementation ...................................................................................................................... 4
2.1. Explain about structure of program ............................................................................... 4
2.2. Explain about each function ............................................................................................ 7
2.3. Code: ................................................................................................................................ 16
4. Testing ..................................................................................................................................... 26
5. Evaluation ............................................................................................................................... 29
5.1. Evaluate my program ..................................................................................................... 29
5.2. Evaluate about Procedural programming with my program .................................... 30
6.Conclusion ............................................................................................................................... 30
Reference list .............................................................................................................................. 31

2


1. Introduction
1.1. Scenario
A math teacher wants to manage grades of a class. He asks you to help
him to write a small application to do that. He needs to enter student IDs,
student’s grades and store these information into 2 separate arrays (integer
array for IDs and float array for grades). Then he needs to print all student

IDs together with their grades. Finally, he needs to know which student
has highest grade and lowest grade. Your program should be menu based
with the options above. When an option is done, the program should go
back to the main menu so he can choose another option. There should be
an option to quit program.

1.2. Problem


The problem of student information management is a difficult problem.
Manual management method has many disadvantages.



I need to find a way to program and create a system that solved the
disadvantages of the old method.



The program should have the main functions needed to enter, print,
calculate min, max, average, exit, …

1.3. Solution


Using procedural programming to write programs.



The program needs an interface to select functions, variables and array to

enter information: name, ID, age, gender, subject scores.



Create separate functions for input, print, find the highest and lowest
scores, average score, result, rank, rating, exit.

3


2. Implementation
I will explain the whole program:

2.1. Explain about structure of program


Declaration section

-

Declare libraries:
<stdio.h>: Provide the ability to enter. Use printf, scanf, …
<string.h>: Adjust a variety of characters (char)
<stdlib.h>: Perform math operations, transformations, ordering, …
<conio.h>: Use it to use getch(), …

-

Struct:


This section declares a variable stu to store variables related to student
information.
Inside, I declare variables to store information:
+ a: ID
+ b: name
+ c: age
+ d: gender
+ math, English, prog, average: points
Thanks to the struct structure, I can use the information easily even
though there are many students.
4


-

The next is declaring functions.

At here, I declare all of functions.


Main

I write a loop to know the limit of number of students, declare an empty
array of structure stu.
Program interface and switch-case for selecting functions are also created
here.
5


Within functions are functions applied.




After the main
This is the place to write the functions, which will be used in main.

6


2.2. Explain about each function


Function “one”:

-

This function is used to enter student information: ID, name, age, gender,
grades of 3 subjects.

-

I use printf to request information from the user.

-

With variables of type int, float, I use scanf to save the parameter to the
variable.

-


With variables of type char, I use gets to store the parameter in the variable.

-

I use fflush(stdin); to clear the program’s temporary memory so that there
are no garbage values.

7




Function “two”:

This function is used to store student information as an element in an
array so that it can be easily used.

Command line

Explain

Void two (stu u[ ], int o)

Declare the function name as “two”,
declare the array u[ ] of the structure
stu, declare the variable o integer
type, o is the limit of the elements of
the array.

for (int i; i


Use loop to call the elements in the
array: variable int i=0, condition iafter completing the loop clause,
increase i by 1.

printf(“\n Enter student %d”, i+1);

This command helps the user to know
the number of students entering

one(u[i])

Run “one”
information

function

to

enter

Table 1. Explain function “two”
8




Function “three”:
This function is used to print all information like ID, name, age, gender,

scores of student.

Take as input the variables ss of structure stu. Use printf to print all the
information to the screen: ID, age, gender, grade of 3 subjects. Use %d,
%.1f, %s with variables to get information.


Function “four”:

This function is used to print out student information. As for how it works,
it’s similar to the “two” function. Instead of using the “one” function for
input, uses the “three” function to print out the data
9




Function “six”:

This function is used to calculate average. I use for loop to get the
scores information from the array. Recipe: average= total of 3 scores
(math, English, prog) divided 3.


Function “five”:

This function is quite long, it has the function of calculating the highest and
lowest points.



Find max: Initialize the float max variable, assign it to the average of the
nd
first element in the array. I do it for easy comparison with 2 value. Use
loop to call out the next elements. Use If to perform max< average. If true,
assign max = average. It helps me to find the larger value than the

10


previous one and ignore the smaller one. This command line sequence
helps to find the maximum value in an array.


Find min: Use the same algorithm as finding max. Initialize min variable
instead of max variable. In the If statement, compare min > average.



Finally I continue to use a loop to print the maximum and minimum
points. In addition, I also print the average score of all students.



Function “nine”:

This function is used to print Passed or Failed. Use loop to get elements of
array. Use If statement, compare average >=5. If true, print out ID with
name is passed, if false, print out ID with name is failed. There are simple
statement to compare and find out result to be passed or failed.



Function “ten”:
This function is used to print out rank of students. Initialize a variable HD
of stu.

11


Use loop 1 with i=0 and condition iy=1 and condition yretrieve 2 consecutive elements in the array.

Use the If clause to compare the average score of the first element < the
mean of the second element.

If true, use the following command lines to swap the positions of those 2
elements in array.

Finally, use a loop to print out the results.

12


Function “XL2”:

This function is quite similar to the function “nine” but it focuses more
deeply. Use a loop to get the elements of the array, use If else, else if
to compare and print the student rating.



How functions are used.

All function has command line getch(); it notifies the user to type any key
to return to the main interface.
-

Function 1: Enter information.

13


Here, function “two” is run first. Inside this function there is a function “one”
with all the information of student, the functions “two” will switch to the
next student. Function “six” is used to calculate the average score of each
student.
-

Function 2: Show all information of students.

Function “four” will be run. Because in this function there are function
“three”, these 2 functions run at the same time to print out all the
information of all the students.
-

Function 3: Min, max, average.

Function “five” will run independently to find and print the student’s max,
min, average.

14



-

Function 4: Result of course

Function “nine” will run to categorize students through subjects or failing
subjects.
-

Function 5: Rank of students

Function “ten” will run to sort and print out the student rank in the class.
-

Function 6: Rating

Function “XL2” will run to classify titles for students.
15


2.3. Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct stu{
int a;
char b[50];
int c;

char d[15];
float math,eng,prog,average=0;
};
void one(stu &ss);
void two(stu u[], int o);
void three(stu ss);
void four(stu u[], int o);
void five(stu u[],int o);
void six(stu u[],int o);
void nine(stu u[],int o);
void ten(stu u[],int o);
void XL2(stu u[], int o);
int main(){
int o;
do {
printf ("\n How many students:");
scanf("%d", &o);
}while (o<=0);

16


stu u[o];

while(true){
printf("*-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-*\n");
printf("____________ Student management system

____________\n");


printf("<<

1.Enter information of students

>>\n");

printf("<<

2.Show information of students

>>\n");

printf("<<

3.Max, Min, Average of class

printf("<<

4.Result of course

>>\n");

printf("<<

5.Rank of students

>>\n");

printf("<<


6.Rating

printf("<<

0.Exit

>>\n");

>>\n");
>>\n");

int in;
printf ("__ You need to select a function: ");
scanf("%d",&in);

switch(in){
case 1:
printf("Enter student list: \n");
two(u, o);
six(u,o);
printf("Enter any letter to continue");
getch();
break;
17


case 2:
printf ("Display the student list \n");
four(u,o);
printf("Enter any letter to continue");

getch();
break;
case 3:
five(u,o);
printf("Enter any letter to continue");
getch();
break;
case 4:
nine(u,o);
printf("Enter any letter to continue");
getch();
break;
case 5:
ten(u,o);
printf("Enter any letter to continue");
getch();
break;
case 6:
XL2(u,o);
printf("Enter any letter to continue");
getch();
break;
case 0:
printf("Thank you for using ");
18


exit(0);
break;
default:

break;
}}
return 0;
}
void one(stu &ss){
printf("\n Enter student ID:");
fflush(stdin);
scanf("%d",&ss.a);
printf ("\n Enter name:");
fflush(stdin);
gets(ss.b);
printf("\n Enter age:");
fflush(stdin);
scanf("%d",&ss.c);
printf ("\n Enter gender:");
fflush(stdin);
gets(ss.d);
fflush(stdin);
printf("\n Enter scores of Math,English,ProG:");
scanf("%f%f%f",&ss.math,&ss.eng,&ss.prog);
}
void two(stu u[], int o){
printf("\n-----------------");
19


for (int i=0; i {
printf("\n Enter student %d", i+1);
one(u[i]);

}
}
void three(stu ss){
printf("\n The student ID is %d ",ss.a);
printf("\n The name is %s", ss.b);
printf("\n The age is %d", ss.c);
printf("\n The gender is %s", ss.d);
printf("\n The scores of Math,English, ProG is %.1f and %.1f and
%.1f",ss.math,ss.eng,ss.prog);
printf("\n-------------------\n");
}
void four(stu u[], int o){
printf("\n-------------------\n");
for (int i=0; i{
printf("\n All infomation of student 1 is:", i+1);
three(u[i]);
}
printf("\n----------------------\n");}

void five(stu u[],int o){
int i=0;
20


float max=u[i].average;
for (int i=1;iif (maxmax=u[i].average;}
float min=u[i].average;

for (int i=1;iif (min>u[i].average)
min=u[i].average;
}
for(int i=0;iprintf("The average scores of %s is: %.1f \n",u[i].b,u[i].average);}
printf("The min average is: %.1f\n",min);
printf("The max average is: %.1f\n",max);
}
void six(stu u[],int o){
for(int i=0;iu[i].average=(float)(u[i].math+u[i].eng+u[i].prog)/3;
}
}
void nine(stu u[],int o){
for (int i=0;iif (u[i].average>=5)printf("Student has ID with name is %d and %s
PASSED\n",u[i].a,u[i].b);
else printf("Student has ID with name is %d and %s FAILED\n",u[i].a,u[i].b);
}}
21


void ten(stu u[],int o){
stu HD;
for(int i=0;ifor (int y=1;yif (u[i].average{
HD= u[i];

u[i]=u[y];
u[y]=HD;
}
}
}
for(int i=0;iprintf("Student with name %s has rank %d \n",u[i].b,i+1);}
}
void XL2(stu u[], int o){
printf("\n---------------------------------\n");
for(int i = 0;i < o;++i){
printf("\n Student rating %d is: ", i+1);
if(u[i].average >= 8) printf("Good student"); else
if(u[i].average >= 5) printf("Normal student"); else
printf("Stay at class"); }
printf("\n----------------------------------\n");
}

22


3. Program results

Ask the number of students when the program starts running

Interface of program

23



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×