Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Programming1 Lab#2

Uploaded by

ntaha4787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming1 Lab#2

Uploaded by

ntaha4787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Faculty of Computer Science and IT

Department of Computer Science


Sana'a - Yemen

Lab Manual # 2
Prepared by Me Enas Aldahbali

Student Name

Department

Roll #

Section

Lab Date -09-2024


Submission
Date
Obtained
Lab Grade: 10
Grade

Instructor's Signature:
Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

A. Title: Introduction C++ Programming


B. Objectives of this lab:
 Learn about problem solving and programming
 To be familiar with syntax and structure of C++ program (A simple C++
program)

Activity 1-1
Write (or draw a diagram) to show the steps that your algorithm will take to solve the
following problem.

Suppose you are helping the university registrar office with the registration process.
You are to send students to six different halls depending on the first letter of their
last names and the balance that has appeared on their bills.
Here are the criteria you will use to separate them:
Students with balance zero, Letters:
A-E in Hall 3, F-J in Hall 2, L-O in Hall 8, P-R in Hall 10, and S-Z in Hall 12.
Students with a non-zero balance go to Hall 18. These students can go back to
register once they have a zero balance on their bills.

Solution:

Step1:

Step 2:

1 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Step 3:

2 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Activity 1-2 - Program Design Process

Problem solving is usually broken into two major phases:


1) Problem solving phase, and 2) Implementation phase.
Phase (1) - In the first phase, you will take three steps:
Step I: You will define the problem that you want to solve, clearly.
Step II: You will design an algorithm that is precise and very well thought to solve the
problem, and
Step III: You will test your algorithm on paper. Your algorithm should work correctly,
before you can write a program for it.

Phase (2) -In this phase, you will take two steps:
Step I: Translate your algorithm to C++ language. If you have a correct and precise
algorithm, the translation should be almost line-by-line. This translation must be
correct and free of:
A. Syntax errors, which are the errors resulted from incorrect use of the
programming language syntax or violation of syntax rules.
B. Computations that are not possible, such as dividing by 0, and
C. Errors made by the programmer. Such errors are those made by using wrong
signs or arithmetic operators.

Step II: Test the program to make sure it produces the correct results. Make sure your
test cases are different. The only way to correctly test a program is to have many
different test cases.

3 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Activity 1-3 - A Simple C++ Program


Write a program to display “Hello World” in C++.

1. Problem Analysis (optional)


First program in C++ is to prints “Hello World!” message on the screen of the
computer. During the processing phase, we don’t need any parameter (variables)
for this problem.
2. Algorithm (optional)
1. Start
2. Display the message " Hello World!"
3. End
3. Flowchart (optional)
Start

Display Message Hello World

End
4. Coding
/*
* Multiple line
* comment
*/
#include<iostream>

//Single line comment


using namespace std;

//This is where the execution of program begins


int main()
{
// displays Hello World! on screen
cout<<"Hello World!";

return 0;
}

A. Comments – You can see two types of comments in the above program
// This is a single line comment
/* This is a multiple line comment
suitable for long comments
*/

4 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Comments as the names suggests are just a text written by programmer during code
development. Comment doesn’t affect your program logic in any way, you can write
whatever you want in comments but it should be related to the code and have some
meaning so that when someone else look into your code, the person should understand
what you did in the code by just reading your comment.
Now if someone reads my comment, he or she can understand what I did there just by
reading my comment. This improves readability of your code and when you are
working on a project with your team mates, this becomes essential aspect.
B. #include<iostream> – This statement tells the compiler to include iostream file.
This file contains pre-defined input/output functions that we can use in our program.
C. using namespace std; – A namespace is like a region, where we have functions,
variables, etc. and their scope is limited to that particular region. Here std is a
namespace name, this tells the compiler to look into that particular region for all the
variables, functions, etc. I will not discuss this in detail here as it may confuse you. I
have covered this topic in a separate tutorial with examples. Just follow the tutorial in
the given sequence and you would be fine.
D. int main() – As the name suggests this is the main function of our program and the
execution of program begins with this function, the int here is the return type which
indicates to the compiler that this function will return a integer value. That is the main
reason we have a return 0 statement at the end of main function.
E. cout << “Hello World!”; – The cout object belongs to the iostream file and the
purpose of this object is to display the content between double quotes as it is on the
screen. This object can also display the value of variables on screen (don’t worry, we
will see that in the coming tutorials).
F. return 0; – This statement returns value 0 from the main() function which indicates
that the execution of main function is successful. The value 1 represents failed
execution.

5. Output (compilation, debugging & testing)


Hello World!

5 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Exercise 1.1
Write a program to display “Your Bio data” in C++.
First Name, Last Name, Age, City, Major

1. Problem Analysis (optional)

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

2. Algorithm (optional)

Write Your Answer Here

_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

6 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

3. Flowchart (optional)
Draw Your Answer Here

4. Coding (Use comments wherever applicable)


5. Output (compilation, debugging & testing)

Assignment #1:
Write a C++ program to produce an output that looks like the following.

My name is YourFirstName YourLastName


Write Your code Here

1
2
3
4
5
6
7
8

Type result displayed on your screen

7 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Hello
Hello Hello
Hello Hello Hello
Hello Hello
Hello

Bye

1. Coding (Use comments wherever applicable)

Write Your code Here

10

11

12

13

14

15

16

17

8 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Assignment #2:
Run following program and then identify all syntax errors, pay attention to
type of error you will get and the line number in which the error has
occurred. Fix them one-by-one and rewrite the correct statement as explained
in Table Below, compile it until there is no more error in the
program. Rewrite and run the program and make sure it produces an output.
1 include<iostream>
2 int main()
3 }
4 cout<"This is the second Assignment. \n";
5 cout<<"There were some syntax errors in it that I fix them. \n';
6 cout>>" "Syntax errors are due to the violation of the grammar of C++ \n"

7 return0;

8 }

Answer

# Line Invalid Statement (Error) Correct Statement


#
1 1 include<iostream> #include<iostream>

Missing #
2

9 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali


Date /09/2024 Lab Manual # 2: Introduction to C++ Programming

Write the correct code Here

10 Lab Manual for Programming1 using C++ By Eng. Enass Aldahbali

You might also like