Parallel Arrays? ANSI-C.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Programming and Data Structure
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CPS120: Introduction to Computer Science Functions.
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Review 1 List Data Structure List operations List Implementation Array Linked List.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
Functions + Overloading + Scope
Chapter 7: User-Defined Functions II
CSE 220 – C Programming Pointers.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computer Programming BCT 1113
Pointers and Pointer-Based Strings
Chapter 7: Arrays.
Lecture2.
Values – also known as scalars (eg. int, double, char, float)
Lecture 6 C++ Programming
User-Defined Functions
Chapter 8: Collections: Arrays
IDENTIFIERS CSC 111.
Reference parameters (6.2.3)
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
6 Chapter Functions.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Parameter Passing in Java
7 Arrays.
CS150 Introduction to Computer Science 1
ECE Application Programming
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Fundamental Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Parallel Arrays? ANSI-C

Problem you are supposed to produce two tables for the final grades of students; first table displays the final numeric grade of students in increasing order of ID; the second table displays the final grade in decreasing order of grade. Display the result of each sort by printing the student ID and the grade. Input: per line of input you have two non-negative integer numbers; the first the ID, the second the numeric grade of the student. There will be at most 25 student (ID, grade) pairs.

Example Input Table1 Table 2 123444 85 123444 85 345660 95 123444 85 123444 85 345660 95 534332 90 345660 95 534332 90 666901 65 432166 76 123444 85 432166 76 534332 90 432166 76 345660 95 666901 65 666901 65 Sorted in increasing order Sorted in decreasing order

Data representation Need two int arrays of the same size, one to store the ID, one to store the grade. int id[25]; int grade[25]; We must coordinate the reading of data: if a student’s id is place in id[i], the grade must be store in the same index value grade[i]. Using the input data: id[0]= 123444 grade[0]= 85 id[1]= 534332 grade[1]= 90 id[2]= 666901 grade[2]= 65 id[3]= 432166 grade[3]= 76 id[4]= 345660 grade[4]= 95

Synchronization of tables We must maintain that synchronization of id and grade per student throughout the program. In particular when we interchange two ID’s, we must also interchange the two grades. So, in sorting when we write: interchange(id, i, k); We must also write after that statement: interchange(grade, i, k); We must also do the same when we interchange grades. Homework: write such program.

Functions of with multiple results. As we have seen functions either: Return one value (of types int, float, double, char) Return no value, its return type is void. How to write functions to return more than one value? Examples: Given a double number: Find its whole part and its decimal part. Given an integer number between 1 and 100: Find max number of 20’s, 10’s, 5’s, and 1’s Given int numbers x1, y1, x2, y2, with each x, y pair representing a point on a line, Find the mid point between those two points.

Given values x1, and x2 It must give us: The computation should result in having x1 with the addition of x1 and x2, and x2 with the multiplication of x1 and x2 If we call the function arrange, and having x1=5, x2=7, printf(“x1= %d, x2 = %d”, x1, x2); arrange(x1, y1); It must give us: x1 = 5, y2 = 7 x1 = 12, y2 = 35 Not only arrange must produce two results, the function must modify the arguments!!!!

Parameter passing mechanism revisited Problem at hand: How to return more than two values from a function? How to modify the argument to a function? Review: For types such as int, float, double, char, C offers pass-by-value: argument’s value is used to initialize the parameter by copying its value. Parameter cannot modify argument in this case.

Reference parameter mechanism Both problems have the same solution: What we need is to specify in a function that a parameter is NOT initialized with the argument’s value, but with the argument’s ADDRESS!!! When a parameter has the address of an argument, it has its value and it can modify it! We call this parameter mechanism: pass-by-address or pass-by-reference!

Syntax: specification of reference parameter When a parameter is to be specified to contain the address of its argument (not its value): we prepend the symbol “*” to the name of the parameter. Example: void arrange( int *val1, int *val2); And when a call to arrange is to be made with arguments x1, x2, we pass their addresses: arrange(&x1, &x2);

Manipulation of parameter In the prototype: void arrange( int *val1, int *val2); val1 and val2 stand for addresses to int variables. To get the value at that address we must write *val1 or *val2. We call this dereferencing. void arrange( int *val1, int *val2){ int temp = *val1; *val1 = temp + *val2; *val2 = temp * (*val2); } Notice: using this parameter passing mechanism Not only: We can modify argument’s values We can return more than one result from a function at once.

Code examples void break( double number, int *whole, double *fraction){ *whole = (int) number; *fraction = number – whole; } double value; int wholePart; double fractionPart; … break(value, &wholePart, &fractionPart); Rule: Parameters specified to be pass-by-reference must be matched by the address of the arguments.

Code examples void moneyUnits( int dollars, int *ones, int *fives, int *tens, int *twenties){ *twenties = dollars/20; dollars = dollars%20; *tens = dollars/10; *ones = dollars%10; } int money; int onesInMoney; int fivesInMoney; int tensInMoney; int twentiesInMoney; … moneyUnits(money, &onesInMoney, &fivesInMoney, &tensInMoney, &twentiesInMoney);

Code example /* Given 2 endpoints of a line, “return” coordinates of midpoint */ void midpoint( double x1, double y1, double x2, double y2, double *midxp, double *midyp { *midxp = (x1 + x2) / 2.0; *midyp = (y1 + y2) / 2.0; } double ax, ay, bx, by, mx, my; ... midpoint(ax, ay, bx, by, &mx, &my);

Parameter and Arguments Parameter Argument Pass-by-value literal, var, any expression of the corresponding type. Pass-by-address address of a variable Common error: given double fun( int *var); fun(aVar); /* no address passed*/ fun(54); /* must be the address of a variable*/