CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS150 Introduction to Computer Science 1
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
CS 2430 Day 1. Agenda Load NetBeans Introduction Syllabus Review some array operations.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
Variables and memory addresses
Data Types Storage Size Domain of all possible values Operations 1.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
One-Dimensional Array Introduction Lesson xx
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Presentation transcript:

CS 1430: Programming in C++

Declaration of Variables int scoreCount; float score, total; char grade; string courseName; bool validInput; // Memory Allocation // Address for each variable // Size for each data type // int : 2 bytes // float : 4 bytes // char : 1 byte // string: # of chars plus one

Computing Highest, Lowest, Average float score; float total, max, min, avg; int scoreCount; // Using one variable to input and process scores // One score at a time // When do we need to keep all scores? // Is my score below or above the average? // How do we keep all scores? // Array!

Declaration of Arrays float scores[100]; // 100 floats char chs[20]; // 20 chars int nums[30]; // 30 integers string allNames[100]; // 100 strings bool prog3Completed[25]; // 25 bool values

Memory Allocation for Array int nums[30]; // 30 integers // Address: the first element // How many bytes? // 30 * 2: 60 bytes nums 10 15 20 50 . . . . 30 18 5

Array Size Must Be Constant Expressions const int MAX_SIZE = 30; int size = 30; int nums[30]; float ArrayA[MAX_SIZE]; // Valid? // Yes! // number of bytes? float ArrayB[size]; // NO! float ArrayC[MAX_SIZE * 10]; //Valid?

Array Index and Array Element How to access array elements? Use array index to specify array element Index of any array begins with 0

Array Element and Array Index int nums[30]; Array Element 10 15 20 50 . . . . 30 18 0 1 2 3 28 29 Array Indices Array Element nums[0] nums[1] nums[29]

Operations on Array Elements An array element is the same as a single variable Operations on array elements: Input Output Arithmetic operations Assignment Comparison Function parameters …

int nums[30], size; // Read an integer and store it in the 1st element of nums. cin >> nums[0]; // Incement the 1st element of nums by one. nums[0] ++; // Assign the value of the 1st element of nums to // the 2nd element of nums. nums[1] = nums[0]; cin >> size; // Incement the 2nd element of nums by the value of size. nums[1] += size; // Set the last element of nums to the value of size. nums[29] = size; // Assign the value of the 16th element of nums to size. size = nums[15];

int nums[30]; // Display 1st element of nums with a width of 9 int nums[30]; // Display 1st element of nums with a width of 9. cout << endl << setw(9) << nums[0]; // Compute the average of the 1st and last elements of // nums and store it in avg. avg = (nums[0] + nums[29]) / 2.0; // If the 2nd element is not zero, display the float // quotient of 1st element divided by 2nd. if (nums[1] != 0) cout << "Quotient = " << float(nums[0]) / nums[1]; // Call function Largest to find the max value between // the first three elments of nums and store it in Max. // Function prototype: // int Largest(int num1, int num2, int num3); Max = Largest(nums[0], nums[1], nums[2]);

const int MAX_SIZE = 30; int nums[MAX_SIZE], index; cin >> nums[29]; // Valid? cin >> nums[30]; // assuming all array elements have a value cout << nums[MAX_SIZE - 1]; cout << nums[MAX_SIZE]; cin >> index; if (index < MAX_SIZE && index >= 0) nums[index] = nums[10];

Initializing Arrays in Declarations int ages[5] = {23, 19, 33, 45, 60}; float temperature[] = {0.0, 112.37, -12, 98.6}; // May or may not work

Input n Numbers to an Array int ArrayA[30]; cin >> ArrayA[0]; cin >> ArrayA[1]; cin >> ArrayA[2]; cin >> ArrayA[3]; ... cin >> ArrayA[29]; cin >> ArrayA[30]; // OK?

Use while Loop to Process Array const int MAX_SIZE = 30; int ArrayA[MAX_SIZE]; int size = 20; int index = 0; // Loop 20 times while (index < size) { cin >> ArrayA[index]; index ++; } //Count-Controlled Loop

Use For Loops to Process Arrays cin >> size; // Assume size in the range // Input to an array for (int i = 0; i < size; i ++) cin >> ArrayA[i]; // Same as while loop // Must Use For Loops for Arrays! cin >> size; // Assume in the range // Input to an array int i = 0; while (i < size) { cin >> ArrayA[i]; ++ i; } // Four parts of a loop!

Use For Loops to Process Arrays Initialize Test // Must use For loop for arrays! for (int i = 0; i < size; i ++) cin >> ArrayA[i]; How many times is the body executed? size times! (Zero times if size <= 0) Body Update

Use For Loops to Process Arrays int ArrayA[30], size, j; cin >> size; // Assume size is between 1 and 30 // Input to an array for (int i = 0; i < size; i ++) cin >> ArrayA[i]; // Output array for (j = 0; j < size; j ++) cout << endl << setw(8) << ArrayA[j];

Use For Loops to Process Arrays int ArrayA[30], size, j; cin >> size; for (int i = 0; i < size; i ++) cin >> ArrayA[i]; // Compute total , max, min // The range is known: between 0 and 60, inclusive int min = 60; int max = 0; int total = 0; for (j = 0; j < size; j ++) { total += ArrayA[j]; if (ArrayA[j] > max) max = ArrayA[j]; if (ArrayA[j] < min) min = ArrayA[j]; }

Use For Loops to Process Arrays int ArrayA[30], size; cin >> size; // size > 0 and size <= 30 for (int i = 0; i < size; i ++) cin >> ArrayA[i]; // Compute total , max, min // The range is NOT known. int min = ArrayA[0]; int max = ArrayA[0]; int total = ArrayA[0]; for (int j = 1; j < size; j ++) { total += ArrayA[j]; if (ArrayA[j] > max) max = ArrayA[j]; if (ArrayA[j] < min) min = ArrayA[j]; }

Never go out of the range of an array const int MAX_SIZE = 30; int ArrayA[MAX_SIZE]; for (int i = 1; i <= MAX_SIZE; i ++) cin >> ArrayA[i]; // Out of Range! 10 15 20 50 . . . . 30 18 0 1 2 3 28 29

Schedule Quiz5-1 (1 point) Due: 10 PM Monday Lab 5 5 points by 5 PM Thursday (Pals on Thursday) (Lab Assistant Wed night) Program 3 No Class on Friday!