Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parallel Arrays? ANSI-C.

Similar presentations


Presentation on theme: "Parallel Arrays? ANSI-C."— Presentation transcript:

1 Parallel Arrays? ANSI-C

2 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.

3 Example Input Table1 Table 2 123444 85 123444 85 345660 95
Sorted in increasing order Sorted in decreasing order

4 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]= grade[0]= 85 id[1]= grade[1]= 90 id[2]= grade[2]= 65 id[3]= grade[3]= 76 id[4]= grade[4]= 95

5 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.

6 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.

7 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!!!!

8 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.

9 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!

10 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);

11 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.

12 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.

13 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);

14 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);

15 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*/


Download ppt "Parallel Arrays? ANSI-C."

Similar presentations


Ads by Google