Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.

Slides:



Advertisements
Similar presentations
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Chapter 4 Summation.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Function Problems. Write Functions Asks users whether the user wants to continue of not, then returns the answer. Takes two integers and returns 1 if.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Passing Objects to Methods
MT262A Review.
while Repetition Structure
Chapter 7 – Arrays.
Two Dimensional Array Mr. Jacobs.
Recursion Chapter 12.
Array An “average.cpp” program
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Engineering Problem Solving with C++, Etter
Arrays Part-1 Armen Keshishian.
New Structure Recall “average.cpp” program
Multi-dimensional Array
C++ Arrays.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Object-Oriented Programming (OOP) Lecture No. 32
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Introduction to Programming
Value returning Functions
Working With Arrays.
Arrays Kingdom of Saudi Arabia
CS-161 Computer Programming Lecture 14: Arrays I
Counting Loops.
Starting Out with C++: From Control Structures through Objects
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Arrays of Two-Dimensions
CHAPTER 2 Arrays and Vectors.
Array Data Structure Chapter 6
C++ winter2008(by:J.Razjouyan)
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Array What it is. How to use it How to declare it How to initialize it.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
getline() function with companion ignore()
Presentation transcript:

Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements of the same index of T1 and T2 in the first 10 elements of T3 the results of the multiplication of each elements of the same index of T1 and T2 in the last 10 elements of the array T3. Display T3.

Function 1 : Enter values T1 & T2 Display prompt to type 10 values Enter T1[0] Enter T1[1] Enter T1[2] ………. Enter T1[10] 3 5 1 4 6 11 2 33 T1 cout << "Please enter " << X << " integer elements of the first array." << endl; for(int count = 0; count < X; ++count) { cout << "array1[" << count << "]: "; cin >> T1[count]; } 10 is wrong in location to the right only 9 because from 0 to 9 X = 10

Function 1 : Enter values T1 & T2 Display prompt to type 10 values Enter T2[0] Enter T2[1] Enter T2[2] ………. Enter T2[10] 2 5 20 3 4 8 6 T2 cout << "Please enter " << X << " integer elements of the second array." << endl; for(int count = 0; count < X; ++count) { cout << "array2[" << count << "]: "; cin >> T2[count]; } X = 10

Function 1 : Enter values T1 & T2 void inputFunc(int T1[], int T2[], int X) { cout<<"Please enter "<<X<<" integer elements of the first array."<<endl; for(int count = 0; count < X; ++count) { cout << "array1[" << count << "]: "; cin >> T1[count]; } cout <<"Please enter "<<X<<" integer elements of the 2nd array."<<endl; { cout << "array2[" << count << "]: "; cin >> T2[count]; Passing array to function

Function 2 : Calculation 3 5 1 4 6 11 2 33 T1 2 5 20 3 4 8 6 T2 5 10 21 7 4 19 8 36 6 25 20 12 3 24 88 99 T3[0] =T1[0]+T2[0] T3[10]=T1[0]*T2[0] T3[1] =T1[1]+T2[1] T3[11]=T1[1]*T2[1] T3[i] =T1[i]+T2[i] T3[i+10]=T1[i]*T2[i]

Function 2 : Calculation void calc(int T1[],int T2[],int T3[], int X) { for (int i = 0; i < X; i++) T3[i]=T1[i]+T2[i]; T3[i+10]=T1[i]*T2[i]; } Here both operation will happen at the same time one after the other

Function 3 : Display array void printarray (int T[], int X) { for (int i=0; i<2*X; ++i) cout << T[i] << "\t"; cout << endl; } \t : horizontal tab (move cursor to next tab)

Main function int main () { const int X = 10; int T1[X];int T2[X];int T3[2*X]; inputFunc(T1, T2, X); calc(T1, T2, T3, X); printarray(T3, X); return 0; }

Example 2 Ask the user to type10 integers of an array T. Find and display the smallest and the greatest value in T. Find and display the frequency of each element of T.

Function 1 : Enter values T void inputFunc(int T[], int X) { cout << "Please enter " << X << " integer elements of an array." << endl; for(int count = 0; count < X; ++count) cout << "array[" << count << "]: "; cin >> T[count]; } X = 10

Function 2:frequency of each element Compare each T[i] with all the elements of T i : 0 to 9 compare T[i] with T[0], T[1],……, T[9] j : 0 to 9.

Function 2:frequency of each element 3 5 4 1 Frequency T[0] =T[0] : Frequency [0] = Frequency[0] + 1 T[0] ! =T[1] : T[0] =T[2] : Frequency [0] = Frequency[0] + 1 ……….. j from 0 to 9 T[0] =T[j] : if true : Frequency [0] = Frequency[0] + 1 ……….. T[0] !=T[9] : 4

Function 2:frequency of each element 3 5 4 1 Frequency 4 T[1] !=T[0] : T[1] =T[1] : Frequency [1] = Frequency[1] + 1 T[1] ! =T[2] : ……….. j from 0 to 9 T[1] =T[j] : if true :Frequency [1] = Frequency[1] + 1 ……….. T[1] =T[9] :Frequency [1] = Frequency[1] + 1 4 3

Function 2:frequency of each element i from 0 to 9 T 3 5 4 1 j from 0 to 9 if T[i] =T[j] Frequency [i] = Frequency[i] + 1 Finding frequency

Function 3:Print elements of the array void printarray (int T[], int X) { for (int i=0; i<X; ++i) cout << T[i] << "\t"; cout << endl; } \t : horizontal tab (move cursor to next tab) Print array T and Frequency array