11/5/2016cs150 introduction to computer science 1 announcements assignment 6 due on wednesday,...

14
08/24/22 CS150 Introduction to Computer Science 1 1 Announcements Assignment 6 due on Wednesday, December 3, 2003 Final Exam on Tuesday, December 9, 2003 at 3pm

Upload: jemima-powell

Post on 21-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

1

Announcements

Assignment 6 due on Wednesday, December 3, 2003

Final Exam on Tuesday, December 9, 2003 at 3pm

Page 2: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

2

Multidimensional Array

A class contains 3 students and each of these students have taken 4 tests. Write a program that will read in the students grades from a file, then print them to the screen along with the largest and smallest grades overall and the average grade of each student.

[0] [1] [2] [3]Student[0] 77 68 86 73Student[0] 96 87 89 78Student[0] 70 90 86 81

The largest grade is: 96The smallest grade is: 68The average grade for student 0 is 76.00The average grade for student 1 is 87.50The average grade for student 2 is 81.75

Page 3: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

3

Arrays and Data Types

Useful for storing a collection of data elements of the same data type (float, int, string).

char myName[5]; //All elements chars

float salaries[NUM_EMP]; //All elements floats

char vowels[]={‘A’,’E’,’I’,’O’,’U’};

int matrix[3][5];

What about storing a collection of data elements of different data types?

Page 4: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

4

Data with Different Data Types For example, what if we wanted to keep the following information on

a particular employee:

employee id SS# number of children salary citizen

The elements have different data types, so we can’t conveniently use an array. Instead we will use a struct ( short for structure).

Page 5: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

5

Structure Definition

To store this information:

employee id SS# number of children salary citizen

We would begin by defining a structure :

struct employ

{

int id

int ssnum;

int numchild;

float salary;

bool citizen;

};

Page 6: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

6

Struct Terminology

For this struct:

struct employ

{

int id

int ssnum;

int numchild;

float salary;

bool citizen;

};

employ is the identifier name and a NEW data type.

The individual components id, ssnum, etc. are called members.

Page 7: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

7

Struct Declaration

As with all data types, in order to use our new data type employ we must allocate storage space by declaring variables of this data type:

employ engineer,tech;

This will allocate space for two variables called engineer and tech with the previously described members id, ssnum, etc.

Page 8: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

8

Member Access Operator

To access a struct member, we use the member access operator (period between struct variable name and member name).

In the variable engineer of data type employ we can make the assignments:

engineer.id = 12345;

engineer.ssnum = 534334343;

engineer.numchild = 2;

engineer.salary = 45443.34;

engineer.citizen = true;

How do we access the data in arrays?

Page 9: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

9

Example One

Write a C++ struct data type realnum that will have members number, realpart, and intpart.

Declare a variable numinfo of that type.

Place the value 3.14159 in the field number.

Page 10: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

10

Example One Solution

struct realnum

{

float number;

int realpart;

int intpart;

};

realnum numinfo;

numinfo.number=3.14159;

Define struct realnum

Declare variable numinfo

Assign member number

Page 11: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

11

Structs as function arguments Structs can be passed to functions by reference or value

in the same manner that other data types have been passed.

Generally, passing structs by reference is preferred since

passing by value requires a local copy of the struct be created within the function’s variables.

Page 12: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

12

Example Two

Write a C++ function split that accepts a variable of type realnum.

Assign the integer part of the number to the member variable intpart and the real part of the number to the member variable realpart.

Page 13: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

13

Example Two Solution

Function prototype:

void split(realnum &);

Function call:

split (numinfo);

Function definition:

void split(realnum & numberinfo)

{

// Use numberinfo.number,numberinfo.intpart,

// and numberinfo.realpart.

}

Page 14: 11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,

04/21/23 CS150 Introduction to Computer Science 1

14

Example Three

Consider the following struct data type:

struct info

{

int num;

int divisors[10];

int howmany;

};

Write a C++ function compute that accepts a variable of type info and returns all the divisors greater than 1 of the variable num in the array divisors and the number of divisors in the variable howmany.