Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

One Dimensional Arrays
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.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
1 TDBA66, VT-04, Lecture Ch7 One dimensional Arrays A data structure (vector) with many elements of the same type A common name where individual elements.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
 Pearson Education, Inc. All rights reserved Arrays.
Computer Science 210 Computer Organization Pointers.
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.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 8 Arrays and Strings
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
ICS103 Programming in C Lecture 11: Arrays I
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Computer Science 210 Computer Organization
Computer Programming BCT 1113
Functions, Part 2 of 2 Topics Functions That Return a Value
New Structure Recall “average.cpp” program
Arrays and Records.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
Simple Data Types and Function Calls
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
ICS103 Programming in C Lecture 13: Arrays II
Lecture 18 Arrays and Pointer Arithmetic
Functions, Part 2 of 3 Topics Functions That Return a Value
ICS103 Programming in C Lecture 12: Arrays I
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give (enumerate) each of the values which a variable of that type can have typedef enum {entertainment, rent, utilities, food, clothing, miscellaneous} expense_t;

Enumerated Types  We can now use the type expense_t to declare variables expense_t expense_var; –Then the value entertainment will be represented by a 0, rent by a 1, etc. –We can assign values to a variable of this type as follows: expense_var = entertainment; –We can perform any operation on this variable that we can perform on an integer

Enumerated Types 4 Use enumerated types rather than integer codes to make your program more readable 4 Typedefs should be placed just after the precompiler directives (and before any variable declarations which use the type) 4 You cannot read in or print out enumerated types - only the integer equivalents 4 The order of enumeration defines the collation order of the type

Arrays 4 Simple variables use a single storage cell to store a single variable –If we want to store (for example) 5 values, we have to declare 5 variables –Sometimes we process a group of related data items (e.g. exam scores for a class) –In this case, it is awkward to declare individual variables for each value so we use a data structure called an array to hold all of the values

Arrays 4 An array is a collection of two or more adjacent memory cells called array elements  We can declare an array with a single variable declaration, e.g. double x[8]; declares an array of eight doubles –The array has a single name –To refer to individual elements of the array we use the array name with an array subscript

Arrays x[5] = 77.0; printf(“%f\n”, x[5]);  This example refers to the fifth value of the array x  Note that the first value in array x is x[0] - that is, array subscripts start with 0  What is the last element of array x then? 4 Generally, we will always refer to individual elements of the array (i.e. we will always use subscripts with the array name)

Arrays  The declaration of an array of a given type is the same as the declaration of a simple variable of that type except that we follow the name of the array with [size] where size is the size of the array –Note that we don’t specify upper and lower bounds for the array, just the size 4 Parallel arrays store related information in corresponding fields of two separate arrays

Array Initialization 4 A simple variable can be initialized when it is declared: int int_var = 0; –An array can also be initialized when it is declared int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; –Note that we don’t need to give the size of the array if we initialize it in the declaration (otherwise, we do)

Array Subscripts 4 A subscript can be an integer literal, a constant given in a #define, or any expression which evaluates to an integer value –The expression must evaluate to a value in the range of 0 to size of array - 1, otherwise a run- time error will occur –The expression cannot evaluate to a floating point number

Examples i = 5; x[0]=16.0; x[1]=12.0; x[2]=6.0; x[3]=8.0; x[4]=2.5; x[5]=12.0; x[6]=14.0; x[7]=-54.5; printf( “%d %.1f“, 4, x[4]); printf( “%d %.1f“, i, x[i]); printf( “%.1f“, x[i] + 1); printf( “%.1f“, x[i] + i); printf( “%.1f“, x[i + 1]); printf( “%.1f“, x[i + i]); printf( “%.1f“, x[2 * i]);

Examples printf( “.1f“, x[2 * i - 3]); printf( “.1f“, x[(int)x[4]]); printf( “.1f“, x[i++]); printf( “.1f“, x[--i]); x[i-1] = x[i]; x[i] = x[i+1]; x[i] - 1 = x[i];

Using for Loops with Arrays 4 Since the elements of an array are related, we often want to perform the same processing on each element of the array 4 We can do this by using a for loop to step through the array, element-by-element –The loop variable is initialized to the lower bound of the array –The condition is to exit when the upper bound is reached

Using for Loops with Arrays –The loop variable is used as the subscript of the array in the loop body for (i = 0; i < SIZE; ++i) square[i] = i * i; for (j = 0; j < SIZE; j++) printf(“The score for %d is %d\n”, j, score[j]); for (k = 0; k < MAX_ITEM; ++k) scanf(“%lf”, &x[k]);

Using for Loops with Arrays #include #define MAX_ITEM 8 int main(void) { double x[MAX_ITEM], mean, st_dev, sum, sum_sqr; int i; printf(“Enter %d numbers separated by blanks “ “or s\n”, MAX_ITEM); for (i = 0; i < MAX_ITEM; ++i) scanf(“%lf”, &x[i]); sum = 0; sum_sqr = 0;

Using for Loops with Arrays for (i = 0; i < MAX_ITEM; ++i) { sum += x[i]; sum_sqr += x[i] * x[i]; } mean = sum / MAX_ITEM; st_dev = sqrt(sum_sqr / MAX_ITEM - mean*mean); prinf(“The mean is %.2f.\n”, mean); printf(“Standard deviation is %.2f.,st_dev); printf(“\nTable of differences\n); printf(“IndexItemDifference\n”); for (i = 0; i < MAX_ITEM; ++i) printf(“%3d%4c%9.2f%5c%9.2F\n”,i,’ ‘,x[i], ‘ ‘,x[i]-mean); return(0); }

Using Array Elements as Function Arguments 4 Individual array elements can be used as actual array arguments –printf(“%d\n”,x[i]); –scanf(“%d %d”, &x[0], &x[1]); 4 The array of the element which we are passing must be of the same type as the formal argument of the function –Note that an array element can be used as either an input argument - x[i] - or an output argument - &x[0]

Array Arguments 4 We can also write functions that have arrays as arguments (i.e. that manipulate all or part of an array) 4 We can declare an array function as an argument with no subscript –int list[] –This declaration allows us to use the array as an output parameter or input/output parameter (but not input only)

Array Arguments 4 An important point is that in the argument declaration we don’t indicate the size of the array –If we want to do this, we must use another argument for this purpose –int list[], int size_of_list 4 C does not allocate memory for a local copy of the array, instead it passes the address of the first element of the array

Array Arguments 4 Given a formal argument for an array of the type shown previously, we can call the function by using the name of the array as the actual argument in the function call: func_name(my_array, ARRAY_SIZE); –We don’t use a subscript in the function call –The actual argument is just the first element of the array, so we could also use: func_name(&my_array[0], ARRAY_SIZE); –The first version is preferred

Array Arguments  In the formal parameter list, instead of int list[], we could also int *list –This is because C passes the address of the first element of the array –You probably won’t use this form until we start to work with strings  ANSI C we can include a qualifier in the declaration of the array formal parameter to notify the compiler that the array is input only - void my_func(const int my_array[]);

Array Arguments 4 In C, it is not legal for a function’s return type to be an array –We must therefore use an output argument to return the array –The calling function must supply an array for the output results (i.e. the calling function must declare an array of the desired size and pass the address of the array, but it need not initialize the array)

Array Arguments 4 It is often the case that we don’t know exactly how many data items we must process in a program –Suppose we are processing these data items in an array –We can declare the array to be as large as the largest data set we will have to process and then just use a part of the array if the data set is smaller than that