Computers Programming Course 10. Iulian Năstac

Similar documents
Computers Programming Course 11. Iulian Năstac

Computers Programming Course 12. Iulian Năstac

ONE DIMENSIONAL ARRAYS

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

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

Intermediate Programming, Spring 2017*

ARRAYS(II Unit Part II)

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Array Initialization

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

CSC209H Lecture 4. Dan Zingaro. January 28, 2015


C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Procedural Programming & Fundamentals of Programming

Chapter 9 Strings. With this array declaration: char s[10];

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

[0569] p 0318 garbage

Computers Programming Course 5. Iulian Năstac

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

CSC 2400: Computer Systems. Arrays and Strings in C

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Chapter 8 Character Arrays and Strings

Personal SE. Functions, Arrays, Strings & Command Line Arguments

CSC 2400: Computer Systems. Arrays and Strings in C

by Pearson Education, Inc. All Rights Reserved.

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

8. Characters, Strings and Files

Chapter 8: Character & String. In this chapter, you ll learn about;

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

CSE2301. Arrays. Arrays. Arrays and Pointers

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Write a C program using arrays and structure

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Data Structures and Algorithms (DSA) Course 3. Iulian Năstac

C programming basics T3-1 -

Programming for Engineers Arrays

BITG 1113: Array (Part 2) LECTURE 9

PDS Class Test 2. Room Sections No of students

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Multiple Choice Questions ( 1 mark)

C-String Library Functions

Introduction to string

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Create a Program in C (Last Class)

Engineering Problem Solving with C++, Etter

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

OBJECTIVE QUESTIONS: Choose the correct alternative:

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

Arrays and Pointers in C. Alan L. Cox

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Model Viva Questions for Programming in C lab

Summer May 18, 2010

Dynamic memory allocation

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Procedural Programming & Fundamentals of Programming

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CS1100 Introduction to Programming

Memory, Arrays & Pointers

Technical Questions. Q 1) What are the key features in C programming language?

211: Computer Architecture Summer 2016

Exercise 1.1 Hello world

Pointers, Arrays, and Strings. CS449 Spring 2016

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Week 8 Lecture 3. Finishing up C

Object-Oriented Programming

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

Strings Investigating Memory Allocation Pointers Fixed-Point Arithmetic. Memory Matters. Embedded Systems Interfacing.

COP 3223 Final Review

C Programming Language Review and Dissection III

CS107 Spring 2019, Lecture 4 C Strings


Lectures 5-6: Introduction to C

Principles of C and Memory Management

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Transcription:

Computers Programming Course 10 Iulian Năstac

Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation of making the subroutine call. Return statements in C allow a function to specify a return value to be passed back to the code that called the function. 2

Recap from previous course 6. Recursive functions Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. 3

Recap from previous course 7. The efficiency of the functions Functions are essential to write effective programs (unless very simple ones). However, in some special applications it is better to remove and replace a function with an inline code. Inline code performs the same actions as the function without the disadvantage caused by a function call (including the decreasing of speed). 4

Recap from previous course 8. Variadic functions A variadic function is a function that accepts a variable number of arguments. A good example, among specific operations, that has been implemented as a variadic function in many languages is output formatting. The C function printf format is such an example. To portably implement variadic functions in the C programming language, the standard stdarg.h header file is used. The older varargs.h header is still in use for some compilers. 5

Recap from previous course There are 4 storage classes in C: auto register static extern 6

Recap from previous course Notes: Global variables (static and extern) are initialized to 0 automatically Local variables get arbitrary values, depending on actual bits' configuration on stack memory 7

Recap from previous course Initialization Initialization is the assignment of an initial value for a data object or variable. Syntax: class type name = expression; 8

Recap from previous course Notes (again): Avoid to use local variables (auto) which are not initialized. These variables get arbitrary values when are initialized. This is due to the fact that on the stack when a variable is erased, this doesn't means that their position on the stack will become zero. Global and static variables always get zero value at declaration. Function parameters cannot be 9 initialized.

Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in memory. In C programming language, a matrix is a collection of variables (of the same type) that can be called with the same name. C programming language uses "Row Major" schema, which stores all the elements for a given row contiguously in memory. 10

Notes: Instead of declaring individual variables, such as number0, number1,..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 11

Declaring Arrays Syntax: type arrayname [lim_1] [lim_2] [lim_n]; where lim_i is the limit of index i (on dimension i) The index i can have the following values: 0, 1, 2,, lim_i - 1 12

Notes: Each lim_i (where i = 0, 1, 2,, n) is a positive integer. We can access the elements of a matrix by using variables with indices. In C, the name of a matrix is in fact a pointer that indicates the address of its first element. At the declaration of a matrix, the compiler allows a memory zone to contiguous store all its elements. Matrices and pointers are strong related. 13

Example int a[3][4]; this declares an integer array of 3 rows and 4 columns. Index of row will start from 0 and will go up to 2. Column 0 Column 1 Column 2 Column 3 row 0 a[0][0] a[0][1] a[0][2] a[0][3] row 1 a[1][0] a[1][1] a[1][2] a[1][3] row 2 a[2][0] a[2][1] a[2][2] a[2][3] 14

One-dimensional matrices (vectors) The vectors are one-dimensional arrays. Syntax: type name_vect [lim]; 15

Notes: The declaration: char p [10]; creates a matrix of 10 elements, from p [0] to p [9]. The amount of memory required for recording a matrix is proportional to the type and its size. The total size in bytes is calculated: No_of_bytes = sizeof(type) * lim; C does not control the limits of the array. You can overcome both edges of a matrix and write in wrong place (of other variables). It remains the responsibility of the programmer to control limits, where necessary. 16

Example of a mistake: int mat[100], i; for (i=0; i<=100; i++) mat[i]=i; 17

A vector is actually a list of information of the same type stored in contiguous memory locations, in the order of indices. 18

The relationship between pointers and vectors The name of an array is a pointer because it holds the value the address of its first element. However there is a difference between an array name and a pointer type variable. Thus, a pointer type variables can be assigned values at runtime, while it is not possible to make the same for an array/vector (the name of a vector/array always represents the address of its first element). It is customary to say that the name of an array is a constant pointer. 19

Example: int t[10]; int *p; int x; p=t; x=t[0]; 20

How to use a vector as a parameter to a function as a pointer as a vector with unmentioned dimension as a vector with mentioned dimension 21

Examples: a) void funct (int *x) /* pointer*/ {... } b) void funct (int x[ ]) /* vector with unmentioned dimension*/ {... } c) void funct (int x[10]) /* vector with mentioned dimension */ {... } 22

const modifier A variable can be transformed into a constant value (that cannot be modified). Usually a constant is defined using # define But const modifier can also transform a variable into a constant. 23

Possible syntaxes: 1) type const name = value; 2) type const *name = value; 3) const type name = value; 4) const type *name = value; 5) const name = value; 24

Notes: Once established the variable (which is const), further assignments, like: *name= a ; or *(name+1)= b are incorrect. If the assignment is missing, then it means that we are dealing with a formal parameter of a function: tip f (const tip *name); 25

Attention It is possible to circumvent the restrictions imposed by the modifier const. If you use another variable that refers to the same memory location, then that location (other pointer) may alter its content:... char const *s = string of char ; char *p; p = (char *)s; *p = a ; *(p+1) = b ;... 26

Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of char values terminated by a special null character value '\0'. 27

Example: a statically declared string that is initialized to "hi": char str[3]; str[0] = 'h'; str[1] = 'i'; str[2] = '\0'; printf("\n %s \n", str); 28

Main properties of strings: 1. A string is stored in a memory organized as a vector of char type. 2. Each character is kept on different byte (through its successive code number). 3. The most commonly used code for this purpose is ASCII. 4. After the last character of the string there must be added the null character, which is '\ 0. 5. Since there is null in final position, then we have to declare the character array with one more character than the effective number of characters. 6. Although C does not have explicit data string, it allows constant string. A string constant is a list of characters enclosed in quotation marks (eg "hello"). 7. Often it is not necessary to be entered manually the null character at the end of the string (the compiler does automatically). This happens when using special functions to operate strings (like gets()). 8. To operate a string you can use: The name of the array (the constant pointer to the string respectively); A simple pointer to that string. 29

Example If we declare: char tab[ ] = This is a string ; then: tab is the address of first char `T` tab+1 is the address of second char `h` tab+2 is the address of third char `i` etc. or: tab[0] contains the ASCII of first char `T` tab[1] contains the ASCII of second char `h` A similar effect we can obtain by using: char const *p = This is a string ; where p[0] or *p - contains the ASCII of first char `T` p[1] or *(p+1) - contains the ASCII of second char `h` 30

Standard functions for strings C provides a library for strings (string.h). C string library functions do not allocate space for the strings they manipulate, nor do they check that you pass in valid strings; it is up to your program to allocate space for the strings that the C string library will use. Calling string.h library functions with bad address values will cause a fault or "strange" memory access errors. 31

Some of the functions in the Standard C string library calculating the length of strings (e.g. strlen) copying the strings (e.g. strcpy) concatenating the strings (e.g. strcat) comparing the strings (e.g. strcmp) searching (identification) of character or substrings (e.g. strchr, strstr) 32