Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
C Language.
Introduction to arrays
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Hand Crafting your own program By Eric Davis for CS103.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 8 Arrays and Strings
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
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.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
Computer Programming for Engineers
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Module 1: Array ITEI222 - Advance Programming Language.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Arrays. Arrays are objects that help us organize large amounts of information.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays, For loop While loop Do while loop
CSCE 206 Lab Structured Programming in C
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
INC 161 , CPE 100 Computer Programming
Functions continued.
Arrays.
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Arrays

The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double) Characters (char) // character array is also known as a string etc Array elements are indexed using integers starting from zero Accessing a member of array is done by using its’ index 1-dimensional arrays are also known as vectors 2-dimensional arrays are also known as matrixes 20152

Declaring an array The size of an array is set at the time of declaration by the number in the square brackets int numbers[10] // an array for 10 integers char textField[15] // array for chars The arrays can be N-dimensional and the lengths of the dimentions may vary int elements[N][M][K] 20153

1-dimentional array (vector) The number in the square brackets sets the size for the array int numbers[5]; When accessing a member of the array, we specify the index of the element in the square brackets numbers[index] To print out the third element of an array we would use: printf("%d", numbers[2]); numbers[0]numbers[1]numbers[2]numbers[3]numbers[4] 20154

Lab task #1 Create an algorithm and based on that a program User will input 5 numbers, that will be stored in an array After input, the following result will be printed on the screen: The list of the numbers that the user entered The smallest number entered The biggest number entered 20155

Improvements for the advanced Make the following changes to the code you wrote: Find the average, sum and product of the numbers entered Format Your output for better readability – try things like "%3d", "%3.2f" etc Allow the user to specify the number of elements that will be inputted

#define macros (preprocessor directive) #define macros are fulfilled before the code is compiled Works like “find and replace” The #define macros are placed right after the #include directives #define #define SIZE 7 #define TEXTFIELD_LEN 30 #define OPTIONS "settings.ini" Priority for now: avoid magic numbers, constants in code 20157

2-dimensional array (matrix) In this case we have a square matrix (both dimensions are equal) int array[5][5] array[0][0]array[0][1]array[0][2]array[0][3]array[0][4] array[1][0]array[1][1]array[1][2]array[1][3]array[1][4] array[2][0]array[2][1]array[2][2]array[2][3]array[2][4] array[3][0]array[3][1]array[3][2]array[3][3]array[3][4] array[4][0]array[4][1]array[4][2]array[4][3]array[4][4] 20158

Going through all of the elements #include int main(void) { int i, j, numArray[5][5]; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { numArray[i][j] = 0; } return 0; } 20159

Lab task #2 Create a 2-dimensional array for 10x10 multiplication table Avoid using magic numbers when declaring the array and limiting the loops The multiplication table will be stored in the array created →Use for() loops for this! The multiplication table is displayed from the array you generated →Use while() loops for this! Format your output for better visuals →Put line breaks at the right time →Use formatted output when printing numbers - %4d

Home task Modify your multiplication table, so that: The user can enter the size (width x length) Limit the size that can be entered based on what your program window can actually fit When the user inputs a size that’s out of bounds, ask again – use a do while() loop for this. You must be able to repeat the generation process without restarting the program. Create an algorithm with the state of lab task #2 (don’t include improvements from home task)