Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,

Slides:



Advertisements
Presentaciones similares
Mr. Redaelli OnlineTaco.com. To Have - Tener Yo tengo – I have Tú tienes – You have (Informal) Usted tiene – You have (Formal) El tiene – He has Ella.
Advertisements

Game Cluedo: How to Play 1.Your group should have the 21 cards containing 6 cards of suspects, 9 rooms and 6 weapons, a tally card for each member and.
Ejemplo de Programa C++
______________________ ¿Qué hay en tu insti? L/O: To use the imperfect tense to compare primary school with secondary school. Para empezar: cambia una.
Negatives and Questions. Negatives Consider the following sentences: Juan estudia mucho. Marta y Antonio viven en Georgia. Rita y el chico necesitan.
Los números ordinales (first, second, third…). Los números ordinales  1—primero*  2—segundo  3—tercero*  4—cuarto  5—quinto  6—sexto  7—séptimo.
Programación orientada a objetos
Sección 5-2 Estructuras de Control de Decisión Expresiones Lógicas.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
Chapter 6 Looping Dale/Weems/Headington. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File.
First Grade – High Frequency Word Reading Competition Classroom Competition Created by: Malene Golding School Improvement Officer: Kimberly Fonteno.
¿Qué haces en la escuela? Question words, objects, yo-go’s.
Vocab: el día escolar Grammar: Subject Pronouns & Present Tense of –ar Verbs.
ALC 16 Bienvenida jueves el 6 de octubre. ALC 16 jueves el 6 de octubre Conjugate Yo bailar Tú escuchar Usted salir Ella abrir Nostros comer Vosotros.
CONJUGATION.
ALC 25 You will need your vocabulary list from the last assignment.
Preparacion Hoy es viernes el 4 de diciembre. Today is Friday, December 4. DO NOW: In preparation for some exercises in class do the following with the.
Unidad I, Lección 4 La América Central y México. 23/9 Bellringer Take down the vocabulary notes which are located on the next 3 slides. The first two.
Escribe las tres formas para presentarte (Write down three ways to introduce yourself) 1. _______________________________ 2.________________________________.
Forming Questions ¡Aprenda! Forming Questions By Patricia Carl October 2013.
ALC 7 Viernes el 9 de septiembre. ALC 7 Emparejar: Use your map to help match Venezuela Colombia Ecuador Perú Bolivia Paraguay Chile Argentina Uruguay.
Foreign Language Teaching Tips Rich Bradshaw. ¡Classroom Vocabulary Scavenger Hunt! Purpose Introduce new vocabulary words in TL Identify items using.
PROF. RAFAEL MONTENEGRO B. UNELLEZ-APURE Introducci Ó n a los Arreglos (“arrays”) en C++
Campanada guidelines in your composition notebook ¿Cómo es tu familia? (10 points) Ex. Hoy es Miercoles el 27 de enero First line will start with the date.
Preparación/ Review Hoy es miércoles el 23 de septiembre de 2015 Today is day, September 23, 2015 ¿Te gusta o no te gusta? What do you like and what you.
Objective Students will learn to tell time and be able to use the numbers from last lesson as a review.
Stamps to be traced. 2 and 3 ALC 21 Hoy es miércoles el 23 de octubre. match the columns writing out the words. 1.Fat 2.Handsome 3.Long 4.Old 5.Pretty.
THE VERB ESTAR Español 1 – Unidad 2 Lección 2. THE VERB ESTAR The verb estar means TO BE and is used to indicate LOCATION as well as to say how people.
Welcome! Conversation Classes. Remembering Names Everyone Stand! Introduce yourself to class and give your name an action! Go around the class and try.
EQUILIBRIUM OF A PARTICLE IN 2-D Today’s Objectives: Students will be able to : a) Draw a free body diagram (FBD), and, b) Apply equations of equilibrium.
IntroducciÓn a los Arreglos (“arrays”) en C++
Quasimodo: Get ready for your quiz!.
Asking Questions P. 184 Realidades 1.
KEEP YOUR Handout: Plural artículos y sustantivos
EspaÑol 1 Lesson 3 30 de Agosto de 2016
Las clases de Sra. Schwarz Realidades 1
Lenguaje C++ Problemas.
Double Object Pronouns
Español 1 11 de mayo de 2017 Good morning, thank you for taking my classes today. For the most part the kids are really great. Here is my schedule for.
October 21st assignment Read through and practice with this PowerPoint. Ask and discuss it with each other if you have a question. Answer the following.
SER and SUBJECT PRONOUNS
Searching data in Arrays
Español 2 5 de diciembre de 2016
First Grade Dual High Frequency Words
Warm-up Fill in the blank with the correct form of the verb “ser” for each subject (p. 35): 1. Yo _______________ de Savannah. 2. Mis amigas ________.
¿Qué hora es?.
GRAPHIC MATERIALS 1. GRAPHIC MATERIALS. GRAPHIC MATERIALS 1. GRAPHIC MATERIALS.
Algebra I By Monica Yuskaitis. Definitions Variable – A variable is a letter or symbol that represents a number (unknown quantity). 8 + n = 12.
Asking Questions P. 184 Realidades 1.
Para hacer ahora Identifiquen los números
Recetas 3 Objetivo: Hacer preguntas Hablar sobre la comida
Quasimodo: Tienes que hacer parte D de la tarea..
1.4 Numbers.
Apuntes: La hora Lección 1: Hola, ¿Qué tal?.
Telling time in Spanish
Asking Questions P. 184 Realidades 1.
How to Conjugate… SPANISH VERBS.
Asking Questions P. 184 Realidades 1.
Asking Questions P. 184 Realidades 1.
Preparacion Hoy es jueves el 3 de diciembre
Vamonos Write a full sentence for each class, including one thing that you would need for that class. Ex. Math → Para la clase de matemáticas yo necesito.
The Windows File System and Windows Explorer To move around the file system and examine your files or get to one you want (say, to modify, delete or copy.
Los adjetivos demostrativos Notes #16 What is a demonstrative adjective in English? Demonstrative adjectives in English are simply the words: THISTHESE.
Gustar, Interesar, Aburrir
Development of the concert programme
How to use “Gustar”.
Welcome to PowerPoint gdskcgdskfcbskjc. Designer helps you get your point across PowerPoint Designer suggests professional designs for your presentation,
Astronomy has really big numbers. Distance between Earth and Sun meters kilometers This is the closest star.
The causative is a common structure in English. It is used when one thing or person causes another thing or person to do something.
Transcripción de la presentación:

Arrays Programming

COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).

COMP102 Prog. Fundamentals I: Arrays / Slide 3 Arrays l 1-dimensional array. l 3-dimensional array (3rd dimension is the day). Oct 15 Oct 16 Oct 14

COMP102 Prog. Fundamentals I: Arrays / Slide 4 Array Applications l Given a list of test scores, determine the maximum and minimum scores. l Read in a list of student names and rearrange them in alphabetical order (sorting). l Given the height measurements of students in a class, output the names of those students who are taller than average.

COMP102 Prog. Fundamentals I: Arrays / Slide 5 Array Declaration l Syntax: [ ] Ex. int Ar[10]; The array elements are all values of the type. The size of the array is indicated by, the number of elements in the array. must be an int constant or a constant expression. Note that an array can have multiple dimensions.

COMP102 Prog. Fundamentals I: Arrays / Slide 6 Array Declaration // array of 10 uninitialized ints int Ar[10]; -- Ar

COMP102 Prog. Fundamentals I: Arrays / Slide 7 Subscripting l Declare an array of 10 integers: int Ar[10]; // array of 10 ints To access an individual element we must apply a subscript to array named Ar. n A subscript is a bracketed expression. –The expression in the brackets is known as the index. n First element of array has index 0. Ar[0] n Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… n Last element has an index one less than the size of the array. Ar[9] l Incorrect indexing is a common error.

COMP102 Prog. Fundamentals I: Arrays / Slide 8 Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- 1 Ar Ar[4]Ar[5]Ar[6]Ar[3]Ar[0]Ar[2]Ar[8]Ar[9]Ar[7]Ar[1] 1 --

COMP102 Prog. Fundamentals I: Arrays / Slide 9 Subscripting Example 1 //For loop to fill & print a 10-int array #include using namespace std; int main ( ) { int index, ar[10]; // array for 10 integers // Read in 10 elements. cout << "Enter 10 integers: "; for(index = 0; index < 10; index ++) cin >> ar[index]; cout << endl; cout << "The integers are "; for(index = 0; index < 10; index ++) cout << ar[index] << " "; cout << endl; return 0; }

COMP102 Prog. Fundamentals I: Arrays / Slide 10 Sorting with Arrays: Ex. 2 // Compare and sort three integers void swap (int&, int&); int main ( ) { int ar[3]; // input integers // Read in three elements. cout << "Enter three integers: "; cin >> ar[0] >> ar[1] >> ar[2]; if (ar[0] > ar[1]) swap (ar[0], ar[1]); if (ar[1] > ar[2]) swap (ar[1], ar[2]); if (ar[0] > ar[1]) swap (ar[0], ar[1]); cout << "The sorted integers are " << ar[0] <<", " << ar[1] << ", " << ar[2] << endl; return 0; }

COMP102 Prog. Fundamentals I: Arrays / Slide 11 Swapping Function: Ex. 2 // Function for swapping two integers void swap (int& first, int& second) { int temp; temp = first; first = second; second = temp; }

COMP102 Prog. Fundamentals I: Arrays / Slide 12 Array Element Manipulation Ex. 3 l Consider int Ar[10], i = 7, j = 2, k = 4; Ar[0] = 1; Ar[i] = 5; Ar[j] = Ar[i] + 3; Ar[j+1] = Ar[i] + Ar[0]; Ar[Ar[j]] = 12; cin >> Ar[k]; // where the next input value is Ar Ar[4]Ar[5]Ar[6]Ar[3]Ar[0]Ar[2]Ar[8]Ar[9]Ar[7]Ar[1]

COMP102 Prog. Fundamentals I: Arrays / Slide 13 Array Initialization Ex. 4 int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; Ar[3] = -1; Ar Ar

COMP102 Prog. Fundamentals I: Arrays / Slide 14 Initializing arrays with random values The following loop initializes the array myList with random values between 0 and 99: for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = rand() % 100; }

COMP102 Prog. Fundamentals I: Arrays / Slide 15 Program with Arrays int main() { int values[5]= {11,1,3,6,10}; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 16 Printing arrays To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < ARRAY_SIZE; i++) { cout << myList[i] << " "; }

COMP102 Prog. Fundamentals I: Arrays / Slide 17 Copying Arrays Can you copy array using a syntax like this? list = myList; This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < ARRAY_SIZE; i++) { list[i] = myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 18 Summing All Elements Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: double total = 0; for (int i = 0; i < ARRAY_SIZE; i++) { total += myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 19 Finding the Largest Element Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element in myList with max, update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 20 Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; }

COMP102 Prog. Fundamentals I: Arrays / Slide 21 Shifting Elements double temp = myList[0]; // Retain the first element // Shift elements left for (int i = 1; i < myList.length; i++) { myList[i - 1] = myList[i]; } // Move the first element to fill in the last position myList[myList.length - 1] = temp;

#include // Random Shuffling #include using namespace std; const int ARRAY_SIZE = 10; int main(){ int myList[ARRAY_SIZE]= {1,2,3,4,5,6,7,8,9,10}; srand(time(0)); for ( int i = 0; i < ARRAY_SIZE; i++) { // Generate an index randomly int index = rand() % ARRAY_SIZE; int temp = myList[i]; myList[i] = myList[index]; myList[index] = temp; } for ( int i = 0; i < ARRAY_SIZE; i++) cout << myList[i] <<' '; cout << endl; return 0;}

#include // Create 6 random numbers for Mark 6 #include using namespace std; const int ARRAY_SIZE = 6; int main(){ int used[50] ={0};//indicate if a random number is used. int Mark[6], temp; srand(time(0)); int i = 0; while (i < 6){ temp = rand()%49 + 1; if (used[temp] == 0) {//temp is not used Mark[i] = temp; i++; used[temp] = 1; } for ( int i = 0; i< 6; i++) cout << Mark[i] << endl; return 0; }

#include //Print numbers in reversed order #include using namespace std; int main(){ srand(time(0)); int numbers[500]; int num_num; cout<< "How many numbers (up to 500)?"; cin >> num_num; for ( int i= 0; i < num_num; i++) numbers[i] = rand()%100; cout << "\n Your numbers reversed are:\n"; for ( int j= num_num-1 ; j >= 0; j--) cout << setw(3) << numbers[j]; cout << endl; return 0;}

#include //print histogram #include using namespace std; int main(){ ifstream ins; int count[26]={0}; char next; ifstream input_stream; char input_file_name[16]; // declare the input file names cout << "Enter the input file name (max 15 characters):\n"; cin >> input_file_name; ins.open(input_file_name); // open the file ins >> next; // get the first char while(!ins.eof()){ // loop to read each line next = tolower(next); if (isalpha(next)) count[ next - 'a']++; ins >> next; } ins.close(); for (char i = 'a'; i <='z'; i++) // print_histogram(count); cout << i << ": " << count[i-'a'] << endl; return 0; }

void print_histogram( int list[]){ int max = list[0]; for (int i = 1; i < 26; i++){ if (max < list[i]) max = list[i]; } double ratio= 70.0 /max ; for ( i = 0; i < 26; i++){ cout << char ('a'+i) <<": “ << setw(5) <<list[i] <<‘ ‘; int count = list[i]* ratio; for ( int j = 0; j < count; j++) cout <<'*'; cout << endl; } cout << endl; }