Programming1 Lab#2
Programming1 Lab#2
Lab Manual # 2
Prepared by Me Enas Aldahbali
Student Name
Department
Roll #
Section
Instructor's Signature:
Date /09/2024 Lab Manual # 2: Introduction to C++ Programming
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:
Step 3:
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.
End
4. Coding
/*
* Multiple line
* comment
*/
#include<iostream>
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
*/
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.
Exercise 1.1
Write a program to display “Your Bio data” in C++.
First Name, Last Name, Age, City, Major
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
2. Algorithm (optional)
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
3. Flowchart (optional)
Draw Your Answer Here
Assignment #1:
Write a C++ program to produce an output that looks like the following.
1
2
3
4
5
6
7
8
Hello
Hello Hello
Hello Hello Hello
Hello Hello
Hello
Bye
10
11
12
13
14
15
16
17
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
Missing #
2