Lessons from CST exercise - PowerPoint PPT Presentation

About This Presentation
Title:

Lessons from CST exercise

Description:

name w/o [ ] meaning, not name, is. important for value transferred ... name of array alone (without subscript) is the same as the address of its first element ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 18
Provided by: jackp3
Learn more at: https://sites.pitt.edu
Category:
Tags: cst | exercise | lessons | meaning | names | of

less

Transcript and Presenter's Notes

Title: Lessons from CST exercise


1
Engr 0012 (04-1) LecNotes 22-01
2
arrays
collection of values - all of the same type,
e.g., int
individual values referred to as an element of
the array
example 1 4 14 -3 2 8 -2 0 3
an array of type int
an individual element is 14
example This is an array.
an array of type char
an individual element is h
Engr 0012 (04-1) LecNotes 22-02
3
arrays - declaring
// defined constants define MAXARRAYSIZE
100   main() // begin main  // variable
declaration double lengthMAXARRAYSIZE,
// array of lengths
widthMAXARRAYSIZE // array of widths
char filename81 // string array
int lengthused, // actual amt in
use i // loop
index  
Engr 0012 (04-1) LecNotes 22-03
4
arrays - using
each element of an array must be accessed
individually
there are no commands that process an entire
array!!!
single statement can find area for set of
lengths and widths
must process element by element
Engr 0012 (04-1) LecNotes 22-04
5
arrays - using
for (i0 iltlengthused ii1) // begin area
calc areai lengthiwidthi // end
area calc
area3 refers to the 4th element of area
area7 refers to the 8th element of area
areai refers to the (i1)th element of area
array indices always start at 0 (zero)
contrast with MATLAB where indices start at 1
(one)
Engr 0012 (04-1) LecNotes 22-05
6
arrays - using
// defined constants define MAXLENGTH
10 main() // begin main // variable
declaration double lengthMAXLENGTH, //
length array widthMAXLENGTH, //
width array areaMAXLENGTH, //
area array perimMAXLENGTH //
perimeter array int lengthused,
// actual num of lengths i
// loop index
gt You, the programmer, must keep track of
amount in use and pass the amount in use as a
parameter to functions.
Engr 0012 (04-1) LecNotes 22-06
7
arrays - initialization
// defined constants define MAXSIZE
5 main() // begin main // variable
declaration double a3 8, 9, //
double array b 4, 5, 6, //
double array c5, //
double array dMAXSIZE //
double array int i //
loop index // algorithm printf( "\na0
f, a1 f, a2 f" "\n\nb0
f, b1 f, b2 f", a0,
a1, a2, b0, b1, b2 )
a0 8.000000, a1 9.000000, a2
0.000000 b0 4.000000, b1 5.000000, b2
6.000000
Engr 0012 (04-1) LecNotes 22-07
8
arrays - initialization
// defined constants define MAXSIZE
5 main() // begin main // variable
declaration double a3 8, 9, //
double array b 4, 5, 6, //
double array c5, //
double array dMAXSIZE //
double array int i //
loop index // algorithm for (i0 ilt3
ii1) // begin for ci 2i
// end for printf( "\n\nc0 f, c1
f, c2 f" "\nc3 f \nc4
f", c0, c1, c2, c3, c4 )
c0 0.000000, c1 2.000000, c2
4.000000 c3 0.000000 c4
23536894931698073000000000000000000000000000000000
00000000000000000000000 00000000000000000000000000
00000000000000000000000000000000000000000000000000
0000 000000000000000000000000000000000000000000000
00000000000000000000000000000000000
Engr 0012 (04-1) LecNotes 22-08
9
arrays - initialization
// defined constants define MAXSIZE 5 //
prototypes void get_d_from_file( double d,
int pdused ) main() // begin main //
variable declaration double a3 8, 9,
// double array b 4, 5, 6, //
double array c5, //
double array dMAXSIZE //
double array int i, dused //
loop index // algorithm get_d_from_file(
d, dused )
Engr 0012 (04-1) LecNotes 22-09
10
arrays - use
simple assignment statements
Engr 0012 (04-1) LecNotes 22-10
11
arrays - use
avelength 0.0 for( i 0 i lt actsize i i1
) // begin for loop avelength
avelength lengthi // end for
loop avelength avelength/actsize
computes the average of an array
Engr 0012 (04-1) LecNotes 22-11
12
arrays - use
for( j 0 j lt actsize j j1 ) //
begin for loop lengthj 3j12
// end for loop
assigns a value to each element of array length
Engr 0012 (04-1) LecNotes 22-12
13
arrays - use
nonsense length3intvalue2
how many ways can we produce an integer value?
Engr 0012 (04-1) LecNotes 22-13
14
arrays - function prototypes/parameters
// preprocessor commands define MAXARRAYSIZE
100   main()   // variable declaration
double lengthMAXARRAYSIZE, // array of
lengths widthMAXARRAYSIZE //
array of widths int actualsize,
// actual amt in use i
// loop index  
// function prototypes void getarray( double
length , int pactsize ) double avearray(
double length , int actsize )  
Engr 0012 (04-1) LecNotes 22-14
15
arrays - function prototypes/parameters
// preprocessor commands define MAXARRAYSIZE
100 // function prototypes void getarray(
double length , int pactsize ) double
avearray( double length , int actsize )
  main()   // variable declaration
double lengthMAXARRAYSIZE, // array of
lengths widthMAXARRAYSIZE //
array of widths int actualsize,
// actual amt in use i
// loop index
// calling functions getarray( length,
actualsize ) ave avearray( length,
actualsize ) 
Engr 0012 (04-1) LecNotes 22-15
16
arrays - addresses
lengthk
printf( "\nEnter next length gt " ) scanf(
"lf", lengthj )
\\ prototypes double prod( double plen, double
pwid ) \\ algorithm area prod(
lengthi, widthi )
Engr 0012 (04-1) LecNotes 22-16
17
arrays - addresses
name of array alone (without subscript) is the
same as the address of its first element
length ltgt length0
Engr 0012 (04-1) LecNotes 22-17
Write a Comment
User Comments (0)
About PowerShow.com