Arrays I Handling lists of data.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
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.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
A First Book of ANSI C Fourth Edition
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Previously Repetition Structures While, Do-While, For.
ICS103 Programming in C Lecture 11: Arrays I
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.
The ‘while’ loop ‘round and ‘round we go.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Final Review1 Final Exam l Final Exam: Thursday December 15, 2005 at 8:30 pm in room MP-008, the regular classroom. If you can not make it to the exam.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 CSC 211 Data Structures Lecture 4 Dr. Iftikhar Azim Niaz 1.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
ARRAYS.
Arrays.
UMBC CMSC 104 – Section 01, Fall 2016
while Repetition Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays Declarations CSCI N305
Repetition-Counter control Loop
Arrays in C.
Control Statement Examples
The C “switch” Statement
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
The C “switch” Statement
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
One-Dimensional Array Introduction Lesson xx
Final Exam Final Exam: Thursday Dec 13th, 2001 at 8:30 pm in SS-111, the regular classroom.
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
The switch Statement Topics Multiple Selection switch Statement
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
The ‘while’ loop ‘round and ‘round we go.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
REPETITION STATEMENTS
Arrays Topics Definition of a Data Structure Definition of an Array
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Suggested self-checks: Section 7.11 #1-11
Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Variables in C Topics Naming Variables Declaring Variables
The while Looping Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Topics Definition of a Data Structure Definition of an Array
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Arrays I Handling lists of data

Arrays A data structure is a group of related data items. The array is one kind of data structure. An array is a group of related data items that all have the same name and the same data type. Arrays are static in that they remain the same size throughout program execution.

Arrays An array is a sequence of data items, all of the same type that are stored contiguously in memory. Each of the data items is known as an element of the array We can access the individual elements of the array using an indexing scheme Arrays can be of any type we choose.

Array Declarations int array [5] ; This declaration sets aside a chunk of memory that’s big enough to hold 5 integers. It does not initialize those memory locations to 0 or any other value. Initializing an array may be done with an array initializer, as in : int array [5] = { 5, 2, 6, 9, 3 } ; array 5 2 6 9 3 0 1 2 3 4

Indexing Array Elements Values of individual elements can be found by indexing into the array. In our example, array [0] is equal to 5 and array [3] is equal to 9. The integer in square brackets is called the subscript. The subscript could also be an expression that evaluates to an integer. In our example, array is the name of the array.

Modifying Elements Individual elements of the array can also be modified using subscripts. array [4] = 20 ; /*changes the value of the element found at subscript 4 to 20 */ Values may be stored in an array using indexing, rather than using the array initializer.

Filling Arrays Since many arrays are quite large, using an array initializer is impractical. Large arrays are often filled using a for loop. for ( i = 0; i < 100; i++) { rolls [ i ] = 0 ; } would set every element of the 100 element array, rolls, to 0.

More Declarations int score [39] , gradeCount [5]; Declares two arrays of type int Neither array has been initialized score contains 39 elements (one for each student in the class) gradeCount contains 5 elements (one for each possible grade, A-F)

Using #define for array sizes #define SIZE 39 #define GRADES 5 main ( ) { int score [SIZE] ; int gradeCount [GRADES] ; } We often use the #define to give the sizes of arrays.

Grades Example /* Fill score array with scores */ for ( i = 0 ; i < SIZE ; i++) { printf (“Enter next score : ”) ; scanf (“%d “, &score [ i ] ); } /* Calculate total & count grades */ total += score [ i ] ; switch ( score [ i ] / 10 ) case 10 : case 9 : gradeCount [4]++ ; break ; case 8 : gradeCount [3]++ ; #include <stdio.h> #define SIZE 39 #define GRADES 5 void PrintInstructions (void) ; double FindAver (double sum, int num) ; main ( ) { int i, total, score [SIZE] ; int gradeCount [GRADES] ; double average; PrintInstructions ( ) ; /* Initialize gradeCount array to 0s */ for ( i = 0; i < GRADES; i++ ) gradeCount [ i ] = 0 ; }

Grades Example continued case 7 : gradeCount [2]++ ; break ; case 6 : gradeCount [1]++ ; default : gradeCount [0]++ ; } average = FindAver (total, SIZE) ; /* Print results */ printf (“The class average is %.2f\n”, average ) ; printf (“There were %2d As\n”, gradeCount [4] ) ; printf (“ %2d Bs\n”, gradeCount [3] ) ; printf (“ %2d Cs\n”, gradeCount [2] ) ; printf (“ %2d Ds\n”, gradeCount [1] ) ; printf (“ %2d Fs\n”, gradeCount [0] ) ; } /* PrintInstructions prints the greeting * for the user, explaining the program’s * purpose, input and output */ void PrintInstructions (void) { printf (“This program calculates ”) ; printf (“the average score for a \n” ) ; printf (“class of 39 students. It “) ; printf (“also reports the number\n”) ; printf (“of A’s, B’s, etc. You will be”) ; printf (“asked to enter the\n”); printf (“individual scores\n” ) ;

Grades Example continued /* FindAver finds and returns the * average when passed the sum of * some items and the number of * items. */ double FindAver (double sum, int num) { double average; average = sum / num ; return average; }

Improvements ? We’re trusting the user to enter valid grades. Let’s add input error checking. If we aren’t handling our array correctly, it’s possible that we may be evaluating garbage rather than valid scores. We’ll handle this by adding all the cases for Fs to our switch structure and using the default case for reporting errors. We still have the “magic numbers” 4, 3, 2, 1, and 0 that are the quality points associated with grades. Let’s use constants for these values.

Improved Grades Example /* Initialize gradeCount array to 0s */ for ( i = 0; i < GRADES; i++ ) { gradeCount [ i ] = 0 ; } /* Fill array with valid scores */ for ( i = 0 ; i < SIZE ; i++) printf (“Enter next score : ”) ; scanf (“%d “, &score [ i ] ) ; while ( score [ i ] > MAX || score [ i ] < MIN ) printf (“Scores must be between”); printf (“ %d and %d\n”, MIN, MAX); #include <stdio.h> #define SIZE 39 #define GRADES 5 #define A 4 #define B 3 #define C 2 #define D 1 #define F 0 #define MAX 109 #define MIN 0 void PrintInstructions (void) ; double FindAver (double sum, int num) ; main ( ) { int i, total, score [SIZE] ; int gradeCount [GRADES] ; double average; PrintInstructions( ) ;

Improved Grades Example continued case 0 :gradeCount [F]++ ; break; default : printf (“Error in score\n”); } average = FindAver (total, SIZE) ; /* Print results */ printf (“The class average is %.2f\n”, average ) ; printf (“There were %2d As\n”, gradeCount [A] ) ; printf (“ %2d Bs\n”, gradeCount [B] ) ; printf (“ %2d Cs\n”, gradeCount [C] ) ; printf (“ %2d Ds\n”, gradeCount [D] ) ; printf (“ %2d Fs\n”, gradeCount [F] ) ; /* Calculate total & count grades */ for ( i = 0 ; i < SIZE ; i++) { total += score [ i ] ; switch ( score [ i ] / 10 ) case 10 : case 9 : gradeCount [A]++ ; break ; case 8 : gradeCount [B]++ ; case 7 : gradeCount [C]++ ; case 6 : gradeCount [D]++ ; case 5 : case 4 : case 3 : case 2 : case 1 :

Grades Example continued /* PrintInstructions prints the greeting * for the user, explaining the program’s * purpose, input and output */ void PrintInstructions (void) { printf (“This program calculates ”) ; printf (“the average score for a \n” ) ; printf (“class of %d students.“, SIZE) ; printf (“ It also reports the number”) ; printf (“\nof A’s, B’s, etc. You will”) ; printf (“ be asked to enter the\n”); printf (“individual scores\n” ) ; } /* FindAver finds and returns the * average when passed the sum of * some items and the number of * items. */ double FindAver (double sum, int num) { double average; average = sum / num ; return average; }

Other Improvements ? Why is main so large ? Couldn’t we write functions to : Initialize an array to hold all 0s ? Fill an array with values input by the user ? Count the grades, and find the class average ? Print the results ? We can as soon as we learn about passing arrays to functions, in the next lecture.