Example 2. Young Won Lim 11/24/17

Similar documents
Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Example 1. Young Won Lim 11/17/17

Example 3 : using a structure array. Young Won Lim 11/25/17

Example 3. Young Won Lim 11/22/17

Example 1 Using 1-d Arrays

Example 3 Using a Structure Array

Applications of Structures (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 11/1/17

Applications of Structures (1A) Young Won Lim 12/8/17

Overview (1A) Young Won Lim 9/14/17

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 1/9/18

Applications of Pointers (1A) Young Won Lim 12/26/17

Overview (1A) Young Won Lim 9/9/17

Overview (1A) Young Won Lim 9/25/17

Function Overview (1A) Young Won Lim 10/23/17

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Pointers (1A) Young Won Lim 3/5/18

Arrays (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 1/14/18

Applications of Arrays (1A) Young Won Lim 2/11/17

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Structure (1A) Young Won Lim 7/30/13

Applications of Arrays (1A) Young Won Lim 3/15/17

Applications of Pointers (1A) Young Won Lim 4/24/18

Pointers (1A) Young Won Lim 1/22/18

Arrays (1A) Young Won Lim 1/27/17

Applications of Pointers (1A) Young Won Lim 2/27/18

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

Memory (1A) Young Won Lim 9/7/17

Applications of Pointers (1A) Young Won Lim 3/31/18

Applications of Pointers (1A) Young Won Lim 1/5/18

Applications of Pointers (1A) Young Won Lim 4/11/18

Pointers (1A) Young Won Lim 2/10/18

Pointers (1A) Young Won Lim 10/18/17

Pointers (1A) Young Won Lim 2/6/18

Structures (1A) Young Won Lim 11/8/16

Applications of Array Pointers (1A) Young Won Lim 11/22/18

Pointers (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 10/23/17

Structures (1A) Young Won Lim 12/4/17

File (1A) Young Won Lim 11/25/16

Type (1A) Young Won Lim 2/17/18

ELF (1A) Young Won Lim 10/22/14

Number System (1A) Young Won Lim 7/7/10

Variables (2D) Young Won Lim 3/28/18

Accessibility (1A) Young Won Lim 8/22/13

Arrays and Strings (2H) Young Won Lim 3/7/18

Program Structure (2A) Young Won Lim 3/8/18

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Arrays (2) Applications

Pointers (2G) Young Won Lim 3/7/18

Program Structure (2A) Young Won Lim 5/28/18

Expressions (2E) Young Won Lim 4/9/18

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Expressions (2E) Young Won Lim 3/10/18

Class (1A) Young Won Lim 9/8/14

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

RAM (1A) Young Won Lim 11/12/13

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Sorting and Searching. Sudeshna Sarkar 7 th Feb 2017

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Computer Science & Engineering 150A Problem Solving Using Computers

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Functions (4A) Young Won Lim 5/8/17

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Functions (4A) Young Won Lim 3/16/18

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Polymorphism (1A) Young Won Lim 8/22/13

Structures (2I) Young Won Lim 4/17/18

ELF (1A) Young Won Lim 3/24/16

C Programming Language

Polymorphism (1A) Young Won Lim 8/15/13

Binary Search Tree (3A) Young Won Lim 6/2/18

CS 108 Computing Fundamentals. October/November Array Bootcamp

Structure (1A) Component

Binary Search Tree (2A) Young Won Lim 5/17/18

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Methods (2A) Young Won Lim 10/14/14

Advanced Pointer Topics

Structures (2I) Young Won Lim 3/7/18

CORDIC Fixed Point Simulation. Young Won Lim 2/28/12

State Monad (3D) Young Won Lim 9/25/17

Tiny CPU Data Path (2B) Young Won Lim 5/2/16

Algorithms (7A) Young Won Lim 4/10/17

Searching an Array: Linear and Binary Search. 21 July 2009 Programming and Data Structure 1

Side Effects (3B) Young Won Lim 11/20/17

CS 101, Spring 2017 Apr 11th Exam 3

Side Effects (3B) Young Won Lim 11/23/17

Algorithms (7A) Young Won Lim 4/18/17

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Fundamental of Programming (C)

Side Effects (3B) Young Won Lim 11/27/17

Using pointers with functions

Transcription:

Copyright (c) 2010-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Using 2-d arrays #include <stdio.h> #include <stdlib.h> #define SIZE 10 3

avg3() definition //-------------------------------------------------- // Calculating the average of three numbers //-------------------------------------------------- double avg3(int x, int y, int z) { return (x+y+z) / 3.; 4

init_arrays() definition //-------------------------------------------------- // Initialize X[4][SIZE] arrays // by assigning random number grade //-------------------------------------------------- void init_arrays (int X[][SIZE], double A[]) { int i; // srand(7) makes rand() generate // the same random sequence // --> easy to debug a program srand(7); for (i=0; i<size; ++i) { X[0][i] = i+1 + 201600; // I X[1][i] = rand() % 101; // K X[2][i] = rand() % 101; // E X[3][i] = rand() % 101; // M A[i] = avg3(x[1][i], X[2][i], X[3][i]); 5

pr_table() definition //-------------------------------------------------- // Print the original table //------------------------------------------------- void pr_table (int X[][SIZE], double A[]) { int i; printf("%10s %10s %10s %10s %10s \n", "StID", "Korean", "Enlgish", "Math", "Average"); for (i=0; i<size; ++i) { printf("%10d %10d %10d %10d %10.2f \n", X[0][i], X[1][i], X[2][i], X[3][i], A[i]); 6

DbubbleSort() definition //-------------------------------------------------- // Bubble Sort Double Array //------------------------------------------------- void DbubbleSort(double a[], int size) { int p, j; double tmp; for (p=1; p< size; ++p) { for (j=0; j< size-1; ++j) { if ( a[j] < a[j+1] ) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; 7

pr_sorted_table() definition //-------------------------------------------------- // Print the Sorted Table //------------------------------------------------- void pr_sorted_table (int X[][SIZE], double A[]) { int i, j; double B[SIZE]; // Backup Array for Sorting for (i=0; i<size; ++i) B[i] = A[i]; //... DbubbleSort(B, SIZE); //... printf("\n\nsorted on a student's average\n\n"); printf("%10s %10s %10s %10s %10s \n", "StID", "Korean", "Enlgish", "Math", "Average"); for (i=0; i<size; ++i) { for (j=0; j<size; ++j) if (B[i] == A[j]) break; printf("%10d %10d %10d %10d %10.2f \n", X[0][j], X[1][j], X[2][j], X[3][j], A[j]); 8

Avg() definition //-------------------------------------------------- // Average over Integer Array //------------------------------------------------- double Avg(int M[], int n) { int i; double S=0.0; for (i=0; i<n; ++i) S+= M[i]; return S/n; 9

DAvg() definition //-------------------------------------------------- // Average over Doubl Array //------------------------------------------------- double DAvg(double N[], int n) { int i; double S=0.0; for (i=0; i<n; ++i) S+= N[i]; return S/n; 10

pr_average() definition //-------------------------------------------------- // Print the Averages //------------------------------------------------- void pr_averages(int X[][SIZE], double A[]) { double A1 = Avg(X[1], SIZE); double A2 = Avg(X[2], SIZE); double A3 = Avg(X[3], SIZE); double A4 = DAvg(A, SIZE); printf("%10s %10.2f %10.2f %10.2f %10.2f \n", "Average", A1, A2, A3, A4); 11

main() definition //==================================================== // main //==================================================== int main(void) { // X[0][SIZE] --> I[SIZE]; // ID of a student // X[1][SIZE] --> K[SIZE]; // Grade of Korean // X[2][SIZE] --> E[SIZE]; // Grade of English // X[3][SIZE] --> M[SIZE]; // Grade of Math int X[4][SIZE]; double A[SIZE]; // Average of a student init_arrays(x, A); pr_table(x, A); pr_sorted_table(x, A); pr_averages(x, A); 12

2-d Array Definition Student ID Korean English X[i][j] Math Average A[j] 13

Row-by-Row : Row Major Ordering 14

Row and Column Index j i X[i][j] i j X[j][i] 15

init_arrays() - filling scores Korean English Math 76 86 44 98 97 X[i][j] Random Number 16

init_arrays() - computing averages i Student ID i+1 + 201600 Korean English X[j][i] Math Average avg3() avg3() avg3() avg3() avg3() avg3() avg3() avg3() A[i] double avg3 (int x, int y, int z) ; A[i] = avg3(x[1][i], X[2][i], X[3][i]) 17

pr_table() Student ID Korean English Math Average I K E M A i 18

pr_sorted_table copying A to B Student ID Korean English X[j][i] Math Average A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[i] Average2 B[0] B[1] B[2] B[3] B[4] B[5] B[6] B[7] B[i] First, copy A[i] into B[i] 19

pr_sorted_table sorting B Student ID Korean English X[j][i] Math Average A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[i] Average2 B[0] B[1] B[2] B[3] B[4] B[5] B[6] B[7] B[i] Decreasing after DbubbleSort() B[j] > B[j+1] A, B: different order 20

pr_sorted_table printing by B Student ID Korean English Math X[0][j] X[1][j] X[2][j] X[3][j] j Average A[0] A[1] A[j] Average2 B[0] B[1] B[2] B[3] B[4] B[5] B[6] B[7] B[i] i Search A[j] = B[i] Assume that two averages have always different values 21

pr_averages Student ID Korean English Math Avg() Avg() Avg() X[j][i] Average DAvg() A[i] double Avg(int X[], int n); double DAvg(double Y[], int n); double A1 = Avg(X[1], SIZE); double A2 = Avg(X[2], SIZE); double A3 = Avg(X[3], SIZE); double A4 = DAvg(A, SIZE); 22

Function Prototypes and Function Calls double avg3 (int x, int y, int z) ; void init_arrays (int X[][SIZE], double A[]); void pr_table (int X[][SIZE], double A[]); void DbubbleSort (double a[], int size); void pr_sorted_table (int X[][SIZE], double A[]); double Avg (int X[], int n); double Davg (double Y[], int n); void pr_averages (int X[][SIZE], double A[]); init_arrays(x, A); in main() A[i] = avg3(x[1][i], X[2][i], X[3][i]). in init_arrays() pr_table(x, A);. in main() pr_sorted_table(x, A);.. in main() DbubbleSort(B, SIZE);. in pr_sorted_table() pr_averages(x, A);. in main() double A1 = Avg(X[1], SIZE); in pr_averages() double A2 = Avg(X[2], SIZE); in pr_averages() double A3 = Avg(X[3], SIZE); in pr_averages() double A4 = DAvg(A, SIZE);.. in pr_averages() A[i] = avg3(x[1][i], X[2][i], X[3][i]); 23

2-d Array Definitions int main(void) { // X[0][SIZE] --> I[SIZE]; // ID of a student // X[1][SIZE] --> K[SIZE]; // Grade of Korean // X[2][SIZE] --> E[SIZE]; // Grade of English // X[3][SIZE] --> M[SIZE]; // Grade of Math int X[4][SIZE]; double A[SIZE]; // Average of a student init_arrays(x, A); pr_table(x, A); pr_sorted_table(x, A); pr_averages(x, A); 24

2-d Array Definition Student ID Korean English X[i][j] Math Average A[j] 25

int X[ ][SIZE] : Formal Parameter X SIZE Student ID Korean English X[i][j] Math int X[4][SIZE]; X Init_arrays( int X[ ][SIZE], ) pr_table( int X[ ][SIZE], ) pr_sorted_table( int X[ ][SIZE], ) pr_averages( int X[ ][SIZE], ) 26

X[1], X[2], X[3] : 2 nd, 3 rd, 4 th rows X SIZE Student ID Korean English X[i][j] Math X[1] can be used as a 1-d array X[2] x[1][0] x[1][1] x[1][2] x[1][3] x[1][4] x[1][5] x[1][6] x[1][7] X[3] x[2][0] x[2][1] x[2][2] x[2][3] x[2][4] x[2][5] x[2][6] x[2][7] x[3][0] x[3][1] x[3][2] x[3][3] x[3][4] x[3][5] x[3][6] x[3][7] 27

int X[ ] : Formal Parameter X[1] X[2] X[3] int X[4][SIZE]; X[1] X[2] X[3] Avg( int X[ ], ) 28

double Y[ ] : Formal Parameter A int A[SIZE]; A DAvg( double Y[ ], ) 29

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun [5] cprogramex.wordpress.com