Arrays IT 1033: Fundamentals of Programming

Similar documents
IT 1033: Fundamentals of Programming Data types & variables

Data Types & Variables

Fundamentals of Programming Data Types & Methods

IT 1033: Fundamentals of Programming Loops

Fundamentals of Programming. Budditha Hettige Department of Computer Science

Fundamentals of Programming

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CS242 COMPUTER PROGRAMMING

Lab # 02. Basic Elements of C++ _ Part1

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

How to declare an array in C?

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CST112 Variables Page 1

(Refer Slide Time: 00:23)

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Personal SE. Functions & Arrays

Fundamentals of Programming CS-110. Lecture 2

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Exercise: Using Numbers

printf( Please enter another number: ); scanf( %d, &num2);

Chapter 2 THE STRUCTURE OF C LANGUAGE

Exercise: Inventing Language

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Array. Prepared By - Rifat Shahriyar

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Introduction to C++ IT 1033: Fundamentals of Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Exam 3 Chapters 7 & 9

Java Module Lesson 2A Practice Exercise

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Subject: Computer Science

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

The Warhol Language Reference Manual

CSC 2400: Computer Systems. Arrays and Strings in C

Object Oriented Programming 2012

Review of Important Topics in CS1600. Functions Arrays C-strings

M1-R4: Programing and Problem Solving using C (JULY 2018)

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

Introduction to Programming Using Java (98-388)

Activity 3: Data Types

ONE DIMENSIONAL ARRAYS

Chapter-8 DATA TYPES. Introduction. Variable:

CSC 2400: Computer Systems. Arrays and Strings in C

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Lecture 3 Tao Wang 1

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Introduction to Programming EC-105. Lecture 2

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Full file at

Chapter 9 Introduction to Arrays. Fundamentals of Java

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

BASIC ELEMENTS OF A COMPUTER PROGRAM

Programming with Java

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Objectives. In this chapter, you will:

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

VARIABLES AND TYPES CITS1001

Ex: If you use a program to record sales, you will want to remember data:

2 nd Week Lecture Notes

Decisions in Java IF Statements

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays and Strings. Arash Rafiey. September 12, 2017

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Fundamentals of Programming Session 8

Worksheet 4 Basic Input functions and Mathematical Operators

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

CS313D: ADVANCED PROGRAMMING LANGUAGE

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

M1-R4: Programing and Problem Solving using C (JAN 2019)

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Lab #10 Multi-dimensional Arrays

LAB 4.1 Relational Operators and the if Statement

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Computer Programming : C++

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

Software and Programming 1

Introduction to C Language (M3-R )

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

This is CS50. Harvard University Fall Quiz 0 Answer Key

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Transcription:

Arrays IT 1033: Fundamentals of Programming Budditha Hettige Department of Computer Science

What is Array An array, is a data structure consisting of a collection of elements Each identified by at least one array index or key simplest type of data structure is a linear array, also called one-dimensional array 7/11/2015 Budditha Hettige (budditha@yahoo.com) 2

Arrays Hold Multiple values Unlike regular variables, arrays can hold multiple values. int count Enough memory for 1 int 12345 float price Enough memory for 1 float 56.981 char letter Enough memory for 1 char A 3

Array Declaration Array Declaration Number of Elements Size of Each Element Size of the Array char letters[25]; 25 1 byte 25 bytes short rings[100]; 100 2 bytes 200 bytes int miles[84]; 84 4 bytes 336 bytes float temp[12]; 12 4 bytes 48 bytes doubleddistance[1000]; 1000 8 bytes 8000 bytes 4

Example 1 5

Example 2 Enter 50 marks and find the average 7/11/2015 Budditha Hettige (budditha@yahoo.com) 6

No Bounds Checking in C++ C++ gives you the freedom to store data past an array s boundaries. 7

Array Initialization Arrays may be initialized when they are declared. When an array is being initialized, C++ does not require a value for every element. It is possible to declare an array without specifying its size, as long as you provide an initialization list. 8

Initializing With Strings When initializing a character array with a string, simply enclose the string in quotation marks: 9

Example 3 7/11/2015 Budditha Hettige (budditha@yahoo.com) 10

More on Arrays Individual array elements are processed like any other type of variable. By using he same subscript, you can build relationships between data stored in two or more arrays. You cannot use the assignment operator to copy one array s contents to another. 11

Printing the Contents of an Array To display the contents of an array, you must use a loop to display the contents of each element. 12

Write a Java program that read 10 integer marks (Range 0 to 100) form keyboard and print the result sheet as shown below. Exercise Write a C++ program that read 10 integer marks (Range 0 to 100) form keyboard and print the result sheet as shown below. ICT 126 2.0 Introduction to Computer Programming ------------------------------------------------ ####### RESULT SHEET ###### ------------------------------------------------ Index NO Marks ================================================ 1 56 2 79 3 45 1. 23... 7/11/2015 Budditha Hettige (budditha@yahoo.com) 13

Find elements Find the largest and small mark of the above marks array 7/11/2015 Budditha Hettige (budditha@yahoo.com) 14

Example (several arrays) Create 2 Arrays to store Student name and GPA values of the 25 students in a class Enter all values and print results in the following format Student summary --------------------- Saman Kumara 2.56 Ruwan Silva 1.94 7/11/2015 Budditha Hettige (budditha@yahoo.com) 15

Multiple Arrays Write a C++ program to store 10 student information in class with considering name, age, height. Your program should read 10 students information from keyboard and display results as given below name age height -------------------------- saman 14 5.7 kamal 12 5.1 7/11/2015 Budditha Hettige (budditha@yahoo.com) 16

Multi-dimensional arrays 7/11/2015 Budditha Hettige (budditha@yahoo.com) 17

Two-dimensional Arrays A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data. 18

2D Array 7/11/2015 Budditha Hettige (budditha@yahoo.com) 19

Example Write a C++ program to display the following Multiplication table 7/11/2015 Budditha Hettige (budditha@yahoo.com) 20

Three Dimensional Arrays and Beyond C++ allows you to create arrays with virtually any number of dimensions. Here is an example of a three-dimensional array declaration: float seat[3][5][8]; 21

3D Array 7/11/2015 Budditha Hettige (budditha@yahoo.com) 22

Exercise Write a function called reversit() that reverses a C-string (an array of char). Use a for loop that swaps the first and last characters, then the second and next-to-last characters, and so on. The string should be passed to reversit() as an argument. Write a program to exercise reversit(). The program should get a string from the user, call reversit(), and print out the result. Use an input method that allows embedded blanks. Test the program with Napoleon s famous phrase, Able was I ere I saw Elba. 7/11/2015 Budditha Hettige (budditha@yahoo.com) 23

Exercise Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint() that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array and the number of elements in it, and return the index number of the largest element. The program should call this function and then display the largest element and its index number. 7/11/2015 Budditha Hettige (budditha@yahoo.com) 24