Lecture-5 Arrays.

Slides:



Advertisements
Similar presentations
1 DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Programming for Engineers
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
ARRAYS.
Array in C# Array in C# RIHS Arshad Khan
Arithmetic Expressions
Computer Organization and Design Pointers, Arrays and Strings in C
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
CSC215 Lecture Advanced Pointers.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
Tutorial 8 Pointers and Strings
Objectives Identify the built-in data types in C++
CSE 303 Concepts and Tools for Software Development
Programming Paradigms
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
Dale Roberts, Lecturer IUPUI
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
Computer Skills2 for Scientific Colleges
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
7 Arrays.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Variables, Pointers, and Arrays
CS150 Introduction to Computer Science 1
C Programming Lecture-8 Pointers and Memory Management
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Exercise Arrays.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Arrays.
Chapter 9: Pointers and String
Strings #include <stdio.h>
CS-161 Computer Programming Lecture 15 & 16: Arrays II
CS31 Discussion 1H Fall18: week 6
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
Variables and Constants
4.1 Introduction Arrays A few types Structures of related data items
Introduction to Problem Solving and Programming
Presentation transcript:

Lecture-5 Arrays

Declaration Analogous to using subscripted variables used in mathematical notations e.g. a1, a2, a3,….an Declared using the [] notation to indicate the range/size of the array Elements are indexed using the [] notation, ranging from 0 to one less than the bound

Example #include <stdio.h> int main() { Representation of array in program memory #include <stdio.h> int main() { int a=0, b[5], c=0, cnt=0; for(cnt=0;cnt<5;cnt++) { b[cnt]=cnt; } return 0; a b[0] b[1] b[2] b[3] b[4] c Memory Follow this link to arr-rev.c

Arrays Operations There are no built-in operations on arrays e.g. you cannot write a+b, to add two arrays a[] and b[] Functions cannot return arrays by value To pass or return the same array, you have to pass by reference Follow this link to ref-arr.c

In-class exercise 5-1 Write a program to: Create array a[5], and take in 5 values from the user to populate the array Create array b[5], and take in 5 values from the user to populate the array Create a third array c[5], add values of variables in a[] and b[] and store results in c[] i.e. C[] = a[] + b[]

Multidimensional Arrays Declared with one or more bracketed subscripts e.g. a[][] For function declarations, only the first index may omit the bounds e.g. void addArray(float a[][2], float cnt);

Strings (string.h) Strings are arrays with elements of type character or ‘char’ Strings are always terminated with a end-of-line control character: \n Internally strings are stored with ASCII integer values of the character which they represent

String operations gets() reads a line as a string(array of characters) from the console. Return value is the string or NULL puts() writes a string to the console strlen(): Number of characters in a strcmp(a,b): compare strings a and b strcpy(a, b): copy b into a

Example Follow this link to str-cmp.c