(Numerical Arrays of Multiple Dimensions)

Slides:



Advertisements
Similar presentations
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Advertisements

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.
Kernighan/Ritchie: Kelley/Pohl:
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
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.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
STRINGS STRINGS IN C FUNCTIONS DEALING WITH CHARACTERS FUNCTIONS DEALING WITH STRINGS.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Lesson #7 Arrays‏.
Stack and Heap Memory Stack resident variables include:
INC 161 , CPE 100 Computer Programming
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
(Numerical Arrays of Multiple Dimensions)
ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Computer Programming BCT 1113
C Programming Tutorial – Part I
Arrays Declarations CSCI N305
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Numeric Arrays Numeric Arrays Chapter 4.
Student Book An Introduction
Module 2 Arrays and strings – example programs.
Passing Arguments to a Function
JavaScript: Functions.
L7 – Assembler Directives
Arrays in C.
CSCE 210 Data Structures and Algorithms
Computer Science 210 Computer Organization
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Arrays, For loop While loop Do while loop
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Week 9 – Lesson 1 Arrays – Character Strings
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
(Numerical Arrays of Multiple Dimensions)
INC 161 , CPE 100 Computer Programming
Variables Title slide variables.
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter 2 Array and String Visit to more Learning Resources.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Arrays in Java.
Exercise Arrays.
Chapter 9: Pointers and String
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Pointers
Visit for more Learning Resources
Presentation transcript:

(Numerical Arrays of Multiple Dimensions)

Traversing the Elements of a 2D Array / Traversing the Elements of a 2D Array /* 3 METHODS OF MANIPULATING OF ELEMENTS OF A MATRIX*/ /* PROGRAM #80-B */ /* 3 METHODS OF MANIPULATING OF ELEMENTS OF A MATRIX*/ #include<stdio.h> #define row 3 #define column 3 main( ){ int Matrix[row][column]={ {1, 3, 5}, {7, 9, 11}, {13, 15, 17} }; int i, j, *ptr; int *FirstElement=&Matrix[0][0]; int *LastElement=&Matrix[row-1][column-1]; }

/* METHOD 1 */ /* Use of two FOR loops, the outer loop for moving along rows, */ /* the inner loop for moving along columns */ for(i = 0; i < row; i++) for( j = 0; j < column; j++) printf(“%5d”, Matrix[i][j]); puts(“”);

/* METHOD 2 */ /* PTR is a pointer pointing to the1st element of the matrix. This pointer gets incremented by one through the For loop */ for(ptr = FirstElement; ptr <= LastElement; ptr++) printf(“%5d”, *ptr);

/* METHOD 3 */ /* Showing &MATRIX[i][0] and MATRIX[i] are the same ie, any matrix is a collection of row vectors */ /* For MATRIX[i][j], vectors can be accessed using MATRIX[i] */ printf(“\n%d, %d\n”, &Matrix[0][0], &Matrix[1][0]); printf(“\n%d, %d\n”, Matrix[0], Matrix[1]); for(i = 0; i < row; i++) for( j = 0; j < column; j++) printf(“%5d”, *(Matrix[i]+j) );

STRINGS STRINGS IN C FUNCTIONS DEALING WITH CHARACTERS FUNCTIONS DEALING WITH STRINGS

Strings There is no string type in C. Instead, to approximate strings, we will use arrays of characters. To transform a simple array of characters into a string, it must contain the '\0‘ character in the last cell. Note that '\0' is one character. It is called the null character or the end-of-string character.

STRINGS IN C String and array Similarities: Both represent a collection of a fixed number of elements of the same type Elements of both array & string are located consecutively in the memory Name of the array is a pointer that points to the first element of the array Name of the string is a pointer that points to the first element of the string

STRINGS IN C String and array Differences: Array represents a collection of numerical values, Data types include int, float, double String represents a collection of characters Data type include only characters

String & Character: Character: a byte of data (ASCII CODE) char Letter='B'; /* Letter is a variable of data type char */ String: a collection of characters char Str[ ]="BIRDS"; /* Str is a string */ NOTE: 'A': a single character "A": a string containing 2 characters, 'A' and '\0'

How to Declare a String? 1. Need to have a name for the string 2. Need to know the length of the string 3. Actual memory space needed is length of the string + 1 (to take care of the NULL character) 4. Data type is char Example: char Str[20];

To declare a string and initialize it we could do it the same way as seen before: char city[ ] = {'T', 'o', 'r', 'o', 'n', 't', 'o', '\0'}; Notice that the size of the array can be omitted here, since the computer can deduce it. However, declaring the size (in this case 8), is always good form. But there is a simpler way: char city[ ] = “Toronto”; If you specify the size here, do not forget the invisible '\0'! Size must be 8 not 7! The result is exactly the same. By using the simpler way, the '\0' is added automatically at the end. 'T' ' o' 'r' 'o' 'n' 't' 'o' '\0‘ 0 1 2 3 4 5 6 7

“U" and ‘U' Double quotes " " represent a string, A single quote ' ', one character. ‘U' is the single character U But “U" is an array size 2 containing the ‘U' character and the '\0' character. All string constants are represented in double quotes (remember "This is my first C program." ?).

How to Initialize a String? /*PROGRAM # 94*/ /*DEMONSTRATES INITIALIZING STRING*/ /* length of the string is 5, need 5+1 memory space */ /* char Str[6]=”BIRDS”; */ #include <stdio.h> main( ){ /* Declare and array with type char */ char str[ ] = "BIRDS"; printf("The string is %s.", str); } Note: The format specifier for a string variable is %s. char str [ ]="BIRDS": declare an array named str capable of storing characters initialize it to "BIRDS"

Inside the Memory char Str[6]=”BIRDS”; char Str[ ]=”BIRDS”; These 2 statements are equal.

Memory Map – Placement of a string within the memory Address memory Array element 2400 ‘B’ Str[0] 2401 ‘I’ Str[1] 2402 ‘R’ Str[2] 2403 ‘D’ Str[3] 2404 ‘S’ Str[4] 2405 ‘\0’ Str[5]

NOTES!!! Str and &str[0] both refer to the top of the string (ie, address 2400). ‘\0’ is NULL character. ‘\0’ lets ‘C’ knows where the string ends. When we calculate the length of the string, '\0' is NOT counted. The length of the string is 5 but we need 6 bytes for the string (one extra for ‘\0’). Therefore, when declaring the string we need (LengthOfString+1) bytes.

Inside the Memory char Str[100]="BIRDS"; is acceptable, it means that we set aside 100 bytes in the memory for Str. Right now we only initialize the first 6 bytes but we expect that later on there will be some usage for the rest of them. Furthermore, we do not have any idea about the content of the Str[6] to str[99].

Placement of a string within the memory Address memory Array element 2400 ‘B’ Str[0] 2401 ‘I’ Str[1] 2402 ‘R’ Str[2] 2403 ‘D’ Str[3] 2404 ‘S’ Str[4] 2405 ‘\0’ Str[5] 2406 ? Str[6] 2407 Str[7] 2408 Str[8] …

/*DEMONSTRATES DISPLAYING STRING*/ /* PROGRAM # 95 */ #include <stdio.h> #define ssize 6 main( ){ char str[ ] = "BIRDS"; int i; printf("Our string is %s. \n", str); /*Display characters string contains*/ puts(“Our string is made out of the following characters:”); for(i=0; i<ssize; i++) printf("%c\n ", str[i]); puts(“”); } AFTER EXECUTION: Our string is BIRDS. Our string is made out of the following characters: B I R D S