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

Similar documents
Lab Session # 6 Functions. ALQUDS University Department of Computer Engineering

- If you want to repeat the same blocks of code over and over you have two choices. Copy

Lab Session # 7 Pointers. ALQUDS University Department of Computer Engineering

ALQUDS University Department of Computer Engineering

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.

Programming for Engineers Arrays

Chapter 9 Introduction to Arrays. Fundamentals of Java

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

How to declare an array in C?

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Arrays. Eng. Mohammed Abdualal

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

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Lab #10 Multi-dimensional Arrays

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Computer Programming: C++

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

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

Instructor: Eng.Omar Al-Nahal

Multiple-Subscripted Arrays

Principles of Programming. Chapter 6: Arrays

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

Fundamentals of Programming Session 14

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Arrays and Strings. Arash Rafiey. September 12, 2017

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

Chapter 6: Using Arrays

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

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

Object Oriented Programming. Java-Lecture 6 - Arrays

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

ONE DIMENSIONAL ARRAYS

Chapter 7 Array. Array. C++, How to Program

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Module 6: Array in C

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

Computers Programming Course 11. Iulian Năstac

Chapter 12 Two-Dimensional Arrays

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

by Pearson Education, Inc. All Rights Reserved. 2

The Warhol Language Reference Manual

Computer Science & Engineering 150A Problem Solving Using Computers

Problem Solving and 'C' Programming

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

C++ PROGRAMMING SKILLS Part 4: Arrays

More non-primitive types Lesson 06

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Introduction to Java Applications

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

IDL Lab #6: The IDL Command Line

15. Multidimensional Arrays

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Programming in C++ 5. Integral data types

Procedural Programming

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Arrays Chapter 6 Chapter 6 1

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

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

Array Initialization

Physics 234: Computational Physics

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

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

INTRODUCTION 1 AND REVIEW

Lab Manual. ISC 115 Computing Foundations. Information Technology Solutions

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

INTI COLLEGE MALAYSIA

ECE Lesson Plan - Class 1 Fall, 2001

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Data Structures and Programming with C++

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

BITG 1113: Array (Part 1) LECTURE 8

Matrix Multiplication

Arrays IT 1033: Fundamentals of Programming

Dr. Nahid Sanzida b e. uet .ac.

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

LAB 2 VECTORS AND MATRICES

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS313D: ADVANCED PROGRAMMING LANGUAGE

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Transcription:

2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 5 Arrays ALQUDS University Department of Computer Engineering Objective: After completing this session, the students should be able to: 1. Understand the syntax of array declaration, array assignments and array initialization. 2. Write programs to model using large sets of data using arrays. 3. Manipulate the array data structure. Background: An array is a series of homogeneous pieces of data that are all identical in type, but the type can also be quite complex. Array is a data structure that groups together entries of similar data type. Arrays can be used to represent polynomials, matrices, and other things. Syntax of array in ANSI C: The syntax of array in ANSI C is as follows: type array_name [size]; type array_name [size] = {list of array values}; Here the type is a variable type defined in C such as int, double, char, etc. The type can also be a user-defined type or a class defined in C++ (or Java). The following figure shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 1

by the position number of the particular element in square brackets ([ ]). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6], and, in general, the ith element of array c is referred to as c[ i - 1 ]. Array names, like other variable names, can contain only letter, digit and underscore characters. Array names cannot begin with a digit character. The position number contained within square brackets is more formally called a subscript or index. A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, if a = 5 and b = 6, then the statement c[ a + b ] += 2 is valid. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 2

Example: int a[5]; // array declared without initialization int a[5] = {1, 2, 3}; // array declared with 5 entries but only 3 initialized (the rest two will be 0). char name[5]; /* define a string of characters */ int a[5] = {1, 2, 3, 4, 5, 6}; // this definition is invalid. It may compile but will crash at run time since the number of entries initialized exceeds the array size. Experiment 5.1: Step1: Which of the following array declaration and / or initialization is valid (no compile or run time error)? Try them on your machine, state the reasons why/why not they are valid. a) int a[80]; b) int a[10] = {0}; c) int a [10] = {50}; d) int a[3] = {0, 1, 2, 3}; e) int a[ ] = {0, 1,2}; Step2: Write code to initialize an integer array of 1000 elements to {1, 2, 3, 4, 5,, 1000}. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 3

Multidimensional arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts: The first (by convention) identifies the element s row and the second (by convention) identifies the element s column. Tables or arrays that require two subscripts to identify a particular element are called double subscripted arrays or 2-dementional arrays. Note that multiple-subscripted arrays can have more than two subscripts. The ANSI standard states that an ANSI C system must support at least 12 array subscripts. The following illustrates a double-subscripted array, a. The array contains three rows and four columns, so it is said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array. Arrays can be any dimension. Here is an example of 2 dimensional array of order 2 x 2 declared as: int Matrix[4][3]; Initialized as Matrix[0][0]= 4; Matrix[0][1]= 2; Matrix[1][0]= 3; Matrix[1][1]= 9; Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 4

Experiment 5.2: Step 1: Create an array for n numbers where the value for n is given by the user. Use a 1-D array, then read the values of elements using looping construct, then find the sum as well as average of these numbers. What do you notice? Step 2: Using 2-D arrays and loops to declare and set a 3x3 matrix, calculate the sum and average of these elements. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 5

Experiment 5.3: Step 1: Write a C program to read a set of numbers into an array then find the maximum or largest element present in an array. The program should also print the location or index at which maximum element occurs in array. Step 2: Write a C program to read a set of numbers (10 numbers) into an array, and then calculate the corresponding value for each of these numbers according to the function: f(x) = x 2-25 if x<0, and f(x) = 10*sin(x) otherwise After that, the program should print out a table of each number and its corresponding value, along with a histogram of asterisks (*) in each line, this histogram represents the value of the function for each number. Hint: Use a 2D array to store values, and then decide the suitable range for the histogram, and then use a looping construct to print out these asterisks. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 6

Post lab5 Write a C program that reads 2-dimentional arrays and then performs the following functions: i) Addition of Two Matrices ii) Multiplication of Two Matrices Algorithm/Analysis/Reasoning C program --------- -------------------------------------------- Output/Screenshots: Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 7