Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Similar documents
Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

DBMS and its Architecture

Write a C program using arrays and structure

DB Basic Concepts. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

Single Dimension Arrays

DECLARAING AND INITIALIZING POINTERS

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

Part II IT Infrastructure (Computer Software)

Principles of Programming. Chapter 6: Arrays

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

Pointers and Arrays 1

First of all, it is a variable, just like other variables you studied

Solutions: Program 1: Program 2: //To calculate the Gross salary of an Employee

Programming in OOP/C++

Procedural Programming

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

Module 6: Array in C

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

How to declare an array in C?

Advanced Computer Networks. Rab Nawaz Jadoon DCS. Assistant Professor COMSATS University, Lahore Pakistan. Department of Computer Science

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

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

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

Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS Institute of Information Technology. Mobile Communication

Multiple-Subscripted Arrays

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.

Understanding SNMP. Rab Nawaz Jadoon DCS. Assistant Professor COMSATS University, Abbottabad Pakistan. Department of Computer Science

Multi-Dimensional arrays

UNIT - V STRUCTURES AND UNIONS

Unit IV & V Previous Papers 1 mark Answers

Concurrent Processes Rab Nawaz Jadoon

A few notes on using pointers in the C language

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Arrays and Strings. Arash Rafiey. September 12, 2017

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Data Representation and Storage. Some definitions (in C)

One Dimension Arrays 1

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Deadlock. Rab Nawaz Jadoon DCS. Assistant Professor COMSATS Lahore Pakistan. Department of Computer Science

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

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Logical Network Design (Part II)

Wireless and Mobile. Rab Nawaz Jadoon. Applications DCS. Assistant Professor. Department of Computer Science

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Introduction to Scientific Computing and Problem Solving

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

C Language, Token, Keywords, Constant, variable

APSC 160 Review Part 2

VARIABLES AND CONSTANTS

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

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

More about BOOLEAN issues

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CS 101, Spring 2016 March 22nd Exam 2

Data Representation and Storage

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

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

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

Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS Institute of Information Technology. Mobile Communication

C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

Chapter 3: Arrays and More C Functionality

Unit 3 Decision making, Looping and Arrays

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

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

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Rab Nawaz Jadoon. Characterizing Network Traffic DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

[ DATA STRUCTURES] to Data Structures

ECE 122. Engineering Problem Solving with Java

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

Secondary Storage Management Rab Nawaz Jadoon

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

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

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

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Fundamentals of Computer Programming Using C

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Exercise: Inventing Language

Expressions and Casting

EENG 212 Lab 2. Recursive Functions

Assignment 3: Binary numbers, data types and C Programs Due 11:59pm Monday 26th March 2018

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Chapter 9 Introduction to Arrays. Fundamentals of Java

Transcription:

Arrays in C DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP)

Array C language provides a capability that enables the user to design a set of similar data types, called array. For understanding the arrays properly, let us consider the following program: No doubt, this program will print the value of x as 10. Why so? Because when a value 10 is assigned to x, the earlier value of x, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time. However, there are situations in which we would want to store more than one value at a time in a single variable. 2

Example For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such a case we have two options to store these marks in memory: Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student s marks. Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values. 3

Array Definition Array An array is a collective name given to a group of similar quantities. OR Consecutive memory locations having the same name and same data type. Note These similar elements could be all ints, or all floats, or all chars, etc 4

Important points regarding array Usually, the array of characters is called a string, whereas an array of ints or floats is called simply an array. Remember that all elements of any given array must be of the same type. i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats. 5

Array Declaration Like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. For example int marks[30]; The bracket ( [ ] ) tells the compiler that we are dealing with an array. Int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of the variable. The [30] however is new. The number 30 tells how many elements of the type int will be in our array. 6

Accessing Elements of the array After array declaration, how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the element s position in the array. All the array elements are numbered, starting with 0. Thus, marks[2] is not the second element of the array, but the third. 7

Entering data to an Array Here is the section of code that places data into an array: 8

Reading Data from an Array We are getting the actual values of the arrays from its index, like, for(int i=0;i<30;i++) { printf( marks[%d] = %d, i, marks[i]); } 9

Simple program Let us try to write a program to find average marks obtained by a class of 30 students in a test: 10

Important points An array is a collection of similar elements. The first element in the array is numbered 0, so the last element is 1 less than the size of the array. An array is also known as a subscripted variable. Before using an array its type and dimension must be declared. However big an array its elements are always stored in contiguous memory locations. 11

Array Initialization So far we have used arrays that did not have any values in them to begin with. We managed to store values at run time, Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this. 12

Important Points Till the array elements are not given any specific values, they are supposed to contain garbage values. If the array is initialized where it is declared, mentioning the dimension of the array is optional as in the 2 nd example above. 13

Arrays in Memory Consider the following array declaration: int arr[8]; 16 bytes get immediately reserved in memory, 2 bytes each for the 8 integers. Since the array is not being initialized, all eight values present in it would be garbage values. all the array elements would always be present in contiguous memory locations. 14

15