Introduction to Algorithms and Programming

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Exercise 2.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Chapter 4 Summation.
How Create a C++ Program. #include using namespace std; void main() { cout
Exercise 5.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
Programming is instructing a computer to perform a task for you with the help of a programming language.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Perimeter = 2*(length+width)
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Control Structures Selection: else / if and switch Dr. Ahmed Telba.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Introduction to C++. 2 What Is a Program Made Of?
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class.
LESSON 2 Basic of C++.
#define #include<iostream> using namespace std; #define GO
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Repetition Structures (Loops)
Chapter 4 Loops Case Studies
Addis Ababa Institute of Technology Yared Semu May 2012
Programming fundamentals 2 Chapter 2:Function
Pointer Data Type and Pointer Variables
CSC1201: Programming Language 2
Random Number Generation
פונקציות לעיתים קרובות לא נוח להגדיר את כל התוכנה בתוך גוף אחד.
Introduction to Algorithms and Programming COMP151
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Control Structures Repetition
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Introduction to Algorithms and Programming COMP151
CS1201: Programming Language 2
Passing Arrays to functions
CHAPTER 2 Arrays and Vectors.
Name: Muhammad Hassan VU-ID: BC Version: Office 2007
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CSC1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Control Structures Selection
Using string type variables
Pointers & Functions.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
Introduction to Algorithms and Programming
Introduction to Algorithms and Programming COMP151
Introduction to Algorithms and Programming COMP151
Introduction to Algorithms and Programming COMP151
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.
Dr. Khizar Hayat Associate Prof. of Computer Science
CSE Module 1 A Programming Primer
Odds and Ends.
Dedication of Duty.
Presentation transcript:

Introduction to Algorithms and Programming LAB 1 Introduction to Algorithms and Programming

C++ Program structure // My first program in C++ /*To Explain the C++ program structure This is the Best Example Program*/ #include<iostream> using namespace std; int main( ) { cout<<"This is the First Program \n"; cout<<"University of Nizwa"; return(0); }

Exercise 1: Write a C++ program that read two numbers and find the addition and subtraction of them

#include<iostream> using namespace std; int main() { /*this program read two numbers and find the addition and subtraction of them*/ #include<iostream> using namespace std; int main() { int a,b,c,d; cout<<"Enter the value for a"<<endl; cin>>a; cout<<"Enter the value for b"<<endl; cin>>b; c=a+b; d=a-b; cout<<"Result of addition"<<c<<endl; cout<<"Result of subtraction"<<d<<endl; return(0); }

Exercise 2: Write a C++ program that read a number and find the square of that given number

//This program read a number and find the square of that given number #include<iostream> using namespace std; int main() { int number, result; cout<<"Enter the value for number"<<endl; cin>>number; result =number*number; cout<<"The result is"<< result <<endl; return(0); }

Exercise 3: Write a C++ program that swap between two numbers

//program to swap between two numbers #include<iostream> using namespace std; int main() { int num1,num2,temp; cout<<"Enter the value for num1"<<endl; cin>>num1; cout<<"Enter the value for num2"<<endl; cin>>num2; temp=num1; num1=num2; num2=temp; cout<<"Value of num1 after swapping"<<num1<<endl; cout<<"Value of num2 after swapping"<<num2<<endl; return(0);

Exercise 4: Write and algorithm to compare three numbers and print the smallest number

//compare three numbers and print the smallest number #include <iostream> using namespace std; int main() { float n1, n2, n3; cout<<"Enter the value for n1"<<endl; cin>>n1; cout<<"Enter the value for n2"<<endl; cin>>n2; cout<<"Enter the value for n3"<<endl; cin>>n3; if(n1 <= n2 && n1 <= n3) cout << “Smallest number: " << n1; } if(n2 <= n1 && n2 <= n3) cout << " Smallest number: " << n2; if(n3 <= n1 && n3 <= n2) cout << " Smallest number: " << n3; return 0;