Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]
|
|
- Andrew McCormick
- 5 years ago
- Views:
Transcription
1 (November 10, ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I The size has to be a constant (not required in the C99-standard) I All elements is of the same type I The index operator [] is used to address single elements I Arrays are allocated in contigous memory
2 (November 10, ) Example: /* Read 100 positive floating point numbers and print them normalized */ int main() { int i; float x[100], max=0; for ( i=0; i<=99; i++ ) { scanf( "%f", &x[i] ); if ( x[i] > max ) max = x[i]; for ( i=0; i<=99; i++ ) printf( "%f", x[i]/max ); return 0;
3 (November 10, ) Arrays as inparameters Example: #include <stdio.h> #include <math.h> double scalprod(double u[], double v[], int n) { double result = 0; int i; for (i= 0; i<n; i++) result += u[i]*v[i]; return result; int main() { double a[] ={3., 4.; printf("the norm is: %6.2f\n", sqrt(scalprod(a, a, 2)));
4 Arrays as outparameters #include <stdio.h> void sort(int v[], int n) { int i; for (i=0; i<n-1; i++) { int min = i; int j; int temp = v[i]; for (j=i+1; j<n; j++) { if ( v[j]<v[min] ) min = j; v[i] = v[min]; v[min] = temp; // simple selections sort int main() { int i, a[] = {3, 1, 2, 5, 8, 4; sort(a,6); for(i=0; i<6; i++) printf("%3d", a[i]); printf("\n"); (November 10, )
5 (November 10, ) Character strings I No String-type! They are stored in arrays of char I At the end a NULL-character is stored (convention) I Can be initialized at declaration time I Can not be moved with an assignment or be compared with relational operators I string.h contains string functions I Format code %s to printf and scanf
6 (November 10, ) Example of string functions int strlen( char s[] ) { int n=0; while ( s[n]!= '\0' ) n++; return n;
7 (November 10, ) Alternative code int strlen( char s[] ) { int n=0; while ( s[n++] ) ; return n-1; Good or bad?
8 (November 10, ) The library string.h A library with functions for string handling. Some of these are: int void void void void int strlen (char s[]); strcpy (char s1[], char s2[]); strncpy(char s1[], char s2[], int n); strcat (char s1[], char s2[]); strncat(char s1[], char s2[], int n); strcmp (char s1[], char s2[]); (Somewhat simplied regarding parameter types and return values...)
9 (November 10, ) Exercises 1. Write a function double enorm(double x[], int n) that computes and returns the euclidic vector norm of the vector x with n elements. The vector norm of a vector x 1 ; x 2 ; : : : x n is dened as vu u t X n i=1 x i 2 2. Write the functions int isvowel(char c) and int isconsonant(char c) that returns 1 if the parameter c is a vowel respective a consonant, 0 otherwise. Also write a main function that reads character from standard input and that counts vowels and consonants. 3. Write a program that reads standard input, nds and prints the longest word. 4. Write a function int match(char m[], char s[]) that returns the index of the rst occurance of the string m in the string s. If the string is not found -1 should be returned.
10 (November 10, ) 5. Write a function int nmatch(char m[], char s[]) that counts and returns the number of occurances of the string m in the string s. 6. Write a program that reads standard input and count the occurrences of each (english) letter. Hint: Use an array of integers with 26 elements. Use place 0 to counts the a, place 1 for b etc. 7. Write a program that counts the frequence of pair of letters. 8. An array contains integer numbers sorted in ascending order. The array has a certain declared size, but it is not required to use all of it. Therefore you have do keep track of the actual use also. Write a function that adds a new number to the array so that the sorting is maintained. What parameters should be used? How should we keep track of both size and actual usage. Also write a function that searches the array for a given key.
11 (November 10, ) Pointers I A variable has an address in memory I A variables address is given by the unary address operator & I Addresses is treated as any value and can be stored in variables (pointer variables) I The unary dereferencing operator * is used at at declaration of pointer variables and to access the item that is referred to by the pointer. I You can do arithmetic on a pointer I The pointer constant NULL (always 0)
12 (November 10, ) Pointers as parameters void swap( int *x, int *y ) { int temp = *x; *x = *y; *y = temp; Called as: int a, b;... swap( &a, &b);
13 (November 10, ) Example: int x = 1, y = 2; int a[] = {4, 3, 2, 1; int *ip, *jp; ip = &x; // ip points to x y = *ip; // as y = x *ip = 3; // as x = 3 jp = ip; // jp also points to x *jp = *jp + 5; // as x = x + 5 (*ip)++; // as x++ jp = NULL; // null reference. Same as jp = 0 *jp = 2; // illegal! ip = &a[0]; // points to the first element in a jp = ip + 1; // points to the second elementet in a *jp = 0; // as a[1] = 0 *(ip+3) = *(ip+1) + 2; // as a[3] = a[1] + 2 ip = a; // as ip = &a[0]
14 Arrays as parameters - again int strlen( char *s ) { int n=0; while ( *s!= '\0' ) { s++; n++; return n; Alternative: int strlen( char *s ) { char *p = s; while ( *p!= '\0' ) p++; return p - s; OBS 1: Called in the same way as before! OBS 2: NO information of the array size! (November 10, )
15 (November 10, ) Four versions of a string copy void strcpy( char *s, char *t ) { int i = 0; while ((s[i] = t[i])!= '\0') i++; void strcpy( char *s, char *t ) { while ((*s = *t)!= '\0') { s++; t++; void strcpy( char *s, char *t ) { while ((*s++ = *t++)!= '\0') ; void strcpy( char *s, char *t ) { while (*s++ = *t++) ;
16 (November 10, ) Arrays with pointers int main( int argc, char *argv[] ) { int i; for ( i = 0; i < argc; i++ ) printf( "%s%s", argv[i], (i < argc-1)? "_" : "" ); printf( "\n" ); return 0; (Note the conditional expression u 1? u 2 : u 3 ) If the program is saved in a le with the name eka a test execution can look like $ eka My God, it is full of stars! eka_my_god,_it_is_full_of_stars! $
17 (November 10, ) Pointers as the value of a function char *findblank(char *s) { /* Searches for the first space character in a string Pre: s - pointer to the first character in a NULL-terminated string Returns: The address of the first space character, or NULL if there is no spaces. */ while ( *s!= ' ' && *s!= '\0' ) s++; if ( *s == '\0' ) return NULL; else return s; Which value will p have after executing this code? char a[] = {'a', 'b', 'c'; char *p = findblank(a);
18 (November 10, ) Multidimensional arrays A matrix, i. e. a two dimensional array is used as double m[10][10]; int i, j; for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) m[i][j] = 0; m[2][4] = 12; The rst number in the declaration is the number of rows, the second is the number of columns, they are numbered from zero and up.
19 (November 10, ) Multidimensional arrays, cont. Alternative, create a memory area of correct size, address yourself using index arithmetics. double *m; int i, j; m = (double *) malloc(100*sizeof(double)); for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) *(m + 10*i + j) = 0; // find the proper element *(m + 10*2 + 4) = 12; Note that you cannot use m[2][4] here, because there is no information about the linelength, therefore C cannot orientate itself in the array.
20 Multidimensional arrays as parameters When using a multidimensional array as a parameter, all dimensions except the rst have to be specied (as a constant). This information is needed for navigation purposes. If you don't know the linelength, how do you nd the next line? void clear(double m[][10], int l) { // l is number of lines int i,j; for (i = 0; i < l; i++) for (j = 0; j < 10; j++) m[i][j] = 0; m[2][4] = 12; int main() { double m[10][10]; // matrix created here int i,j; clear(m, 10); for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++) printf("%5.2f ", m[i][j]); printf("\n"); return 0; (November 10, )
21 (November 10, ) Multidimensional arrays as parameters, cont. Can also be formulated like this. Here m is a pointer to an array of 10 doubles, i. e a pointer to the rst line. void clear(double (*m)[10], int l) { int i,j; for (i = 0; i < l; i++) for (j = 0; j < 10; j++) *(m[i] + j) = 0; *(m[2] + 4) = 12; int main() { double m[10][10]; // matrix created here int i,j; clear(m, 10); for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++) printf("%5.2f ", m[i][j]); printf("\n"); return 0;
22 Multidimensional arrays as parameters, cont. We can also see a matrix as an array of pointers, each pointer will refer to a line. This gives us void clear(double *m[], int k, int l) { int i,j; for (i = 0; i < l; i++) { m[i] = malloc(k*sizeof(double)); // create a line for (j = 0; j < k; j++) *(m[i] + j) = 0; // initialize it *(m[2] + 4) = 12; // set m[2][4] int main() { double *m[10]; // an array with 10 pointers is created int i,j; clear(m, 10, 10); for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++) printf("%5.2f ", m[i][j]); printf("\n"); return 0; (November 10, )
23 Multidimensional arrays as parameters, cont. A pointer to a pointer can of course be dereferred using. #include <stdio.h> #include <stdlib.h> void clear(double **m, int k, int l) { int i,j; for (i = 0; i < l; i++) { *(m+i) = malloc(k*sizeof(double)); // create one line for (j = 0; j < k; j++) *(*(m+i) + j) = 0; // initialize it *(*(m+2) + 4) = 12; // set m[2][4] int main() { double **m; // just a pointer, no space is created int i,j; m = (double **) malloc(10*sizeof(double *)); // an array with 10 pointers clear(m, 10, 10); for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++) printf("%5.2f ", m[i][j]); printf("\n"); (November 10, )
24 (November 10, ) Warnings Incorrect usage of pointers is a common cause of errors that is extremely hard to nd.! Some common errors: I Trying to dereference the NULL-pointer I Use of undened pointers I Use of pointers to memoryareas that are no longer allocated I Several pointers that uninentionally references the same object
25 (November 10, ) Denition of own types: typedef Example typedef float Real; typedef unsigned char Digit; typedef char String[100]; typedef int * IntPointer; Real x; String name, adress; IntPointer ip, jp; I typedef denes a new datatype I The syntax follows the syntax of declarations I Makes it easy to introduce new types I Use typedef!
26 (November 10, ) Compound data types: struct Example struct Point { double x, y; ; // Note the semicolon! typedef struct Point Point; typedef struct Triangle {// Can be done directly Point a, b, c; Triangle; Point p1 = {0, 0; Point p2; Triangle t; p2.x = 1.; p2.y = 0.; t.a = p1; t.b = p2; t.c.x = p1.x + 2; t.c.y = t.a.x + 1;
27 (November 10, ) Dynamic memory handling: malloc and free Example #include <stdlib.h> #include <stdio.h> int i, n, *buffer; printf( "How many number do you need to treat?"); scanf( "%d", &n ); buffer = (int *) malloc( sizeof(int)*n ); for ( i = 0; i < n; i++ ) { buffer[i] = i*i;... free(buffer);
28 (November 10, ) Observe I The operator sizeof that returns the size in bytes of any type I malloc returns NULL if the allocation failed I The type cast (int *) malloc returns the type void * I Memory is returned with free no automatic GBC I The array notation I All possibilities to produce errors...
29 (November 10, ) Exercises 1. Write a function that solves the equation The solution is given by ax 2 + bx + c = 0 x 1;2 = b p b 2 4ac 2a The coecients a, b and c should be sent to the function as parameters. Write three dierent version to return the solutions in diernet ways a) using two parameters b) as a struct (value of the function) c) as a pointer to a struct Also, write a main function that tests your function with a series of dierent coecients.
30 (November 10, ) 2. The following struct is used to store a varying number of integer numbers struct container { int *buffer; /* Pointer to storage area */ int size; /* Allocated size */ int number; /* Actual usage */ ; typedef struct container container; Write a function int store(int data, container *c) that stores data in the rst free position and that returns the index of that position. If the container is full (ie if size==number) a new container should be created, twice the size of the old one. All data should then be moved to the new one, and the new onw replaces the old one. Also write a function that returns the number that is stored in a given position. What parameters should such a function take? 3. Same as above, but using text strings instead of integer numbers.
31 (November 10, ) Linked structures: a list of integers #include <stdio.h> #include <stdlib.h> struct listelem { int item; struct listelem *next; ; typedef struct listelem listelem; typedef struct listelem *link;
32 (November 10, ) link cons( int it, link nx ) { /* Create a list node Pre: it - an int nx - pointer to a list node or NULL Return: A pointer to the new node or NULL if the allocation failed */ link l = (link) malloc( sizeof(listelem) ); if (l==null) { return NULL; else { l->item = it; l->next = nx; return l; Obs: The operator ->. The expression x->y is equivalent to (*x).y
33 (November 10, ) int head( link l ) { /* Returns the stored value Pre: l - pointer to a list node */ return l->item; link tail( link l ) { /* Returns a pointer to the first node in the rest of the list Pre: l - pointer to a list node */ return l->next;
34 (November 10, ) void print( link l ) { /* Prints the list on standard output Pre: l - pointer to the first node in the list or NULL */ printf("["); while (l) { printf( "%d", l->item ); if ( l->next ) printf( ", " ); l = l->next; printf("]"); int main() { int i; link list=null; for ( i = 10; i >0; i-- ) list = cons( i, list ); print(list); printf("\n"); return 0;
35 (November 10, ) Exercises on linked lists Given the following declarations: struct listelem { int item; struct listelem *next; ; typedef struct listelem listelem; typedef struct listelem *link; link cons( int it, link nx ) { link l = (link) malloc( sizeof(listelem) ); l->item = it; l->next = nx; return l; void print( link l ) { putchar( '[' ); while (l) { printf( "%d", l->item); if (l->next ) putchar(','); l = l->next; putchar( ']' );
36 (November 10, ) Exercises on linked lists 1. Write a function that tests if a given value is found in the list. 2. Write a function that stores a value at the end of the list. 3. Write a function that removes all elements of a certain value. 4. Use this list structure to implement a stack and a queue.
37 (November 10, ) Exercises on binary trees 1. Declare a struct to represent a node in a binary tree and write a cons-function to initialize your struct. 2. Write a function that computes the number of nodes in the tree and the height of the tree. The functions should have a pointer to the root node as parameter 3. Write a function that test if two trees are isomorph (have the same structure) 4. Write a function that prints a tree in pre order with one node per line and indentation that shows the structure of the tree, i. e. proportional the depth of the node.
Arrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationAryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9
Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths
More informationC: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36
Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example
More informationProcedural Programming & Fundamentals of Programming
Procedural Programming & Fundamentals of Programming Exercise 3 (SS 2018) 29.05.2018 What will I learn in the 4. exercise Pointer (and a little bit about memory allocation) Structure Strings and String
More informationPointers. Pointer References
Pointers Pointers are variables whose values are the addresses of other variables Basic operations address of (reference) indirection (dereference) Suppose x and y are integers, p is a pointer to an integer:
More informationESC101N: Fundamentals of Computing End-sem st semester
ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) EECS 2031 22 October 2017 1 Be extra careful with pointers! Common errors: l Overruns and underruns Occurs when you reference a memory beyond what you allocated. l Uninitialized
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) CSE 2031 Fall 2011 23 October 2011 1 Be extra careful with pointers! Common errors: Overruns and underruns Occurs when you reference a memory beyond what you allocated. Uninitialized
More informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More informationM4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be
More informationArray Initialization
Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]
More information20 Dynamic allocation of memory: malloc and calloc
20 Dynamic allocation of memory: malloc and calloc As noted in the last lecture, several new functions will be used in this section. strlen (string.h), the length of a string. fgets(buffer, max length,
More informationHigh-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet
High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy
More informationC Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008
C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationPersonal SE. Functions, Arrays, Strings & Command Line Arguments
Personal SE Functions, Arrays, Strings & Command Line Arguments Functions in C Syntax like Java methods but w/o public, abstract, etc. As in Java, all arguments (well, most arguments) are passed by value.
More informationComputer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];
Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data
More informationCS 241 Data Organization Pointers and Arrays
CS 241 Data Organization Pointers and Arrays Brooke Chenoweth University of New Mexico Fall 2017 Read Kernighan & Richie 6 Structures Pointers A pointer is a variable that contains the address of another
More informationCharacter Strings. String-copy Example
Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";
More informationPDS Class Test 2. Room Sections No of students
PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not
More informationQuestion Bank (SPA SEM II)
Question Bank (SPA SEM II) 1. Storage classes in C (Refer notes Page No 52) 2. Difference between function declaration and function definition (This question is solved in the note book). But solution is
More informationLow-Level C Programming. Memory map Pointers Arrays Structures
Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack
More informationCSC209H Lecture 4. Dan Zingaro. January 28, 2015
CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a
More informationCSE2301. Arrays. Arrays. Arrays and Pointers
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationI2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub 2Computer Science BS Degree -Imperative Programming
More informationMain Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University
C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types
More informationLecture07: Strings, Variable Scope, Memory Model 4/8/2013
Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)
More informationCS 237 Meeting 19 10/24/12
CS 237 Meeting 19 10/24/12 Announcements 1. Midterm: New date: Oct 29th. In class open book/notes. 2. Try to complete the linear feedback shift register lab in one sitting (and please put all the equipment
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationCSC 2400: Computer Systems. Arrays and Strings in C
CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays: C vs.
More informationRicardo Rocha. Department of Computer Science Faculty of Sciences University of Porto
Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()
More informationC: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this
More informationJagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming
Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through
More informationStructures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };
Structures EECS 2031 25 September 2017 1 Basics of Structures (6.1) struct point { int x; int y; keyword struct introduces a structure declaration. point: structure tag x, y: members The same member names
More information#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.
EGN 3210 Sample Test 2 Dr. Fernando Gonzalez NAME S.S.# #1. (Functions) Write a function that receives 3 integers, and returns the value of the largest one through the function name. #2. (Functions) What
More informationCSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationCSE2301. Arrays. Arrays. Arrays and Pointers
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationCS 241 Data Organization Binary Trees
CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for
More informationSample Examination. Family Name:... Other Names:... Signature:... Student Number:...
Family Name:... Other Names:... Signature:... Student Number:... THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF COMPUTER SCIENCE AND ENGINEERING Sample Examination COMP1917 Computing 1 EXAM DURATION: 2 HOURS
More informationPointers. Pointers. Pointers (cont) CS 217
Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,
More informationadvanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef
today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation
More informationCSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings
CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Pointer Arithmetic and Element
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationreturn return else return
compare0.c 1 // Compares two strings' addresses 4 #include 5 6 int main(void) 7 { 8 // get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // compare strings'
More informationProblem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.
Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be
More informationContents. A Review of C language. Visual C Visual C++ 6.0
A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source
More informationLinked Data Representations. Data Structures and Programming Techniques
Linked Data Representations 1 Linked Data Representations Linked data representations such as lists, stacks, queues, sets and trees are very useful in Computer Science and applications. E.g., in Databases,
More informationSubject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100
Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationFinal Intro to C Review
Final Exam Content: Final Intro to C Review - Pass by reference Functions - General Syntax - Structures - Recursion(maybe?) - Programming by nature is cumulative so any past material is up for grabs as
More informationECE264 Fall 2013 Exam 3, November 20, 2013
ECE264 Fall 2013 Exam 3, November 20, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationCSC 2400: Computer Systems. Arrays and Strings in C
CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays in C
More informationAPS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:
Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x
More informationIntroduction to string
1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationLinked Data Representations
Linked Data Representations Manolis Koubarakis 1 Linked Data Representations Linked data representations such as lists, stacks, queues, sets and trees are very useful in computer science and applications.
More informationmith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut
mith College CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 2 Hours D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More informationARRAYS(II Unit Part II)
ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful
More informationData Type Fall 2014 Jinkyu Jeong
Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants
More informationStudent Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam
Student Name: (in Capital Letters) CSE 1311 Introduction to Programming for Engineers and Scientists Final Exam Fall 2013 1 1. If count is a properly defined integer variable, the following piece of code:
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationCS 223: Data Structures and Programming Techniques. Exam 2
CS 223: Data Structures and Programming Techniques. Exam 2 Instructor: Jim Aspnes Work alone. Do not use any notes or books. You have approximately 75 minutes to complete this exam. Please write your answers
More informationPrinciples of C and Memory Management
COMP281 Lecture 9 Principles of C and Memory Management Dr Lei Shi Last Lecture Today Pointer to Array Pointer Arithmetic Pointer with Functions struct Storage classes typedef union String struct struct
More informationSYSC 2006 C Winter 2012
SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization
More informationQuestions Bank. 14) State any four advantages of using flow-chart
Questions Bank Sub:PIC(22228) Course Code:-EJ-2I ----------------------------------------------------------------------------------------------- Chapter:-1 (Overview of C Programming)(10 Marks) 1) State
More informationC Review. MaxMSP Developers Workshop Summer 2009 CNMAT
C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,
More informationMemory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)
Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)
More informationWhat have we learned about when we learned about function parameters? 1-1
What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? Call-by-Value also known as scalars (eg. int, double, char,
More informationCreate a Program in C (Last Class)
Create a Program in C (Last Class) Input: three floating point numbers Output: the average of those three numbers Use: scanf to get the input printf to show the result a function to calculate the average
More informationOutline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C
Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationJTSK Programming in C II C-Lab II. Lecture 3 & 4
JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationOne Dimension Arrays 1
One Dimension Arrays 1 Array n Many applications require multiple data items that have common characteristics In mathematics, we often express such groups of data items in indexed form: n x 1, x 2, x 3,,
More informationLinked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Dynamic memory allocation: review typedef struct { int hitemp;
More informationelse return for return
addresses.c 1 // Prints two strings' addresses 4 #include 5 6 int main(void) 7 { 8 // Get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // Print strings' addresses
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationStrings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb
CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs
More informationFrom Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides
From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor
More informationUNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS
UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS Programming Year 2017-2018 Industrial Technology Engineering Paula de Toledo Contents 1. Structured data types vs simple data types 2. Arrays (vectors and matrices)
More informationCS 222: Pointers and Manual Memory Management
CS 222: Pointers and Manual Memory Management Chris Kauffman Week 4-1 Logistics Reading Ch 8 (pointers) Review 6-7 as well Exam 1 Back Today Get it in class or during office hours later HW 3 due tonight
More informationAnswer all questions. Write your answers only in the space provided. Full marks = 50
Answer all questions. Write your answers only in the space provided. Full marks = 50 1. Answer the following: [2+3+2+1=8 marks] a) What are the minimum and maximum numbers that can be represented in 10-bit
More informationFile: /media/young/d1 (180713)/Work tive_tree_search/search_defs.h Page 1 of 1
File: /media/young/d1 (180713)/Work tive_tree_search/search_defs.h Page 1 of 1 #define N 7 #define R N the number of a tree the number of expanding choices = 2*R+1 ---------------------------------------------------------
More informationCOP 3223 Introduction to Programming with C - Study Union - Fall 2017
COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationC-LANGUAGE CURRICULAM
C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals
More informationIntroduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Sean Ogden Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Ayush Dubey Cornell CS 4411, August 30, 2013 Administrative Information
More informationIntroduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Robert Escriva Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Cornell CS 4411, August 30, 2010 1 Why C? 2 A Quick Example 3 Programmer s Responsibilities
More informationMultiple Choice Questions ( 1 mark)
Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s
More information