Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.

Slides:



Advertisements
Similar presentations
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Class 3 CSI2172
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
1 Array Knowledge Understand the execute technique of array Skill Can write application program using one and two dimensional array.
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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
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",
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 22, 2002.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Outline Midterm results Static variables Memory model
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
WEEK 4 Class Activities Lecturer’s slides.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
While loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits.
Introduction to C programming. History of C programming Invented and Developed by Dennis Ritchie and Brian Kernighan at Bell Laboratories in 1972 Predecessor.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
 Recursion  Pointers and Arrays #include void print3(int n){ if (n==0)return; printf ("%d\n",n); print3(n-1); printf ("%d\n",n); } void main(){ print3(5);
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS
Functions Dr. Sajib Datta
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Hassan Khosravi / Geoffrey Tien
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
Introduction to Programming and the C Language
Structure ការណែនាំអំពី Structure
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
C Programming Pointers
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
Presentation transcript:

Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.

Arrays Pseudo-code #include int main() { /* declare variables */ /* read in 20 integers */ /* Ask User */ /* display results */ return 0; }

Arrays Pseudo-code #include int main() { int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13, i14,i15,i16,i17,i18,i19,i20; int ans; printf(“Enter number 1: “); scanf(“%d”, &i1); printf(“Enter number 2: “); scanf(“%d”, &i2); … /* Ask User */ displayResults(ans,i1,i2,i3,i4,i5,i6,i7,i8,i9, i10,i11,i12,i13,i14,i15,i16,i17,i18, i19, i20); return 0; }

Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt] ); cnt++; } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i]); else if( !(nums[i]%2) ) printf(“%d “, nums[i]); return; }

Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt++] ); } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i] ); else if( !(nums[i]%2) ) printf(“%d “, nums[i] ); return; }

Arrays Referencing Arrays –identifier[index] –NOTE: index can be any expression Examples –int x[10]; –x[2+1] = 5; –x[3] = x[3] + num; (same as x[3] += num;) –x[5] = x[z] + x[3]; –sum = x[3] + x[f(r)];

Arrays Memory –int i[5]; –NOTE: Indexing starts with 0! i[0] i[1]i[2]i[3]i[4]

Arrays Initializing arrays –datatype identifier[array_size] = { init list }; Examples –int x[3] = { 2, 4, 8}; –char v[] = { ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ }; 248 x[0] x[1]x[2] aeiou v[0] v[1]v[2]v[3]v[4]

Functions, Arrays, and Pointers call-by-reference –Array int f(int n[]); int x[5]; f(x); int f(int n[]); int x[5]; f(&x[0]); int f(int *n); int x[5]; f(x); int f(int *n); int x[5]; f(&x[0]); –Why are these all equivalent?

Functions, Arrays, and Pointers Main Data Area x Function f data Area n

Functions, Arrays, and Pointers Pointer and Array equivalence int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9]; for (i = 0; i < 10; i++) printf(“%d “, x[i]);

Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt++] ); } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i] ); else if( !(nums[i]%2) ) printf(“%d “, nums[i] ); return; }