Lecture 12 Oct 16, 02.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
C++ for Engineers and Scientists Third Edition
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:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSci 125 Lecture 21 Martin van Bommel. Memory Allocation Variable declarations cause compiler to reserve memory to hold values - allocation Global variables.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Example 21 #include<iostream.h> int main() { char Letter = 0;
Array in C# Array in C# RIHS Arshad Khan
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Two Dimensional Array Mr. Jacobs.
The Ohio State University
Programming fundamentals 2 Chapter 1:Array
Student Book An Introduction
C++ Arrays.
Array Data Structure Chapter 6
Vectors.
One-Dimensional Array Introduction Lesson xx
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Introduction to Programming
CS-161 Computer Programming Lecture 14: Arrays I
Data type List Definition:
Arrays November 8, 2017.
Counting Loops.
Declaration, assignment & accessing
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Multidimensional array
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
Dr Tripty Singh Arrays.
CHAPTER 2 Arrays and Vectors.
INC 161 , CPE 100 Computer Programming
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Array What it is. How to use it How to declare it How to initialize it.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Dr. Khizar Hayat Associate Prof. of Computer Science
Functions Chapter No. 5.
Presentation transcript:

Lecture 12 Oct 16, 02

Arrays

1- D arrays A one – dimensional array or a vector is a list of related values with the same data type that is stored using a single group name. The group name is referred to as the array name

1-D array declaration Temperatures 95.75 float temp[5]; // array declaration /* The array named temp has storage reserved for 5 floating point numbers in the computer memory. */ /* Each value in an array is called an element of the array. */ /*Each value in the table is an element of the array named temp which is declared above. */ Temperatures 95.75 83 97.625 72.5 86.25

Other examples of 1-D array declarations int volt[10]; /* declares an array named volt and had storage reserved for 10 integer values. const int size=4; char code[size]; float amount[100];

Accessing array elements Elements in an array are stored sequentially in computer memory. Any individual element can be accessed by giving the name of the array and the elements position. This position is called elements index or subscript value. The first element has an index of 0. Second element has an index 1 and third 2 and so on….

Accessing array elements contd… eg: Given the declaration, float temp[5]; // array declaration //temp[0] refers to first temperature stored in temp array //temp[1] refers to second temperature stored in temp array. //temp[2] refers to third temperrature stored in temp array. //temp[3] refers to fourth temperature stored in temp array. //temp[4] refers to fifth temperature stored in temp array.

Assigning values to arrays float sum; // variable declaration float temp[5]; // array declaration // assigning values below temp[0] = 95.75; temp[1] = temp[0] -11; temp[2] = 5.0 * temp[0]; temp[3] = 79.0; temp[4] = (temp[1] + temp[2] – 3.1 ) / 2.2; sum = temp[0] + temp[1] + temp[2] + temp[3]+temp[4]; // sum is a variable which holds a single floating point value/

Assigning values to arrays contd.. float temp[10]; float sum = 0; int i; for (i=0; i<5; i++) { sum = sum + temp[i]; cout<<temp[i]<<sum; }

Example 1 (1-D array input and output) #include<iostream.h> int main() { int i, temp[5] ; // array declaration for(i=0; i<5; i++) // assigning values to an array cout<<“Enter a temperature:”; cin >> temp[i]; } cout<<endl; // dispaying values of the array for( i=0; i<5;i++) cout<<“temperature “<<i<<“is”<<temp[i]<<endl; return 0;

Output of example 1 Enter a temperature : 85 Enter a temperature : 90 temperature 0 is 85 temperature 1 is 90 temperature 2 is 78 temperature 3 is 75 temperature 4 is 92 // Note that the there are 5 elements with index values of 0 1 2 3 4 .

Example 2 ( Assigning values to a 1-D array and finding their sum) #include<iostream.h> int main() { int i, temp[100], total =0; // array declaration for(i=0; i<5 ; i++) // assigning values for array cout<<“Enter a temperature:”<<endl; cin>> temp[i]; } cout <<“\nThe total of the temperatures”; for(i=0;i<5<i++) // summing arrays values and displaying cout<<“ “<<temp[i]; total = total + temp[i]; cout <<“ is “<<total<<endl; return 0;

Enter a temperature: 85 Example 2 output Enter a temperature: 90 The total of the temperatures 85 90 78 75 92 is 420