Procedural Programming in C: A Menu-Based Student Information System, Assignments of C programming

Procedural Programming assignment

Typology: Assignments

2020/2021

Uploaded on 06/16/2021

unkaeciique
unkaeciique 🇻🇳

4.7

(174)

29 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 1 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 0: Procedural Programming
Submission date
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Hồ Ngọc Khánh
Student ID
GCS200074
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
P1
P2
P3
M1
M2
D1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Procedural Programming in C: A Menu-Based Student Information System and more Assignments C programming in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 0 : Procedural Programming Submission date Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Hồ Ngọc Khánh Student ID GCS 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 P1 P2 P3 M1 M2 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Lecturer Signature:

  • A. INTRODUCTION (P1)
    • I. Introduction of the problem
    • II. Project Requirements
  • B. BASICS OF PROCEDURAL PROGRAMMING
    • I. Computer programming
        1. Definition of Computer Programming
        1. Definition of Computer Programming Languages....................................................................
    • II. About coding
    • III. Procedural Programming
        1. Definition of procedural programming
        1. Key Features of Procedural Programming
        1. Variables
        1. The key factor in Procedural Programming
        1. Advantages and Disadvantages of Procedural Programming
  • C. ANALYSIS AND DESIGN (P1)
    • I. Variables and data types required
    • II. Selection Structures
    • III. Split the program into functions and demonstrated by diagrams
        1. Functions
        1. Diagrams
  • D. IMPLEMENTATION (P1, M1)
    • I. Code
    • II. Result..........................................................................................................................................
  • E. Evaluation (D1)
    • I. Condition statements.................................................................................................................
    • II. Iteration statement
  • F. CONCLUSION
    • Figure 1: TEACHER'S STRUGGLING IN STORING GRADES AND WORK ON THEM TABLES OF FIGURES
    • Figure 2: THE CORE DEFINITION OF COMPUTER PROGRAMMING
    • Figure 3: CHARLES BABBAGE AND THE CREDIBLE COMPUTER
    • Figure 4: THE CORE DEFINITION OF COMPUTER PROGRAMMING LANGUAGES
    • Figure 5: CODING......................................................................................................................................
    • Figure 6: FLOWCHART EXAMPLE OF CALCULATING THE EQUATION A+B
    • Figure 7: C PROGRAM EXAMPLE OF CALCULATING THE EQUATION A+B
    • Figure 8: EXAMPLES OF LOCAL VARIABLES (LEFT SIDE: float min=sc[0]; RIGHT SIDE: int x,num;)
    • Figure 9: EXAMPLE OF GLOBAL VARIABLES: float sc[50] and int id[50]
    • Figure 10: EXAMPLES OF OPERATORS IN C LANGUAGE
    • Figure 11: FLOWCHART OF CORE DEFINITION OF CONDITION STATEMENTS
    • Figure 12: EXAMPLE OF PROGRAM WITH CONDITION STATEMENTS "if...else"
    • Figure 13: FLOWCHART OF CORE DEFINITION OF SWITCH CASE STATEMENTS
    • Figure 14:EXAMPLE OF PROGRAM WITH SWITCH CASE STATEMENTS
    • Figure 16: FLOWCHART OF CORE DEFINITION OF ITERATION STATEMENTS
    • Figure 15: EXAMPLES OF PROGRAM WITH ITERATION STATEMENTS
    • Figure 17: "SWITCH...CASE" MENU OPERATION
    • Figure 18: "IF...ELSE" MENU OPERATION
    • Figure 19: ITERATION STATEMENT TO BUILD REPETITIVE MENU OPERATION
  • FUNCTIONS Figure 20: ITERATION STATEMENT AND CONDITION STATEMENT OF THE TWO CALL-FOR-VALUE
    • Figure 21: ITERATION STATEMENT OF THE TWO CALL-FOR-REFERENCE FUNCTIONS
    • Figure 22: CALL-FOR-REFERENCE FUNCTION: student_input
    • Figure 23: CALL-FOR-REFERENCE FUNCTION: student_output
    • Figure 24: CALL-FOR-VALUE FUNCTION: max_score
    • Figure 25: CALL-FOR-VALUE FUNCTION: min_score
    • Figure 26: FLOWCHART SUMMARIZING THE WHOLE PROGRAM
    • Figure 27: USE-CASE DIAGRAM SUMMARIZING THE WHOLE PROGRAM
    • Figure 28: INTRODUCE FILE HEADER AND DECLARE TWO GLOBAL ARRAYS
    • Figure 29: CODE FUNCTION: student_input
    • Figure 30: FLOWCHART OF student_input
    • Figure 31: CODE FUNCTION: student_output
    • Figure 32: FLOWCHART OF student_output
    • Figure 33: CODE FUNCTION: max_score
    • Figure 34: FLOWCHART OF max_score
    • Figure 35: CODE FUNCTION: min_score.................................................................................................
  • Figure 36: FLOWCHART OF min_score
  • Figure 37: MAIN FUNCTION
  • Figure 38: FLOWCHART OF MAIN FUNCTION
  • Figure 39: GRADE'S VALIDATION CHECKING IN student_input
  • Figure 40: FIRST ORDER'S RESULT: PRINT ALL STUDENTS' GRADES
  • Figure 41: SECOND ORDER'S RESULT: STUDENT ID WITH THE HIGHEST GRADE
  • Figure 42: THIRD ORDER'S RESULT: STUDENT ID WITH THE LOWEST GRADE
  • Figure 43: CHECKING THE MENU OPERATION WITH OFF-NUMBERS
  • Figure 44: FOURTH ORDER'S RESULT: EXIT PROGRAM
  • Figure 45: FUNCTIONS: max_score AND min_score
  • Figure 46: READING-ORDERS PROCEDURE USING "IF...ELSE"
  • Figure 47: READING-ORDERS PROCEDURE USING "SWITCH...CASE"
  • Figure 48: FUNCTION: student_input.....................................................................................................
  • Figure 49: FUNCTION: student_output
  • Figure 50: CHECKING VALIDATION OF GRADES
  • Figure 51: MENU OPERATION ITERATION STATEMENT

B. BASICS OF PROCEDURAL PROGRAMMING

I. Computer programming

1. Definition of Computer Programming Computer programming is the process of composing the instructions. We use the programming language to do so. These instructions are converted into a readable language that a machine can understand. The directions are then taken by the computer. Figure 2 : THE CORE DEFINITION OF COMPUTER PROGRAMMING Since the invention of Charles Babbage's difference engine in 1822, machines also required a way of instructing them to perform a particular operation. This means that it is regarded as a programming language. Computer languages consisted, initially, of a series of steps to wire a given programme; these were converted into a series of steps keyed into a computer and then executed; later, these languages gained sophisticated features such as logical branching and object orientation. The machine languages over the last fifty years have arrived in two phases, the first major languages and the second major languages which are currently in use. Figure 3 : CHARLES BABBAGE AND THE CREDIBLE COMPUTER

2. Definition of Computer Programming Languages Computer programming languages fall into the range of low-level languages, such as assembly, and high-level programming languages, such as JavaScript, C/C++, C# and Python which are the most famous ones. On an easier thought, programming languages are like the languages that we speak, languages are used to express ideas but being spoken in different ways. Figure 4 : THE CORE DEFINITION OF COMPUTER PROGRAMMING LANGUAGES For instance, to say “Hello!”, the French would say “Bonjour!” while Spanish would say “Hola!”. Just like English with its understanding grammars, each computer programming language has its own “grammar” or they called it “syntax”. As shown by some specific demonstrations of printing out “Hello!”,

  • Python: print(“Hello”)
  • JavaScript: alert(“Hello”);
  • Pascal: writeln(“Hello”);

II. About coding

Coding is a technique where you take orders (steps in a task) and translate them into a language that the machine knows because machines don't talk the same language as humans do. They interact in a language called BINARY and use 0's and 1's. Figure 5 : CODING

Figure 7 : C PROGRAM EXAMPLE OF CALCULATING THE EQUATION A+B

2. Key Features of Procedural Programming - Predefined functions : a predefined function is usually a name instruction. Typically, predefined functions are incorporated into higher-level programming languages, but are derived from the library or register rather than the software. An example of a predefined function is 'charAt(),' which searches for a character position in a string. - Local variable : The local variable is a variable that is declared in the key structure of the method and is limited to the local scope of the method. The local variable may only be used in the system in which it is specified, and if it is used outside the defined method, the code will cease to operate. - Global Variable : A global variable is a variable that is declared outside of some other function specified in the code. As a consequence, global variables can be used in all functions, unlike local variables. - Modularity : Modularity is where two different structures have two different tasks at hand but are grouped together first to accomplish a greater task. Each community of systems will then have its own tasks done one after the other before all tasks have been completed. - Parameter Passing : Parameter Passing is a method used to transfer parameters to functions, subroutines or processes. Parameter Passing can be achieved by 'pass by value,' 'pass by comparison,' 'pass by result,' 'pass by value-result,' and 'pass by term.' 3. Variables Variables are the names of the location of the memory. They are used to store the data. Their values can be modified, and they can be reused several times. It's a way to represent the place of memories in a symbol such that it can be readily recognised. RULES FOR SETTING VARIABLES

  • A variable can include alphabets, numbers, and underscores.
  • The name of the variable will start with the alphabet, and just underscore. It's not going to start with a digit.
  • No whitespace is permitted in the name of the variable.
  • The name of the variable must not be any reserved word or keyword. TYPES OF VARIABLES IN C
  • Local variable: is declared inside the function or block
  • Global variable: is declared outside the function or block, available to all functions and any function can change its value.
  • Static variable: is declared with the static keyword
  • Automatic variable: is declared inside the block, so mostly variables are automatic variables.
  • External variable: is a variable can be shared in multiple C source files. 4. The key factor in Procedural Programming i. Variables There are two most used means of variables, the rest are hardly used in educational curriculum:
  • Local variables : They are declared inside any kind of function, included main function. Local variables can only be used inside the functions they stay in. Otherwise, their values will only stay in those functions, hence we need to return some needed variables if there are not main function.
  • Global variables : They are declared outside of any function, whatever positions in the file you declared for global variables, they are still the ones which initialize the first place. Global variables can be used anywhere in the file and being changed by any equation you gave to them. Figure 9 : EXAMPLE OF GLOBAL VARIABLES: float sc[50] and int id[50] Figure 8 : EXAMPLES OF LOCAL VARIABLES (LEFT SIDE: float min=sc[0]; RIGHT SIDE: int x,num;)
  • Switch Case Statement : allows a variable to be evaluated for equality with a list of values. Each value is called a case, and the variable that is turned on is verified for each switch case. Switch case statements are only used in some specific situation where the expression are integer or character values. Beside, at the end of each case, there should be a break command to stop the switch case statement. Figure 13 : FLOWCHART OF CORE DEFINITION OF SWITCH CASE STATEMENTS Figure 14 :EXAMPLE OF PROGRAM WITH SWITCH CASE STATEMENTS
  • Iteration Statement : allows us to execute a statement or group of statements multiple times. However, there will always be a condition to stop the loop or unless it will go on forever. Iteration statement is basically a loop combined with condition statement to do task over and over. Figure 16 : FLOWCHART OF CORE DEFINITION OF ITERATION STATEMENTS There are three most used kinds of iteration statements, they are “for”, “while” and “do…while”. 5. Advantages and Disadvantages of Procedural Programming ADVANTAGES
  • Procedural programming is excellent for general programming.
  • Coded flexibility and ease of execution of compilers and interpreters
  • A huge range of books and online course materials accessible on validated algorithms, making it easier to learn along the way.
  • The source code is modular, so it can be used to hack a certain CPU as well.
  • The coding can be replicated in various parts of the software without having to copy it.
  • The memory requirement also slashes with the Procedural Programming technique
  • The program flow can be easily tracked Figure 15 : EXAMPLES OF PROGRAM WITH ITERATION STATEMENTS

C. ANALYSIS AND DESIGN (P1)

I. Variables and data types required

  • For the menu operation, I will use an integer variable to take orders repeatedly and condition statements which are “if…else” or “switch…case” to go along. Since the orders can only be taken from the keyboard, choosing integer numbers is the best choice of all.
  • The two global arrays are students’ IDs and students’ grades, which will be declared from the beginning of the program o id[100] with the data type of integer will store the information of students’ IDs since IDs are specific rows of number and somehow can not be constanting. o sc[100] with the data type of float will store information of students’ grades since grades are combination of certain number and decimal number. However, grades have to stay between 0 and 10. So when scanning in a value which is invalid, a notification of re-typing grade will be shown up.
  • The highest and lowest grade also have the data type of float since they take out the highest and lowest grades from the sc[100] array. Hence, max_score and min_score functions will carry out for the highest and lowest grade. Beside, both of them at the beginning will be assigned as sc[0].
  • There are also some optional variables will assist along the program, which I will mention when we meet, to make it more perfect.

II. Selection Structures

The problem makes us to think of two main objects, which are creating a four-cases menu based operation and functions of those four cases:

  1. Display Students’ IDs and Grades
  2. Show the student with the highest grade
  3. Show the student with the lowest grade
  4. Exit program With the menu operation , there are two ways to create by using “if…else” or “switch…case”. I’m going to show both of them so we can have a small comparison. I also added an error debugging if user type from the keyboard not the value from 1 to 4.
  • Using “switch…case”: switch (x){ case 1 : student_output(num); break;

case 2 : for (int i= 0 ;i<=num;i++) if (max_score(num)==sc[i]) printf("Student with ID %d has the highest score: %.2f\n",id[i],sc[i]); break; case 3 : for (int i= 0 ;i<=num;i++) if (min_score(num)==sc[i]) printf("Student with ID %d has the lowest score: %.2f\n",id[i],sc[i]); break; case 4 : break; default: printf("Please try again!\n"); break; } Figure 17 : "SWITCH...CASE" MENU OPERATION

  • Using “if…else”: if (x== 1 ) student_output(num); else if (x== 2 ){ for (int i= 0 ;i<=num;i++) if (max_score(num)==sc[i]) printf("Student with ID %d has the highest score: %.2f\n",id[i],sc[i]); } else if (x== 3 ){ for (int i= 0 ;i<=num;i++) if (min_score(num)==sc[i]) printf("Student with ID %d has the lowest score: %.2f\n",id[i],sc[i]); } else if (x== 4 ) break; else printf("Please try again!\n"); } Figure 18 : "IF...ELSE" MENU OPERATION
  • While the “switch…case” does seem easier to comprehend, I decided to stick with the “if…else” menu operation since it will help me evaluate more in the future since “if…else” can be used on almost any data type.

For the two call-for-value functions which are max_score and min_score will have the same preference. Both used the same iteration statement “for” and condition statement “if…else”. With the two call-for-reference functions , I used for both the same kind iteration statement which is “for” since it is comprehensive. In addition, in the student_input, I also used “while” to check on the grade typed from the keyboard. void student_input(int n){ for (int i= 0 ;i<=n;i++){ printf("Student %d's ID: ",i+ 1 ); scanf("%d",&id[i],"\n"); printf("Student %d's score: ",i+ 1 ); scanf("%f",&sc[i],"\n"); while (sc[i]< 0 || sc[i]>= 10 ){ printf("Grade's invalid. Please retype!\n"); printf("Student %d's score: ",i+ 1 ); scanf("%f",&sc[i],"\n"); } } } void student_output(int n){ for (int i= 0 ;i<=n;i++) printf("Student ID: %d --- Score: %.2f\n",id[i],sc[i]); } Figure 21 : ITERATION STATEMENT OF THE TWO CALL-FOR-REFERENCE FUNCTIONS

III. Split the program into functions and demonstrated by diagrams

1. Functions

  • Read and store students’ IDs and students’ grades Figure 20 : ITERATION STATEMENT AND CONDITION STATEMENT OF THE TWO CALL-FOR-VALUE FUNCTIONS

void student_input(int n){ for (int i= 0 ;i<=n;i++){ printf("Student %d's ID: ",i+ 1 ); scanf("%d",&id[i],"\n"); printf("Student %d's score: ",i+ 1 ); scanf("%f",&sc[i],"\n"); while (sc[i]< 0 || sc[i]>= 10 ){ printf("Grade's invalid. Please retype!\n"); printf("Student %d's score: ",i+ 1 ); scanf("%f",&sc[i],"\n"); } // "while" iteration to check student's grade to stay between 0 and 10 } } // function: read and store values which typed from keyboard . . . student_input(num); Figure 22 : CALL-FOR-REFERENCE FUNCTION: student_input

  • Display the students’ IDs and students’ grades void student_output(int n){ for (int i= 0 ;i<=n;i++) printf("Student ID: %d --- Score: %.2f\n",id[i],sc[i]); } // function: display the list of students' ID and grades . . . if (x== 1 ) student_output(num); // order taken as displaying students’ IDs and students’ grades Figure 23 : CALL-FOR-REFERENCE FUNCTION: student_output
  • Show the student with the highest grade float max_score(int n){ float max=sc[ 0 ];