Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science 210 Computer Organization Introduction to C.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
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.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Arrays. Arrays are objects that help us organize large amounts of information.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Numbers in ‘C’ Two general categories: Integers Floats
CSCE 206 Structured Programming in C
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 3 Assignment and Interactive Input.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
Revision Lecture
CSE1320 Loop Dr. Sajib Datta
ICS103 Programming in C Lecture 3: Introduction to C (2)
Pointers.
Arrays in C.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Functions Declarations CSCI 230
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS1100 Computational Engineering
Arrays, For loop While loop Do while loop
Chapter 11 Introduction to Programming in C
CNG 140 C Programming (Lecture set 8)
CSCE 206 Lab Structured Programming in C
EKT150 : Computer Programming
Chapter 11 Introduction to Programming in C
Introduction To Programming Information Technology , 1’st Semester
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Revision.
Pointers.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Conditionals.
Chapter 11 Introduction to Programming in C
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Files.
Arrays I Handling lists of data.
Functions.
Functions continued.
More on conditional statements
Loops.
Bubble sort.
Let’s start from the beginning
Chapter 11 Programming in C
CSCE 206 Lab Structured Programming in C
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Revision.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 11 Introduction to Programming in C
Presentation transcript:

Arrays

Lab task #1: loops Use your week 1 code (odd or even?) as the baseline You can use our code sample if you don’t have one User must be able to enter multiple numbers without restarting the program User must be able to exit the program when they desire to do so Tips: Place the functional part of your code in a loop Place a prompt in that loop so user can decide when to stop The loop must only be stopped once the user says so – do not count iterations! Use either the loop condition or break statement to stop the loop 2018 Risto Heinsar

From code to a runnable program Preprocessor Deals with trigraphs, macros, line splicing Compiler Creates assembly code Assembler Creates object code that is machine runnable Linker Creates links to other library functions – e.g. where is printf? 2018 Risto Heinsar

#define macros (preprocessor directives) #define macros are processed before compilation (preprocessor) Uses “find and replace” principle Positioned right after #include statements #define <name> <expression> #define SIZE 7 #define TEXTFIELD_LEN 30 #define OPTIONS "settings.ini" Purpose for now: removing magical numbers, using constants Unlike variables, these cannot be changed during runtime! 2018 Risto Heinsar

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 in the array is done by using its index 1-dimensional arrays are also known as vectors 2-dimensional arrays are also known as matrixes 2018 Risto Heinsar

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 14 + 1 chars The arrays can be N-dimensional and the lengths of the dimensions may vary int elements[N][M][K]; 2018 Risto Heinsar

1-dimentional array (vector) When declaring, the number in the square brackets defines array size 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] 2018 Risto Heinsar

Sample 1: Arrays #include <stdio.h> #define NUM_COUNT 3   #define NUM_COUNT 3 int main(void) { int scoreTable[NUM_COUNT]; scoreTable[0] = 93; scoreTable[1] = 85; scoreTable[2] = 51; printf("The students achieved the following results: %d, %d and %d\n", scoreTable[0], scoreTable[1], scoreTable[2]); return 0; } 2018 Risto Heinsar

Sample 2: arrays and loops #include <stdio.h> #define NUM_COUNT 5 int main(void) { int numArr[NUM_COUNT] = {19, 2, -5, 133, 0}; int i; printf("The numbers stored in the array are:\n"); for (i = 0; i < NUM_COUNT; i++) printf("%d\n", numArr[i]); } return 0; 2018 Risto Heinsar

Homework for next week Model 2 algorithms in UML: one using loops and one without Client wishes to withdraw cash from an ATM. Read the desired amount, validate it and output using the minimum amount of bank notes 945€ -> 1x500€, 2x200€, 2x20€, 1x5€ Write a program in C Client wishes to withdraw cash from an ATM. Read the desired amount, validate it and output using the maximum number of unique bank notes 945€ -> 1x500€, 1x200€, 1x100€, 1x50€, 1x20€, 1x10€, 13x5€ Validate! Both 103€ and -20€ should trigger an error. There’s a pseudocode to help you! 2018 Risto Heinsar

Lab task #2: finding min, max – UML The following task must be modelled in UML! User will enter 5 numbers from the keyboard The program will find the minimum and maximum value from those numbers The program will print out the original array The program will print out the minimum and maximum values from that array 2018 Risto Heinsar

Lab task #2: finding min, max – code Create a program based on the algorithm composed previously (slight alterations!) User will input 5 numbers. Those numbers are stored in an array After input, the following result will be printed on the screen: The list of the numbers that were entered The smallest number and the index of that number The biggest number and the index of that number If either the smallest or the biggest number is entered multiple times, display the last occurrence 2018 Risto Heinsar

Advanced tasks Task 1: Task 2: Task 3: Use a suitable formatting Find the minimum and maximum value in the same loop Find the arithmetic mean, sum and product of the given numbers Task 2: If the min/max value is present multiple times, display all of the positions Task 3: Allow the user to specify how many numbers will be entered The absolute maximum is set to 50 numbers Limit the user’s input so that the user couldn’t overflow the array 2018 Risto Heinsar