Arrays Part-1 Armen Keshishian.

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright © 2012 Pearson Education, Inc. 16/4/1435 h Sunday Lecture 3 1.Using a Loop to Step Through an array 2.Implicit Array Sizing 3.No Bounds Checking.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 3 Control Statements
MT262A Review.
CO1401 Program Design and Implementation
An Introduction to Programming with C++ Sixth Edition
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 7 – Arrays.
Arrays Arrays exist in almost every computer language.
Command Line Arguments
Array An “average.cpp” program
Chapter 2 Assignment and Interactive Input
CO1401 Programming Design and Implementation
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Multi-dimensional Array
C++ Arrays.
לולאות קרן כליף.
CS 1430: Programming in C++.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
DAYS OF THE WEEK.
Chapter 2 Elementary Programming
One-Dimensional Array Introduction Lesson xx
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS-161 Computer Programming Lecture 14: Arrays I
CS150 Introduction to Computer Science 1
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Code::Block vs Visual C++
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Starting Out with Programming Logic & Design
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review for Final Exam.
Arrays Lecture 11.
Arrays I Handling lists of data.
Chapter 9: Data Structures: Arrays
CHAPTER 2 Arrays and Vectors.
Dr. Sampath Jayarathna Cal Poly Pomona
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
CHAPTER 2 Arrays and Vectors.
Life is Full of Alternatives
Pointers and dynamic objects
Pointers & Functions.
Arrays.
Contact
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays Part-1 Armen Keshishian

Concept An array allows you to store and work with multiple values of the same data type. An array works like a variable that can store a group of values, all of the same type. int days[6]; //size declarator must be constant integer greater than zero Element 0 Element 1 Element 2 Element 3 Element 4 Element 5

Accessing Array Elements The individual elements of an array are assigned unique subscripts, these subscripts are used to access the elements. Subscript numbering in C++ always starts at zero. The subscript of the last element in an array is one less than the total number of elements in the array. For accessing an element of an array you should use its subscript then you can work with that as a individual variable with the same array’s data type. days[0] = 20; The expression days[0] is pronounced “days sub zero”

Example Write a program that asks user to insert number of working hours per day for a week and shows the total number of hours for that week.

#include "stdafx.h" #include <iostream> using namespace std; int main() { int days[7]; cout << "Please enter number of hours per day." << endl; cout << "Monday: "; cin >> days[0]; cout << "Tuesday: "; cin >> days[1]; cout << "Wednesday: "; cin >> days[2]; cout << "Thursday: "; cin >> days[3]; cout << "Friday: "; cin >> days[4]; cout << "Saturday: "; cin >> days[5]; cout << "Sunday: "; cin >> days[6]; //Display the values in the array cout << endl; cout << "-------Your schedule is-------" << endl; cout << "Monday: " << days[0] << endl; cout << "Tuesday: " << days[1] << endl; cout << "Wednesday: " << days[2] << endl; cout << "Thursday: " << days[3] << endl; cout << "Friday: " << days[4] << endl; cout << "Saturday: " << days[5] << endl; cout << "Sunday: " << days[6] << endl; //Calculate total number of hours int sum = 0; for (int i = 0; i < 7; i++) sum += days[i]; } cout << "Total number of hours: " << sum << endl; system("pause"); return 0;

No bounds checking in C++ C++ does not prevent you from overwriting an array’s bounds. This means that you can write programs with subscripts that go beyond the boundaries of a particular array.

Off-by-one error This is an easy and common mistake to make on working with arrays, because array subscripts start at 0 rather than 1. int numbers[10]; for (int i = 1; i <= 10; i++) { numbers[i] = 0; }

Array initialization Arrays may be initialized when they are defined. int days[7] = { 8,7,6,5,4,6,0};

Number of days in each month Write a program that shows number of days in each month by asking users to insert number of month. int main() { int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31 }; cout << "Please insert number of month: "; int month = 0; cin >> month; cout << "Number of days for month " << month << " is " << days[month] << endl; system("pause"); return 0; }

Examples Write a program that creates an array with 10 elements and asks user to fill that array out, then it shoud: 1- Find the max 2- Show the total 3- copy all elements into another array. 4- copy all elements into another array in reverse 5- compare with another array with the same size. HOMEWORK

Parallel Arrays Consider a company with 5 employees. Each employee has total hours and pay rate. Write a program for this company that creates two arrays one for hours and one for pay rates. Then asks user to insert hour and rate for each employee. Then calculates the gross pay for each employee.

Arrays and Functions To pass an array as an argument to a function, pass the same of the array. It is possible to pass a single value of an array to a function Write a program to use a function to show all numbers in an array

Display values of an array #include <iostream> using namespace std; void displayValues(int numbers[], int size); int main(int argc, const char * argv[]) { int nums[5] = {5, 6, 4, 6, 20}; displayValues(nums, 5); return 0; } void displayValues(int numbers[], int size) { for (int i =0 ; i < size; i++) { cout << numbers[i] << " " ;

Homework arraysGeneralFunctions Write a program that asks user to insert 10 numbers (integer) into an array and shows Min Average All negative numbers All Even numbers You should use functions for each item. Hint (functions prototypes): Min -> int GetMin(int numbers[], int size); Average -> float GetAverage(int numbers[], int size); Negative -> void ShowNegatives(int numbers[], int size); Even -> void ShowEvenNumbers(int numbers[], int size);

Homework employeePayChecks Consider a company with 5 employees, each employee has total number of hours per month, pay rate, and number of dependents. Write a program that asks total hours for each employee per month and the pay rate. Then calculates the gross amount as well as net amount. To calculate net amount first you should deduct $300 for each dependent then uses the chart below to get percentage and deduct it from gross amount then add all deducted amount for dependents back to net salary. Gross amount Tax percentage Amount < 60,000 15% 60,000 <= Amount < 80,000 18% 80,000 <= Amount < 99,000 22% 99,000 <= Amount 25% Example: if an employee’s income is 61,000 and he/she has 4 dependents. First you should deduct $300 for each dependent which will be (300 * 4 = $1,200). 61000 – 1200 = 59800. Since 59,800 is less than 60,000 you should use 15% of 59800 which will be $8970. This amount is the tax that employee should pay off of $59,800 that means 59800 – 8970 = 50830 Now you should add dependents deductions back to net pay 50830 + 1200 = 52030 This value is the final net.