C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
C Language.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Arrays part 3 Multidimensional arrays, odds & ends.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 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.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Arrays.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
C ARRAYS -a collection of same type data, 1D, 2D- © 1/25.
Introduction to C programming
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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”
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Array in C# Array in C# RIHS Arshad Khan
Computer Organization and Design Pointers, Arrays and Strings in C
© 2016 Pearson Education, Ltd. All rights reserved.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Arrays, For loop While loop Do while loop
Introduction To Programming Information Technology , 1’st Semester
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays Arrays A few types Structures of related data items
Arrays.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

C Programming – Part 3 Arrays and Strings

 Collection of variables of the same type  Individual array elements are identified by an integer index  Array index always begins at 0  The index is written in square brackets  //Example of declaring a single dimension array int values[10];

 Normally when an array is declared, the type and the number of elements are specified  The compiler will then know how much memory to allocate for the array variable  It is possible to declare an array with dimensions that are not fixed immediately, but space can be allocated as required  This is done with pointers int *values;

 In C, like most other languages, array indices start at o and end at one less than the array size  Eg int itemList [5]; //Contains the following elements itemList[0] itemList[1] itemList[2] itemList[3] itemList[4]

 No one function call or property which directly gives the length of an array in c as in other programming languages  To find the length of an array, you can do the following int arrayItems[10]; int numberOfItems = sizeof(arrayItems) / sizeof(int);

 They are declared as follows: int results[20][5]; This ranges from results[0][0], results[0][1], results[0][3], results[0][4], results[1][0], results[1][1]…results[19][4] Arrays can have even more dimensions int results[20][10][5];  For further dimensions, simply add more []

 int amounts[3]; amounts[0] = 10; amounts[1] = 25; amounts[2] = 35;  This is normal initialization  Remember here that the size of the array is 3  Only indices 0, 1 and 2 can be referenced

 int numbers[] = {1, 2, 3}; //sets up an array with size 3  int numbers[5] = {3, 4,8}; //sets up an array with size 5, but only sets the first three values  This is equivalent to the following:  int numbers[5]; numbers[0] = 3; numbers[1]=4; numbers[2]=8;

 You can use designated initializers to skip over elements of an array  int number[3]= { [0]=5, [2]=7};

 You can initialize a one dimensional character array by specifying  A brace-enclosed comma-separated list of constants  A string constant (with optional surrounding braces)  Initializing a string constant places the null character(\0) at the end of the string if there is room or if the array dimensions are not specified

 char name1[ ] = { 'J', 'a', 'n' };  char name2[ ] = { "Jan" };  char name3[4] = "Jan";

int month_days [2][12]; month_days[0][0] = 1; month_days[0][1] = 2; int month_days[2][12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 In C, strings are defined as arrays of characters  char name[50];  C has no string handling facilities, so the following is illegal char firstName[50], lastName[50], fullName[100]; firstName = “John”; //illegal lastName= “Doe”; //illegal fullName = firstName + lastName; //illegal

 To print a string output, printf is used with the %s control character printf(“%s”, name);  In order to allow variable length strings the \0 character is used to indicate the end of a string  So if we have a string char NAME[50]; and the name “DAVE” is stored in it, the contents will look like:

 Write a program which given a string and a character returns a count of the number of occurrences of that character char find = ‘l’;  char testString [] = “Hello World”;

 Write a program which loops over a character array and displays the vowels and a count of each of the vowels that are present in the array //Starting point char sentence[] = “I am enjoying learning C programming in TEC 284”;