Lab 5.

Slides:



Advertisements
Similar presentations
Appendix Lab Manual for Programming Skills 1. Symbols used in Writing Programs { opening curly bracket } closing curly bracket # hash sign or number sign.
Advertisements

Chapter 4 Summation.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Exercise 5.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Lecture 8 Sept 25, Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,
Programming is instructing a computer to perform a task for you with the help of a programming language.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
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.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 4 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.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Example 21 #include<iostream.h> int main() { char Letter = 0;
MT262A Review.
while Repetition Structure
Loops OR Iterations (For - While – Do_While)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Two Dimensional Array Mr. Jacobs.
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 >
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
Multi-dimensional Array
Chapter 2.2 Control Structures (Iteration)
C++ Arrays.
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
While Loops.
One-Dimensional Array Introduction Lesson xx
Arrays Skill Area 315 Part A
Introduction to Programming
Data type List Definition:
CS150 Introduction to Computer Science 1
Counting Loops.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Lecture 12 Oct 16, 02.
CS150 Introduction to Computer Science 1
Chapter 2.2 Control Structures (Iteration)
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS1201: Programming Language 2
Arrays I Handling lists of data.
INC 161 , CPE 100 Computer Programming
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Fundamental Programming
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.
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Functions Divide and Conquer
Arrays Imran Rashid CTO at ManiWeber Technologies.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Presentation transcript:

Lab 5

Ex1 Write a C++ program that calculate the factorial of a number n that the user will enter. Where the factorial(n!) for nonnegative integer(n) = n * (n-1) * (n-2) * …. * 2 * 1

Ex.1 #include<iostream.h> int main() { int x,fact=1; cout<<"Please enter number : "; cin>> x; for(int i=x; i>0;i--) fact=fact*i; cout<<" The factorial of " << x << " = " <<fact<<endl; system("pause"); return 0; }

Ex.2 An integer number is said to be a perfect number if its factors, including 1 (but not the number itself),sum to the number. For example: ”6” is a perfect number because 6= 1+2+3. Write a program that determines if parameter number is a perfect number.

Ex.2 Perfect number: (هو عدد موجب يساوى مجموع الارقام التى تقبل القسمة عليه بما فيها الواحد مثل: 6=1+2+3 فرقم 6 يقبل القسمة على 1 وعلى 2 وعلى 3 فهذه الارقام تسمى factors اى معاملاته التى يقبل القسمة عليها وبالمثل: 28=1+2+4+7+14 فى هذه المسألة نريد التأكد من شيئين : الاول ان نقسم العدد مثلا 6 على كل الارقام من 1 وحتى 6 والذى نجد باقى القسمة بتاعه صفر "يعنى ان ال 6 يقبل القسمة عليه " ناخده ونضعه فى sum وكذلك نظل نجمع على ال sum كل الاعداد التى تقبل القسمة على رقمنا الذى نختبره . ثانى شئ : نشوف هل sum تساوى الرقم الذى نختبره ام لا.

Ex.2 # include <iostream.h> int main() { int num, sum=0; cout<<"Enter the number that you will check : "; cin>>num; cout<<endl; for(int i=1 ; i<num ; i++) if(num%i==0) sum+=i; } if(sum==num) cout<<num<<" is aperfect number\n"; else cout<<num<<" isn't aperfect number\n"; return 0;

Ex.3 An integer is said to be prime if it is divisible(يقبل القسمة) only by 1 and itself. For example 2,3,5,7 and 11 are prime, but 4,6,8,9 are not prime. Write an algorithm and a program that determines if a number is prime.

Ex.3 Answer: In this example you should draw the flowchart (an algorithm) in the first and then write the program. The program: اولا : اى رقم يقبل القسمة على الواحد وكذلك على نفسه ففى هذه المسألة لازم نختبر الرقم لو يقبل القسمة على اى رقم اخر غير الواحد او نفسه نستبعده (يعنى لو باقى القسمة كان بيساوى صفر دة معناه ان الرقم مش (prime مثلا: 4 طبعا كباقى الارقام تقبل القسمة على نفسها وعلى الواحد ولكنها ايضا تقبل القسمة على 2 يعنى عندما نختبر باقى قسمة 4 على 2 سينتج صفر لذلك رقم 4 مش هيكون prime ملحوظة: الواحد مش prime

Ex.3 # include <iostream.h> int main() { cout<<"Enter the number that you will check : "; int num ; int count=0; //عداد نقدر من خلاله نعرف هل الرقم الذى نختبره يقبل القسمة على اى عدد غير الواحد ونفسه ام لا cin>>num; cout<<endl; for(int i=2 ; i<num ; i++) //بدأنا ب 2 لان اى رقم بيقبل القسمة على واحد if(num%i==0) //لو الشرط تحقق count++; //هيزيد واحد ودة معناه ان الرقم الذى بنختبره بيقبل القسمة على عدد تانى غير"الواحد ونفسه" لذلك نستبعده counter } if(num==1 || num==0) cout<<" This number can't describe as prime\n"; else if(count>0) //يعنى العداد زاد عندما العدد قبل القسمة على رقم غير نفسه والواحد cout<<num<<" is not prime number\n"; else cout<<num<<" is a prime number\n"; return 0;

Arrays

Notes When we need to group or list many data items of the same type into one group we use the “Array”. An array can have different values since each item has different location in the array but the array can’t store many different types of values , (such int, char and float) How to declare the array? int num[4]; where “int”  type of array “num”  array name “ [4] ”  array size int num[ 4 ] = { 10 , 23 , 12 , 17 }; int num[ ] = { 10 , 23 , 12 , 17 }; // we don’t need to declare the size

Notes The items in an array are called elements. The index of the first elements is 0 while the index of the last item is size-1 Ex. The first element  age[0] The last element  age[3] int arr[5]={1,4,6}; That mean: arr[0] =1 arr[1] =4 arr[2]=6 arr[3]=0 arr[4]=0 The two dimensional array is array of arrays.  arr[x][y] arr[3][3]  #include<iostream.h> int main() { int arr[5]={1,4,6}; for(int i=0 ;i<5;i++) cout << "arr[" << i << "] = " << arr[i] << endl; return 0; } y x

Ex.1 Write a C++ program that accepts 7 integers from the user, stores them in an array.

Ex.1 Answer #include<iostream.h> int main() { int arr[7] , sum=0 ; for(int i=0;i<7;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; } return 0;

Ex.2 Write a C++ program that accepts 7 integers from the user, stores them in an array, and then calculates the summation of them.

Ex.2 Answer #include<iostream.h> int main() { int arr[7] , sum=0 ; for(int i=0;i<7;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; sum+=arr[i]; } cout<<"The summation = "<<sum<<endl; return 0;

Ex.3 Write a C++ program that accepts 10 integers from the user, stores them in an array, and then calculates the summation and the average of the even numbers.

Ex.3 answer #include<iostream.h> int main() { int arr[10] , count=0; float sum=0 ; float avrg=0; for(int i=0;i<10;i++) cout<<"enter number: " ; cin>>arr[i]; if(arr[i]%2==0) sum+=arr[i]; count++; } avrg=sum/count; cout<<"the summation is: " << sum <<endl; cout<<"The average is: " << avrg << endl; return 0;

Ex.4 Write C++ program that accepts 3 numbers then prints them in a reverse order.

Ex.4 Answer #include<iostream.h> int main() { int arr[3] ; for(int i=0;i<3;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; } for(i=2;i>=0;i--) cout<< arr[i] << endl; return 0;

Ex.5 Write a program that accepts 10 integers from the user, stores them in an array, and then calculates and prints the max and min number.

Ex.5 #include<iostream.h> int main() { int arr[10] , max , min ; for(int i=0;i<10;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter a number: "<<endl; cin>>arr[i]; } max = arr[0]; min= arr[0]; for(i=0;i<10;i++) if(arr[i] > max) max=arr[i]; else if(arr[i]<min) min=arr[i]; cout<<"The max number = "<<max<<endl <<"The min number = " << min<< endl; return 0;

Nested for loobs Astrerisks

Write a program that prints the following patterns:

#include<iostream #include<iostream.h> int main() { for(int line =1;line<=5;line++) for(int s=line;s<=4;s++) cout<< " "; } for(int ast=1;ast<=2*line-1;ast++) cout<<"*"; cout<<endl; for(int line =4;line>=1;line--) system("pause"); return 0;