Programming with Arrays - Computing in Engineering and Science - Lab Slides, Slides of Computer Science

This lecture is from lab session of Computing in Engineering and Science course. This is basic subject for computer science students. Though it is helpful for everyone who study about programming. Some key words in this lecture are: Programming with Arrays, Input Function, Grouping Data, Mean and Standard Deviation, Finding Max and Min, Exercise, Loop

Typology: Slides

2013/2014

Uploaded on 02/01/2014

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercise 8 – Programming with arrays April 27, 2006
1
Exercise VIII (and last)
Exercise VIII (and last)
Programming with Arrays
Programming with Arrays
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
April 27, 2006
2
Outline
Goals for exercise eight
Specific tasks for exercise eight
Code structure for exercise eight
3
Exercise Eight Goals
Write program using arrays
Write programs that computes statistical
functions of mean and standard
deviation
Introduce concept of linear regression
as background for project three
4
Exercise Eight Task One
Four files to download for this exercise
Notes, task one program, task two data
file, and task two answers
Task one program is copy and paste
Prepare data file shown in exercise that
has five data points (xi, yi)
Run regression program on your data file
and verify results
View code to see how arrays are read
from data file and used in calculations
5
Exercise Eight Task One Code
Main function only calls other functions
Input function reads data
Function is called in an if statement that
returns a value of true if there is no error in
data input, false otherwise
Input returned as pass-by-reference data
Regression function does calculations
Output function provides output
You can, but do not have to follow this
structure in task two
6
Input Function
Prototype
bool getInputOK( double x[],
double y[], int& n );
Function call
if ( !getInputOK( x, y, n) )
return EXIT_FAILURE;
Function call returns a value of true if
the input is okay and false if it is not
pf3

Partial preview of the text

Download Programming with Arrays - Computing in Engineering and Science - Lab Slides and more Slides Computer Science in PDF only on Docsity!

Exercise VIII (and last)Exercise VIII (and last) ––

Programming with ArraysProgramming with Arrays

Larry Caretto Computer Science 106

Computing in Engineering

and Science

April 27, 2006

2

Outline

  • Goals for exercise eight
  • Specific tasks for exercise eight
  • Code structure for exercise eight

3

Exercise Eight Goals

  • Write program using arrays
  • Write programs that computes statistical functions of mean and standard deviation
  • Introduce concept of linear regression as background for project three

4

Exercise Eight Task One

  • Four files to download for this exercise
    • Notes, task one program, task two data file, and task two answers
  • Task one program is copy and paste
    • Prepare data file shown in exercise that has five data points (xi , yi )
    • Run regression program on your data file and verify results
  • View code to see how arrays are read from data file and used in calculations

5

Exercise Eight Task One Code

  • Main function only calls other functions
  • Input function reads data
    • Function is called in an if statement that returns a value of true if there is no error in data input, false otherwise
    • Input returned as pass-by-reference data
  • Regression function does calculations
  • Output function provides output
  • You can, but do not have to follow this structure in task two 6

Input Function

  • Prototype bool getInputOK( double x[], double y[], int& n );
  • Function call if ( !getInputOK( x, y, n) ) return EXIT_FAILURE;
  • Function call returns a value of true if the input is okay and false if it is not

7

Input Function II

  • Input function reads data from file that

tests for end of file or exceeding the maximum array bounds

  • Reads input into temporary variables and then copies these into array variables if valid data are read
  • Uses inFile.fail() and inFile.good() func-

tions that we have discussed previously

8

Input Function III

int n = 0; do { inFile >> xin >> yin; if ( inFile.fail() ) break; x[n] = xin; y[n] = yin; n++; // n = data number } while ( n < MAX_DATA );

Global parameter

9

Exercise Eight Task Two

  • Download data file with sets of data pairs (xi, yi)
  • Find data count, average x and y, maxi- mum and minimum x and y and standard deviation for x and y - For entire data set - For data in which x >= 1000 - For data in which x < 1000 - Group y data based on the corresponding x values of x[i], y[i] data pairs 10

Grouping Data

  • Group data pairs into two subsets based on x values

345 2000 xi < 1000

1000 2 xi 1000

678 9 xi < 1000

1234 5 xi 1000

xi yi Group

11

Mean and Standard Deviation

1 2

0

1

0

2

=

=

N

x

N

x

s

N

i

i

N

i

i

=

1

0

1 N

i

xi

N

x

  • Have seen calculations previously
  • Can compute sums for means of x and y and their standard deviations in one loop
  • Can also do min and max calculations in the same loop

12

Exercise Eight Task Two Hints

  • Use input function from task one code without change to read data
  • Review task one code for hints on how to perform array calculations
  • You may, but you do not have to use the task-one code structure with three functions
  • You can do the same calculation tasks (count, mean, standard deviation, max and min) three times in a single function