ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Guidelines for working with Microsoft Visual Studio.Net.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Multiple-Subscripted Array
Performance Improvement
Guidelines for working with Microsoft Visual Studio 6.
Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.
Dynamic Memory Allocation. One Dimensional Dynamic Memory #define SIZE1 25 #define SIZE2 36 int *p; long double *q; p = (int *)malloc(SIZE1 * sizeof(int));
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
1 ENERGY 211 / CME 211 Lecture 13 October 20, 2008.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
CMSC 1041 Functions II Functions that return a value.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
Arrays in C.
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
1 Programming Environment and Tools VS.Net 2012 First project MSDN Library.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Functions & Pointers in C Jordan Erenrich
Application Debugging. Debugging methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
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]
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
2-D Arrays Declaration & Initialization. Declaration You can think of 2D arrays as a matrix rows and columns: – a 4x3 matrix –
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Advanced loop controls. Loop Controls Recall from last week loop controls Event-controlled loops using sentinels repeats until a control variable takes.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Chapter 1: Introduction to Computers and Programming.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Dynamic memory allocation and Intraprogram Communication.
Lecture 3 Translation.
Dynamic Array Multidimensional Array Matric Operation with Array
Chapter 1. Introduction to Computers and Programming
Lecture 18 Arrays and Pointer Arithmetic
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
C Programming Lecture-8 Pointers and Memory Management
An Overview of C.
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code “time.h” contains prototypes of some functions that access machine’s internal clock and type defitions time_t and clock_t –typedef long clock_t; –typedef long time_t; –clock_t clock(void); –time_t time(time_t *p); –double difftime(time_t time1, time_t time0); –#define CLOCKS_PER_SECOND 60 /*machine-dependent*/

HOW to time C Code : Types clock_t : Type capable of representing clock tick counts and support arithmetical operations time_t : Type capable of representing times and support arithmetical operations. It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970.

HOW to time C Code : Functions clock_t clock(void); When a program is executed, the operating system keeps track of the processor time that is being used. When clock() is invoked, the value returned is the system's best approximation to the time used by the program up to that point

HOW to time C Code : Functions time_t time(time_t *p); The function time() typically returns the number of seconds that have elapsed since 1 January This function can be used to seed random number generator -- > srand(time(NULL)) double difftime(time_t time1, time_t time0); If two values of time() are passed to difftime() the difference expressed inseconds is returned as a double.

HOW to time C Code : Example1 #include int main(void) {int i=0; // Declare clock variables: clock_t t1, t2; float ratio; ratio = 1./CLOCKS_PER_SEC; // Record the start time: t1 = clock(); printf("%f\n",t1*ratio); /**Run a segment of Code **/ for(i=0;i< ;i++); // Record the end time: t2 = clock(); printf("%f\n",t2*ratio); // Print out the difference, converting // it to seconds: printf("Time = %f", ratio*t2 - ratio*t1); }

HOW to time C Code : Example2 #include int main(void) {int i=0; // Declare time variables: time_t t3,t4; // Record the start time: time(&t3); /************************* Run a segment of Code *************************/ for(i=0;i< ;i++); // Record the end time: time(&t4); // Print out the difference printf("Difference %lf\n",difftime(t4,t3)); printf("Date %s",ctime(&t3)); } /* output : Difference Date Mon May 11 10:40: */

Debugging A methodical process of finding and reducing the number of bugs, or defects, in a computer or a piece of electronic hardware thus making it behave as expected( definition from wikipedia ) Debugger is the tool to debug your program

Debugging A debugger allows the programmer to step through the code a line at a time and to see the values of variables and expressions at each step. –example: gdb (The GNU debugger) –for MS Visual Studio debugger: Start Debug Step Into / Step Over

Dynamic Allocation of Matrices In mathematics, a matrix (plural matrices, or less commonly matrixes) is a rectangular array of numbers, as shown at the right (definition from wikipedia)

Dynamic Allocation of Matrices int x [3] is similar to int *x=malloc( sizeof(int)*3) int x [3][3] is similar to int **x How to allocate memory for a dynamic matrice?? 1. Allocate memory for rows. 2. Allocate memory for columns.

Dynamic Allocation of Matrices #define rowN 5; #define columnN 6; int **x // create rowN X columnM matrice // 1. Allocate memory for rows x =(int **) malloc( sizeof(int *)*rowN); // 2. Allocate memory for columns for(i=0;i<rowN;i++) x[i]=(int *)malloc(sizeof(int)*columnN);

Deallocate Dynamic Matrices for(i=0; i<rowN; i++) free(x[i]); free(x);

DYnamic Matrices : Example #include int main(void) { int **x; int rowN,columnN, i,j; printf("Give values for row and column"); scanf("%d%d",&rowN,&columnN); x = (int **)calloc(rowN,sizeof(int*)); for(i=0;i<rowN;i++) x[i]=(int *)malloc(sizeof(int)*columnN); for(i=0;i<rowN;i++) for(j=0;j<columnN;j++) x[i][j]=i*j; for(i=0;i<rowN;i++) {for(j=0;j<columnN;j++) printf("%d ", x[i][j]); printf("\n"); } for(i=0; i<rowN; i++) free(x[i]); free(x); return 0; }