Introduction to Programming

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Introduction to Programming Lecture 4. Key Words of C main main if if else else while while do do for for.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to Programming
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computing and Statistical Data Analysis Lecture 2
Programming Fundamental
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Programming Fundamental
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
C++ Arrays.
Object-Oriented Programming (OOP) Lecture No. 32
Dynamic Memory Allocation
Vectors.
7 Arrays.
Flow of Control October 16, 2017.
Conditional Construct
CS-161 Computer Programming Lecture 14: Arrays I
Strings A collection of characters taken as a set:
Programming Funamental slides
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
CS150 Introduction to Computer Science 1
Lecture 12 Oct 16, 02.
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
String What it is Why it’s useful
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
7 Arrays.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Fundamental Programming
C Programming Pointers
Introduction to Programming
CS150 Introduction to Computer Science 1
Structure (i.e. struct) An structure creates a user defined data type
Introduction to Programming
Introduction to Programming
Programming Fundamental
Programming Fundamental
Intro to Arrays Storing List of Data.
Programming Fundamental-1
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Introduction to Programming Lecture 11

ARRAYS

Arrays They are special kind of data type They are like data structures in which identical data types are stored In C each array has name data type size They occupy continuous area of memory

Storage of an array in memory C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8] C[9] Name ... 35 59 24 memory Index

Declaration of Arrays For example , int age [ 10 ] ; arrayType arrayName[numberOfElements ]; For example , int age [ 10 ] ; More than one array can be declared on a line int age [10] , height [10] , names [20] ; Mix declaration of variables with declaration of arrays int i , j , age [10] ;

Referring to Array Elements Array name e.g. age index number age [ 4 ]

Example1: Using Arrays for ( i = 0 ; i < 10 ; i ++ ) { cin >> age [ i ] ; }

Example 2 totalAge = 0 ; for ( i = 0 ; i < 10 ; i ++ ) { totalAge + = age [ i ] ; }

Initializing an Array int age [ 10 ] ; for ( i = 0 ; i < 10 ; i ++ ) { age [ i ] = 0 ; }

Initializing an Array int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ;

‘ i ‘ will have value from 0 to 9 Initializing an Array int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ; for ( i = 0 ; i < 10 ; i ++ ) ‘ i ‘ will have value from 0 to 9

Example: 3 #include < iostream.h > main ( ) { int c [ 100 ] ; }

Example: 3 do { int z , i = 0 ; cin >> z ; if ( z != -1 ) c[ i ] = z ; assignment statement

Example 3 i ++ ; } while ( z != -1 && i < 100 ) ; cout << “ The total number of positive integers entered by user is “ << i -1;

Copying Arrays Data types should be identical Size should be same int a [ 10 ] ; int b [ 10 ] ;

Copying Arrays To copy from array “ a ” to array “ b ” : b [ 0 ] = a [ 0 ] ; b [ 1 ] = a [ 1 ] ; b [ 2 ] = a [ 2 ] ; b [ 3 ] = a [ 3 ] ; … … … b [ 10 ] = a [ 10 ] ;

Copying Arrays for ( i =0 ; i < 10 ; i ++ ) b [ i ] = a [ i ] ;

Example: 4 Take the sum of squares of 10 different numbers which are stored in an array int a [ 10 ] ; int arraySize =10 ; int sumOfSquares = 0 ; for ( i = 0 ; i < arraySize ; i ++ ) { sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ; }

Example 5 int z ; int a [ 100 ] ; for ( i = 0 ; i < 100 ; i ++ ) { a [ i ] = i ; } cout << “ Please enter a positive integer “ ; cin >> z ; int found = 0 ;

Example 5 for ( i =0 ; i < 100 ; i ++ ) { if ( z == a [ i ] ) found = 1 ; break ; }

Example 5 if ( found == 1 ) cout << “ We found the integer at position ” << i ; else cout << “ The number was not found” ;

rand ( ) # include < stdlib.h > 0 - 32767

Calling rand ( ) x = rand ( ) ; A call goes to ” rand ( ) “ , it generates a number and returns to x

Modulus “ % ” It returns the remainder rand ( ) % 6 = ? Result has to be between 0 and 5 inclusive 1 + rand ( ) % 6 It will randomly generate number between 1 and 6

Fair Die If a die is rolled 10/100 million of time , then on average equal number of 1’s ,equal number of 2’s , equal number of 3’s etc. will be generated

Example: Tossing a Coin It has only two possibilities 0 / 1 rand ( ) % 2 ;

Importance of rand ( ) It is shipped in every standard library with compiler Most major programming languages give some kind of random number generator as a function as part of library Writing a random number generator is itself a field

Array Declaration data type name size

const

const const int arraySize = 100 ; It creates an identifier “ arraySize ” and assigns a value 100. This is called integer constant . It is not a variable Its value cannot be changed