+ All Categories
Home > Documents > Introduction to Algorithms and Programming (COMP151L) · Introduction to Algorithms and Programming...

Introduction to Algorithms and Programming (COMP151L) · Introduction to Algorithms and Programming...

Date post: 13-Apr-2018
Category:
Upload: lytu
View: 216 times
Download: 1 times
Share this document with a friend
37
1 Introduction to Algorithms and Programming (COMP151L) A Student's Manual for Computer Lab Works Dr. Mohamed Aissa [email protected] Room 11 i 13 Summer 2015
Transcript

1

Introduction to Algorithms and

Programming (COMP151L)

A Student's Manual for Computer Lab Works

Dr. Mohamed Aissa [email protected]

Room 11 i 13

Summer 2015

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work #2

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 2 Assignment Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

3

Lab Work #2 Adding Integers

Related Chapter Chapter Title

2 Introduction to C++ Programming

STUDENT LEARNING OUTCOMES:

Upon completion of this computer lab, a successful student will be able to:

Draw a flowchart to find the sum of two numbers,

Understand the assign operator,

Write a program in C++ to find the sum of two integers based on the previous flowchart

Understand the compilation errors and correct them.

1. Write an algorithm to find out the sum of 2 integer numbers:

Lab Questions

stionsExercise

Lab Work # 2 Assignment Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

4

Flowchart

Lab Work # 2 Assignment Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

5

Exercise 1

The following program contains some errors. Fix these errors and obtain an executable

program.

\\ Program to calculate the sum of two integers

# include <iostream>

using namespace

int main

int a,b

{

cout >> " Input a ">>end

cin<<a;

cout << " Input b "<<endl;

cin<<b;

s=a+b

cout << " The sum of a and b = " s<<endl;

return (0)

}

Lab Work # 2 Assignment Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

6

Exercise 2

a) Change the type of the two numbers to float and then make the suitable changes if it is

necessary.

b) Initialize a and b with a number different at zero.

c) Re-execute your program.

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work #3

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 3 If Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

8

Lab Work #3 Conditional Structures

Related Chapter Chapter Title

4 Control Structures

STUDENT LEARNING OUTCOMES:

Upon completion of this computer lab, a successful student will be able to:

draw a flowchart to calculate the value of a given function containing conditions,

translate the flowchart into a C++ program,

understand compilation errors and correct them.

1. Suppose the following function:

( ) {

1. Draw the flowchart to calculate ( ) 2. Based on the flowchart, write the C++ program.

Questions

stionsExercise

Lab Work # 3 If Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

9

#include <iostream>

using namespace std;

int main( )

{

int x;

int f;

cout <<"Input x "<<endl;

cin>>x;

f=0;

if (x>=0)

f= x;

else

f= -x;

cout << " x = "<<x<<endl;

cout << " f="<<f<<endl;

return (0);

}

𝑥 𝑓

𝑓

START

yes

𝑓 𝑥

END

𝑥 𝑓

𝑥

𝑓 𝑥

no

Lab Work # 3 If Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

10

2. Suppose the following function:

( ) {

a. Draw the flowchart to calculate ( ) b. Based on the flowchart, write the C++ program.

#include <iostream>

using namespace std;

int main()

{

int f;

int x;

cout <<"Input x "<<endl;

cin>>x;

f=0;

if (x%2==0)

f=4*x;

else

f=3*x;

cout << " x = "<<x<<endl;

cout << " f="<<f<<endl;

return (0);

}

𝑥 𝑓

𝑓

START

yes

𝑓 𝑥

END

𝑥 𝑓

𝑥%2

𝑓 𝑥

no

Lab Work # 3 If Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

11

3. Suppose the following function:

( ) {

c. Complete the following flowchart to calculate ( ) d. Based on the flowchart, write the C++ program.

#include <iostream>

using namespace std;

int main()

{

int f;

int x;

cout <<"Input x "<<endl;

cin>>x;

f=0;

if ( )

f= ;

else

if( )

f= ;

else

f= ;

cout << " x = "<<x<<endl;

cout << " f = "<<f<<endl;

return (0);

}

𝑥 𝑓

𝑓

START

yes

END

no

yes

no

𝑥 𝑓

Lab Work # 3 If Statements

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

12

4. Suppose the following function:

( ) {

e. Complete the following flowchart to calculate ( ) f. Based on the flowchart, write the C++ program.

#include <iostream>

using namespace std;

int main()

{

int f;

int x;

cout <<"Input x "<<endl;

cin>>x;

f=0;

if (x<=1)

f=1;

else

if( )

f= ;

else

if( )

f= ;

else

f= ;

cout << " x = "<<x<<endl;

cout << " f="<<f<<endl;

return (0);

}

𝑥 𝑓

𝑓

START

yes

END

no

𝑥 𝑓

no

no

yes

yes

Dr. Mohamed Aissa Lab Work # 4 COMP151L Introduction to Algorithms and Programming

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work #4

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 4 Looping Structures

Dr. Mohamed Aissa Lab Work # 4 COMP151L Introduction to Algorithms and Programming

14

Lab Work #4 Looping Structures

Related Chapter Chapter Title

4 Control Structures

STUDENT LEARNING OUTCOMES:

Upon completion of this lab work, a successful student will be able to:

draw a flowchart containing a loop,

understand the different looping structures in C++,

translate the flowchart into a C++ program and write three C++ programs using the three

loop structures,

understand compilation errors and correct them.

1. Draw a flowchart to calculate the following integer summation:

( )

2. Based on this flowchart, write the following C++ program using for loop and test it.

3. Modify the previous program using while, and do while loops. Test both programs.

*** Good Luck ***

Questions

stionsExercise

Lab Work # 4 Looping Structures

Dr. Mohamed Aissa Lab Work # 4 COMP151L Introduction to Algorithms and Programming

15

// Summation of integer numbers included between 1 and n

using for loop

#include <iostream>

using namespace std;

int main()

{

int sum= 0;

int number=1, n;

cout << " Input a positive integer number : "<<endl;

cin>>n;

for(number=1;number<= n;number++)

sum += number;

cout << "\n Sum of numbers includeded between 1 and

" <<n<<" = "<<sum<<endl<<endl;

return (0); // indicate program ended successfully

}

𝑛

𝑠

START

yes

END

𝑛 𝑠

𝑖 𝑛

no

𝑖

𝑠 𝑠 𝑖

𝑖 𝑖

Lab Work # 4 Looping Structures

Dr. Mohamed Aissa Lab Work # 4 COMP151L Introduction to Algorithms and Programming

16

// Summation of integer numbers included between 1

and n using while loop

// Summation of integer numbers included between 1

and n using do while Loop

Lab Work # 4 Looping Structures

Dr. Mohamed Aissa Lab Work # 4 COMP151L Introduction to Algorithms and Programming

17

Solution

// Summation of integer numbers included between 1

and n using while loop

// Summation of integer numbers included between 1

and n using do while Loop

#include <iostream>

using namespace std;

int main()

{

int sum= 0;

int number=1, n;

cout << " Input a positive integer number :

"<<endl;

cin>>n;

while ( number <= n)

{

sum += number;

number++;

}

cout << "\n Sum of numbers includeded

between 1 and " <<n<<" = "<<sum<<endl<<endl;

return (0);

}

#include <iostream>

using namespace std;

int main()

{

int sum= 0;

int number=1, n;

cout << " Input a positive integer number : " <<endl;

cin>>n;

do

{

sum += number;

number++;

}

while(number<=n);

cout << "\n Sum of numbers includeded between 1

and " <<n<<" = "<<sum<<endl<<endl;

return (0);

}

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work #5

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 5 One-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

19

Lab Work #5 One-Dimensional Arrays

Related Chapter Chapter Title

5 Arrays

STUDENT LEARNING OUTCOMES:

Upon completion of this lab work, a successful student will be able to:

write a program to input the elements of one-dimensional array,

write a program to output the elements of one-dimensional array,

draw a flowchart to find the sum of the elements of one-dimensional array,

translate the flowchart into a C++ program,

understand program errors and correct them.

1. Write the following program to input the elements of one-dimensional array:

// Input the elements of one-dimensional array

#include <iostream>

using namespace std;

main()

{

int a[5];

int i;

// Input Array Elements

cout << "\n Iutput Data "<< "\n";

cout << endl;

for ( i=0; i<5; i++)

{

cout << "a[" << i <<"]: ";

cin >> a[i];

}

return( 0);

}

Questions

stionsExercise

Lab Work # 5 One-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

20

2. Add to the previous program one loop to output the elements of one-dimensional

array:

// Input and output the elements of one-dimensional array

#include <iostream>

using namespace std;

main()

{

int a[5];

int i;

// Input Array Elements

cout << "\n Iutput Data "<< "\n";

cout << endl;

for ( i=0; i<5; i++)

{

cout << "a[" << i <<"]: ";

cin >> a[i];

}

// Output Array Elements

return( 0);

}

Lab Work # 5 One-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

21

3. Draw a flowchart to find the sum of the elements of one-dimensional array.

Lab Work # 5 One-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

22

4. Based on the previous flowchart, write a program to find the sum of the elements of

one dimensional array.

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work

#6

Two-Dimensional Arrays

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

24

Lab Work #6 Two-Dimensional Arrays

Related Chapter Chapter Title

5 Arrays

STUDENT LEARNING OUTCOMES:

Upon completion of this lab work, a successful student will be able to:

write a program to input the elements of two-dimensional array,

write a program to output the elements of two-dimensional array,

draw flowcharts to process the elements of two-dimensional array,

translate the flowcharts into C++ programs,

understand program errors and correct them.

5. Write the following program (Program1) to input the elements of two-dimensional

array:

// Input the elements of two-dimensional array

#include <iostream>

using namespace std;

main()

{

int a[3][2];

int i,j;

// Input Array Elements

cout << "\n Iutput Data "<< "\n";

cout << endl;

for ( i=0; i<3; i++)

for ( j=0; j<2;j++)

{

cout << "a[" << i <<","<< j <<"]: ";

cin >> a[i][j];

}

return(0);

}

Questions

stionsExercise

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

25

6. Add to the previous program (Program1) one loop to output the elements of two-

dimensional array:

// Input and output the elements of two-dimensional array

#include <iostream>

using namespace std;

main()

{

int a[3][2];

int i,j;

// Input Array Elements

cout << "\n Iutput Data "<< "\n";

cout << endl;

for ( i=0; i<3; i++)

for ( j=0; j<2;j++)

{

cout << "a[" << i <<","<< j <<"]: ";

cin >> a[i][j];

}

return(0);

}

// Output Array Elements

return( 0);

} (Program2)

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

26

7. Draw a flowchart to find the sum of the elements of two-dimensional array A(m,n).

∑ ( )

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

27

8. Based on the previous flowchart, extend the previous program to find the sum of the

elements of two-dimensional array.

(Program3)

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

28

9. Extend the previous flowchart such if finds the sum of positive and negative elements

of two-dimensional array A(m,n).

∑ ( )

, if ( )

∑ ( )

, if ( )

Lab Work # 6 Two-Dimensional Arrays

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

29

10. Extend the previous program such that it calculates the sum of positive elements and

the sum of negative elements of two-dimensional array.

(Program4)

*** Good Luck ***

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work

#7

Functions

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 7 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

31

Lab Work #7 Functions

Related Chapter Chapter Title

6 Functions

STUDENT LEARNING OUTCOMES:

Upon completion of this lab work, a successful student will be able to:

draw a flowchart to find the factorial of a positive integer,

declare a function,

call a function,

define a function,

write a program to declare, call and define a function to calculate the factorial of a positive

integer

understand program errors and correct them.

1. Draw a flowchart to calculate the factorial of a positive integer:

( )

2. Write a program using a function. The program is to print the factorial for a given integer

number n. You should have a function to calculate the factorial of that introduced number.

The main program coordinates the process of inputting values and calling this function as

necessary.

Questions

stionsExercise

Lab Work # 7 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

32

1. Flowchart

START

yes

no

Fact=1

Fact=1

Fact=Fact*I

I=I+1

N <I

no

END

N, Fact

N=0 yes

N

Lab Work # 7 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

33

2. Program

// Function prototypes

#include <iostream>

using namespace std;

double factorial(int);

int main( )

{

int n;

double result;

cout << "Enter a value for n: ";

cin >> n;

result = factorial(n);

cout << "\n The factorial of n is " << result<< endl;

return(0);

}

double factorial(int m)

{

int i;

double fact;

fact=1;

if (m==0)

{

fact=1;

return(fact);

}

for (i=1;i<=m;i++)

fact=fact*i;

return(fact);

}

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

Introduction to Algorithms and

Programming / Lab

(COMP151L)

Lab Work

#8

Functions

Student Name ID

Dr. Mohamed Aissa

[email protected]

Office # 11 i 13

Summer 2015

Lab Work # 8 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

35

Lab Work #8 Functions

Related Chapter Chapter Title

6 Functions

STUDENT LEARNING OUTCOMES:

Upon completion of this lab work, a successful student will be able to:

declare a function,

call a function,

define a function,

write a program to declare, call and define function.

1. Write a main program calling a function. In the main program, the user should input the

dimension of the rectangle (length and width). The main program should call a function

"rectangle" to calculate the perimeter of the rectangle which should be printed by the

main program.

Questions

stionsExercise

Lab Work # 8 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

36

Program

Lab Work # 8 Functions

Dr. Mohamed Aissa Student's Lab Works COMP151L Summer 2015

37

2. Add a another function to calculate the area of the rectangle. Again, the main program

should call this function and then print the area.


Recommended