CSC 138 Structured Programming REVISION CHAPTER ONE One

  • Slides: 53
Download presentation
CSC 138 – Structured Programming REVISION CHAPTER ONE One Dimensional Array CHAPTER TWO Two

CSC 138 – Structured Programming REVISION CHAPTER ONE One Dimensional Array CHAPTER TWO Two Dimensional Array CHAPTER THREE Records CHAPTER FOUR Input Output File

LEARNING OBJECTIVES Upon completion, you should be able to understand: o the structure of

LEARNING OBJECTIVES Upon completion, you should be able to understand: o the structure of 1 D array o the structure of 2 D array o the structure of records/struct o the structure of read(instream) and write(ofstream) file o Apply all the concept for implementation phase(mini project)

VARIABLE VS. ARRAY Variable • Variable can only store one value at a time

VARIABLE VS. ARRAY Variable • Variable can only store one value at a time Fruit : Apple Array • Array can store many values of the same data type at one time Fruit : Apple Grape Kiwi

REFRESH YOUR MEMORY Declare an array of type float with 25 elements with identifier

REFRESH YOUR MEMORY Declare an array of type float with 25 elements with identifier my_float_array float my_float_array [25] ; DATA TYPE VARIABLE NAME -1 D ARRAY DECLARATION- Array size

REFRESH YOUR MEMORY 1. Declare an array of type char with 1000 elements with

REFRESH YOUR MEMORY 1. Declare an array of type char with 1000 elements with identifier my_char_array 2. Declare an array of type double called “price” having 5 elements 3. Declare an array “Scores”having 20 elements of type integer

REFRESH YOUR MEMORY 4. Explain the error in the following array declaration: a) char

REFRESH YOUR MEMORY 4. Explain the error in the following array declaration: a) char my char array []; b) score[25]; c) int ids[-30]; d) double [50] salaries;

INPUT DATA INTO ARRAY Counter-controlled loop: int main() { int num[5]; for(int i =0;

INPUT DATA INTO ARRAY Counter-controlled loop: int main() { int num[5]; for(int i =0; i<5; i++) cin >> num[i]; }

INPUT DATA INTO ARRAY Sentinel-controlled loop: int main() { int array. Size; cout<< "Enter

INPUT DATA INTO ARRAY Sentinel-controlled loop: int main() { int array. Size; cout<< "Enter the size of array: "; cin >> array. Size; int num[array. Size]; for(int i =0; i<array. Size; i++) { cin >> num[i]; } }

ASSIGNING DATA INTO ARRAY Assigns the letters x, y, and z to the letters

ASSIGNING DATA INTO ARRAY Assigns the letters x, y, and z to the letters array, replacing the letters A, B, C char letters[3] = {‘A’, ‘B’, ‘C’}; //line 1 letters[0] = ‘x’ //line 2 letters[1] = ‘y’ //line 3 letters[2] = ‘z’ //line 4

OUTPUT / PRINTING ARRAY O Print all elements in the array using for loop:

OUTPUT / PRINTING ARRAY O Print all elements in the array using for loop: for(int i=0; i<Array. Size; i++) { cout<<mylist[i]<<endl; } O Print all elements in the array using while loop: int i=0; while(i<array. Size) { cout<<mylist[i]<<endl; i++; }

ARRAY OF CHARACTERS

ARRAY OF CHARACTERS

ARRAY OF CHARACTERS

ARRAY OF CHARACTERS

ARRAY OF CHARACTERS

ARRAY OF CHARACTERS

REFRESH YOUR MEMORY Find the average number among 10 numbers below and list all

REFRESH YOUR MEMORY Find the average number among 10 numbers below and list all numbers that are greater than the average. • 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

STEP TO SOLVE THE PROBLEM 1 4 Ask user to insert 10 numbers 2

STEP TO SOLVE THE PROBLEM 1 4 Ask user to insert 10 numbers 2 Add up all 10 numbers 3 Divide by 10 Show the average number on screen

SOLUTION (WITHOUT ARRAY)

SOLUTION (WITHOUT ARRAY)

SOLUTION (OUTPUT)

SOLUTION (OUTPUT)

SOLUTION (WITH ARRAY)

SOLUTION (WITH ARRAY)

SOLUTION (OUTPUT)

SOLUTION (OUTPUT)

REFRESH YOUR MEMORY Chapter 2 COLUMN DATA TYPE VARIABLE NAME ROW -2 D ARRAY

REFRESH YOUR MEMORY Chapter 2 COLUMN DATA TYPE VARIABLE NAME ROW -2 D ARRAY DECLARATION-

Let’s Do It!

Let’s Do It!

INPUT INTO 2 D ARRAY o Input a single row: • To input data

INPUT INTO 2 D ARRAY o Input a single row: • To input data into row number 4 (fifth row) row = 4; for(col = 0; col < Number_of_Columns; col++) cin >> matrix[row][col]; o Input data into each component of the array: for(row = 0; row < Number_of_Rows; row++) { for(col = 0; col < Number_of_Columns; col++) cin >> matrix[row][col]; }

PRINTING 2 D ARRAY o To print a two-dimensional array, you have to print

PRINTING 2 D ARRAY o To print a two-dimensional array, you have to print each element in the array using a loop like the following: for (int row = 0; row < row. Size; row++) { for (int column=0; column<column. Size; column++) { cout << matrix[row][column] << " "; } cout << endl; }

Array operations using 7 basic algorithms • Min • Max • Count • Total

Array operations using 7 basic algorithms • Min • Max • Count • Total • Average • Sort (bubble) • Search (sequential)

RECORD DECLARATION o A struct is a definition, not a declaration OR struct student.

RECORD DECLARATION o A struct is a definition, not a declaration OR struct student. Type { string first. Name; string last. Name; char course. Grade; int test. Score; int programming. Score; double GPA; //variable declaration } new. Student, student; C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25

RECORD INITIALIZATION o Initialization can be done during declaration. o Example 1: struct student

RECORD INITIALIZATION o Initialization can be done during declaration. o Example 1: struct student stud 1 = {“Nadiah”, “ 880409 -13 -5555”, “A 11000”, 1998}; o Example 2: struct date { int day; int month; int year; } Birth_Day struct date Birth_Day = {1, 1, 1982}; Company Logo 1 day 1 month 1982 year CSC 138 Structured programming

RECORD ASSIGNMENT o The assignment statement: student = new. Student; is equivalent to the

RECORD ASSIGNMENT o The assignment statement: student = new. Student; is equivalent to the following statements: student. first. Name = new. Student. first. Name; student. last. Name = new. Student. last. Name; student. course. Grade = new. Student. course. Grade; student. test. Score = new. Student. test. Score; student. programming. Score = new. Student. programming. Score; student. GPA = new. Student. GPA; C++ Programming: From Problem Analysis to Program Design, Fourth Edition 27

EXERCISE (3. 2. 3) Suppose FSKM has 5 students. We need to calculate the

EXERCISE (3. 2. 3) Suppose FSKM has 5 students. We need to calculate the total score obtained by each student. The total score is by adding the mark of test score and programming score. From the total score, find the course grade of each student. Lastly, display all information of all students. struct student. Type { string first. Name; string last. Name; char course. Grade; int test. Score; int programming. Score; double GPA; int score; } ; student. Type student[5];

ANSWER (3. 2. 3) int main() { for(int i=0; i<5; i++) { cout<<"insert first

ANSWER (3. 2. 3) int main() { for(int i=0; i<5; i++) { cout<<"insert first name : "; cin>>student[i]. first. Name; cout<<"insert last name : "; cin>>student[i]. last. Name; cout<<"insert test score : "; cin>>student[i]. test. Score; cout<<"insert programming score : "; cin>>student[i]. programming. Score; cout<<"insert GPA : "; cin>>student[i]. GPA; } } for(int i=0; i<5; i++) { student[i]. score = student[i]. test. Score+student[i]. programming. Score; if(student[i]. score >=90) student[i]. course. Grade='A'; else if(student[i]. score >=80 && student[i]. score<90) student[i]. course. Grade='B'; else if(student[i]. score >=70 && student[i]. score<80) student[i]. course. Grade='C'; else if(student[i]. score >=60 && student[i]. score<70) student[i]. course. Grade='D'; else student[i]. course. Grade='F'; } for(int i=0; i<5; i++) { cout<<"First Name : “ <<student[i]. first. Name<<endl; cout<<"Last Name : “ <<student[i]. last. Name<<endl; cout<<"course Grade : “ <<student[i]. course. Grade<<endl; cout<<"test Score : “ <<student[i]. test. Score<<endl; cout<<"Programming Score : “ <<student[i]. programming. Score<<endl; cout<<" GPA : “ <<student[i]. GPA<<endl; }

EXERCISE (3. 2. 5) o Suppose that you have the following definitions: struct tour.

EXERCISE (3. 2. 5) o Suppose that you have the following definitions: struct tour. Type { char city. Name[20]; int distance, hour, sec; double min; }; i. ii. iii. iv. v. Declare the variable named destination of type tour. Type. Write C++ statements to store the following data in destination: city. Name - chicago, distance - 550 miles, travel. Time - 9 hours and 30 minutes. Write the definition of a function to output the data stored in a variable of type tour. Type. Write the definition of value –returning function that input data into a variable of type tour. Type. Write the definition of void function with reference parameter of type tour. Type to input data in a variable of type tour. Type.

EXERCISE (3. 2. 6) o The following struct definition consists of four data members:

EXERCISE (3. 2. 6) o The following struct definition consists of four data members: book’s title, serial number, price and shelf location. struct Books { char title[50]; int serial. No; float price; char location[20]; }; a) Create an array of 100 malay books b) Write the function definition for each of the following i. Function input. Data(): This function receives two parameters; an array of malay books and the size of the array. It then reads data from the user and stores them into the array. ii. Function precious. Book(): this function receives two parameters; an array of malay books and the size of the array. It determines the most expensive book and displays all the information about the book.

EXERCISE (3. 2. 7) A bookshop wants to manage the information of the books

EXERCISE (3. 2. 7) A bookshop wants to manage the information of the books sold in their shop. You have been assigned to develop the program to update the books information. The information stored consists of the titles, quantities and prices of books. The title, quantity and price of book are stored in a structure named book. Stock. Assuming that there are FIVE HUNDRED (500) books managed by the bookshop. Write definition for the following functions. Note : function parameter must at least have an array of type book. Stock. o Input. Data() : this function prompts user to enter book information and stores them in array of type book. Stock. o total. Price(): this function calculates total price by summing up multiplication result of quantities and prices. It will then return the result. o display. Restock(): this function displays the details of restocked book by finding its quantity which is less than FIVE (5) books. o search. Title(): this function searches book title and display the book details. If the title is not found, display appropriate message. Searched book title is received through this function parameter.

WRITE DATA INTO FILE(A Complete o Example 1: Program) 33

WRITE DATA INTO FILE(A Complete o Example 1: Program) 33

WRITE DATA INTO FILE(A Complete o Example 1 (output): Program) 34

WRITE DATA INTO FILE(A Complete o Example 1 (output): Program) 34

READ DATA FROM FILE § The ifstream : is used to read primitive data

READ DATA FROM FILE § The ifstream : is used to read primitive data type values, arrays, strings, and objects from a text file. Eg: The program creates an instance of ifstream and reads all lines from the file “test. Marks. txt”. Each line consists of name (a string), test 1 and test 2 (an integer) can be displayed on the screen. in. File. getline(names, 25, '; ') in. File >> test 1 >> test 2; After INPUT from file 35

READ DATA FROM FILE (A Complete Program) #include <fstream. h> //Step 1: Include a

READ DATA FROM FILE (A Complete Program) #include <fstream. h> //Step 1: Include a header file main(){ //Step 2: Declare an input stream variable ifstream in. File; Use getline() function to read character with spaces: ifstream. Variable. Name. getline(attribute. Variable, character. Size, delimiter); char names[25]; double test 1, test 2; //Step 3: Open for an input file in. File. open(“test. Marks. txt”); //Step 4: Read data from an input file - using eof() in. File. getline(names, 25, '; '); in. File >> test 1 >> test 2; while(!in. File. eof()){ If not at end of file, continue reading data cout << names << " " << test 1 << " " << test 2; in. File. getline(names, 25, '; '); in. File >> test 1 >> test 2; } in. File. close(); //Step 5: Close an input file getch(); } 36

READ DATA FROM FILE & STORE INTO ARRAY o Example 1: Suppose that a

READ DATA FROM FILE & STORE INTO ARRAY o Example 1: Suppose that a file names in. Data. txt contains the following data: 10. 20 15. 60 16. 12 5. 35 8. 20 7. 28 o The numbers in each line represents the length and width of a rectangle. Write C++ statements that will read data from in. Data. txt file and store into array rectangle[3][2]. Lastly, display the array onto the screen as the following format:

READ DATA FROM FILE & STORE INTO ARRAY o Example 1:

READ DATA FROM FILE & STORE INTO ARRAY o Example 1:

READ DATA FROM FILE & STORE INTO ARRAY o Example 1:

READ DATA FROM FILE & STORE INTO ARRAY o Example 1:

READ DATA FROM FILE & STORE INTO ARRAY o Example 1 (Output): Notepad :

READ DATA FROM FILE & STORE INTO ARRAY o Example 1 (Output): Notepad : Console (Command Prompt):

READ DATA FROM FILE & STORE INTO ARRAY o Example 2: Extend from the

READ DATA FROM FILE & STORE INTO ARRAY o Example 2: Extend from the same program, calculate the area of rectangle by reading the data in each line and store the results in out. Data. txt.

READ DATA FROM FILE & STORE INTO ARRAY o Example 2:

READ DATA FROM FILE & STORE INTO ARRAY o Example 2:

READ DATA FROM FILE & STORE INTO ARRAY o Example 2 (Output): Notepad :

READ DATA FROM FILE & STORE INTO ARRAY o Example 2 (Output): Notepad :

READ DATA AS RECORD o Example 3: i. Read all the records that have

READ DATA AS RECORD o Example 3: i. Read all the records that have been stored in a file named comp. Stock. txt which consist of: brand, price and operating system. Notepad :

READ DATA AS RECORD o Example 3: ii. All the data will be stored

READ DATA AS RECORD o Example 3: ii. All the data will be stored into a variable struct array named computers. Given the definition of Computer record and declaration of computers array: struct Computer { char brand[20]; double price; int operating //1: Windows 8 //2: Windows 10 }; main() { Computer computers[7]; : }

READ DATA AS RECORD o Example 3:

READ DATA AS RECORD o Example 3:

o Example 3: READ DATA AS RECORD

o Example 3: READ DATA AS RECORD

READ DATA AS RECORD o Example 3 (output): Notepad : Console (Command Prompt):

READ DATA AS RECORD o Example 3 (output): Notepad : Console (Command Prompt):

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4: o Based on the

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4: o Based on the previous program, write a program by using modular programming techniques that find and write all brands of computers using Windows 10 and the total numbers of those computers in an output file named win 10. txt. Console (Command Prompt): Notepad:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4:

PASS FILE VARIABLE INTO FUNCTION (AS PARAMETER) o Example 4: