CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Similar documents
C: How to Program. Week /Apr/23

CSE202- Lec#6. (Operations on CSE202 C++ Programming

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Lecture 04 FUNCTIONS AND ARRAYS

Array. Arrays. Declaring Arrays. Using Arrays

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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.

C: How to Program. Week /Apr/16

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

How to declare an array in C?

C Arrays Pearson Education, Inc. All rights reserved.

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

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

IV Unit Second Part STRUCTURES

C++ PROGRAMMING SKILLS Part 4: Arrays

Arrays. Week 4. Assylbek Jumagaliyev

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

C Arrays. O b j e c t i v e s In this chapter, you ll:

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Single Dimension Arrays

Principles of Programming. Chapter 6: Arrays

ONE DIMENSIONAL ARRAYS

Two Dimensional Array - An array with a multiple indexs.

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

Fundamentals of Programming Session 14

This exam is to be taken by yourself with closed books, closed notes, no calculators.

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

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

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

AMCAT Automata Coding Sample Questions And Answers

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Arrays. Systems Programming Concepts

Unit 7. Functions. Need of User Defined Functions

Advanced C Programming Topics

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

Content. In this chapter, you will learn:

Language comparison. C has pointers. Java has references. C++ has pointers and references

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

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

CSE 5A Final Fall 2006

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Arrays and Pointers (part 1)

C Programming Review CSC 4320/6320

Programming for Engineers Arrays

Programming in C. Pointers and Arrays

MODULE 3: Arrays, Functions and Strings

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

Computer Science & Engineering 150A Problem Solving Using Computers

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

Object Oriented Programming. Java-Lecture 6 - Arrays

Preview from Notesale.co.uk Page 2 of 79

Chapter 6 - Pointers

Fundamental of Programming (C)

For instance, we can declare an array of five ints like this: int numbers[5];

CC112 Structured Programming

BBS 514 YAPISAL PROGRAMLAMA (STRUCTURED PROGRAMMING)

ARRAYS(II Unit Part II)

CSE 124 Discussion (10/3) C/C++ Basics

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Intermediate Programming, Spring 2017*

Lab 3. Pointers Programming Lab (Using C) XU Silei

Arrays and Pointers (part 1)

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Two Dimensional Array - An array with a multiple indexs.

IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Lesson 7. Reading and Writing a.k.a. Input and Output

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

15 FUNCTIONS IN C 15.1 INTRODUCTION

Object oriented programming C++

Chapter 7 Functions. Now consider a more advanced example:

Multiple Choice Questions ( 1 mark)

C Programming Language

Introduction to C Final Review Chapters 1-6 & 13

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Fundamentals of Programming Session 4

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

C programming basics T3-1 -

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Slides adopted from T. Ferguson Spring 2016

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Unit 3 Decision making, Looping and Arrays

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Arrays and Applications

Fundamentals of Programming Session 8

Transcription:

Arrays CSE101-Lec#17 (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Outline To declare an array To initialize an array To pass an array to a function

Arrays Introduction Collection of related data items of same data type. Static entity i.e. they remain the same size throughout program execution

Arrays Array Group of consecutive memory locations Same name and data type To refer to an element, specify: Array name Position number in square brackets([]) Format: arrayname[position_number] First element is always at position 0 Eg. n element array named c: c[0], c[1]...c[n 1] Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] -45 6 0 72 3-89 0 62-3 1 6453 78 Position number of the element within array c

Arrays An array is an ordered list of values The entire array has a single name Each value has a numeric index c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

Arrays Array elements are like normal variables c[0] = 3;/*stores 3 to c[0] element*/ scanf ( %d, &c[1]);/*reads c[1] element*/ printf ( %d, %d, c[0], c[1]); /*displays c[0] & c[1] element*/ The position number inside square brackets is called subscript/index. Subscript must be integer or an integer expression c[5-2] = 7; (i.e. c[3] = 7)

Defining Arrays When defining arrays, specify: Name Data Type of array Number of elements datatype arrayname[numberofelements]; Examples: int students[10]; float myarray[3284]; Defining multiple arrays of same data type Format is similar to regular variables Example: int b[100], x[27];

Initializing Arrays Initializers int n[5] = { 1, 2, 3, 4, 5 }; If not enough initializers given, then rightmost elements become 0 int n[5] = { 0 }; // initialize all elements to 0 C arrays have no bounds checking. If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array.

Initializing Arrays Array is same as the variable can prompt for value from the user at run time. Array is a group of elements so we use for loop to get the values of every element instead of getting single value at a time. Example: int array[5], i; // array of size 5 for(i=0;i<5;i++){// loop begins from 0 to 4 } scanf( %d, &array[i]);

Program of Initializing an array to zero using loop.

Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 0 0 0 0 0 0 0 0 0 0

Program of Initializing an array element with calculations using loop.

Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 2 4 6 8 10 12 14 16 18 20

Program to compute sum of elements of array Total of array element values is 383

#include <stdio.h> /* function main begins program execution */ int main() { char array[ 5 ]; /* define an array of size 5 */ Program to explain the address of array printf( " array = %p\n&array[0] = %p\n" " &array = %p\n", array, &array[ 0 ], &array ); return 0; /* indicates successful termination */ } /* end main */ array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78

Character Arrays Character arrays Character arrays can be initialized using string literals char string1[] = "first"; It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Null character '\0' terminates strings string1 actually has 6 elements Can access individual characters string1[ 3 ] is character s Array name is address of array, so & not needed for scanf scanf( "%s", string2 ); Reads characters until whitespace encountered

Program to print character array as strings.,

Enter a string: Hello string1 is: Hello string2 is: string literal

Passing Arrays to Function Arrays can be passed to functions in two ways: 1. Pass entire array 2. Pass array element by element

Pass entire array Here entire array can be passed as an argument to the function Function gets complete access to the original array While passing entire array Address of first element is passed to function, any changes made inside function, directly affects the Original value. void modifyarray(int b[], int arraysize); Function passing method: Pass by Address

Pass array element by element Here individual elements are passed to the function as argument Duplicate carbon copy of Original variable is passed to function So any changes made inside function does not affects the original value Function doesn t get complete access to the original array element. void modifyelement(int e); Function passing method: Pass by Value

Passing Arrays to Functions Function prototype void modifyarray(int b[], int arraysize); Parameter names optional in prototype int b[] could be written int [] int arraysize could be simply int void modifyarray(int [], int); Function call int a[size]; modifyarray(a, SIZE);

Passing arrays and individual array elements to functions

Effects of passing entire array by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element by value: The value of a[3] is 6 Value in modifyelement is 12 The value of a[3] is 6

Next Class: Applications of Arrays cse101@lpu.co.in