0% found this document useful (0 votes)
218 views

Lab Activity 1

The document describes a lab activity to introduce fundamentals of C++ programming. The activity has three parts where students will write and compile simple C++ programs to get familiar with the development environment, practice debugging programs, and detect errors in code samples. The learning outcomes are to develop C++ programs in an IDE and debug programs for syntax, compile-time, runtime and logical errors. The hardware/software required is a C++ compiler like Visual Studio or Turbo C++.

Uploaded by

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

Lab Activity 1

The document describes a lab activity to introduce fundamentals of C++ programming. The activity has three parts where students will write and compile simple C++ programs to get familiar with the development environment, practice debugging programs, and detect errors in code samples. The learning outcomes are to develop C++ programs in an IDE and debug programs for syntax, compile-time, runtime and logical errors. The hardware/software required is a C++ compiler like Visual Studio or Turbo C++.

Uploaded by

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

LAB ACTIVITY 1: INTRODUCTION TO

FUNDAMENTALS OF PROGRAMMING

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 1A, 1B and 1C

By the end of this practical session, you should be able to :

 Develop C++ program using Integrated Development Environment (IDE)


 Debug simple programs to demonstrate syntax/compile time, run time and logical
error

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

SCENARIO:

Welcome to C++ program using Integrated Development Environment (IDE).

You have impressed Miss Suria on your capability to solve the task handed to you in 4A and 4B
in DFC1043. Therefore, Infinity Design Solution Sdn. Bhd, with the supervision of Miss Suria
has given you the task to develop payroll system by using C++ program in Integrated
Development Environment (IDE) to increase employees salary by 13% from gross pay and 5%
from allowance received (Please refer Lab Activity 4B in DFC1043).

INSTRUCTION:

Your task is to convince Miss Suria that you are familiar with Integrated Development
Environment (IDE) by writing and compiling a program using C++ before you proceed to
develop payroll system.
Activity 1A

Activity Outcome: Write and compile a program using C++.


Duration : 30 minutes

Task 1: Follow the procedure below step by step.

PROCEDURE OUTPUT
Program 1:
Hello World
Step 1: Type the programs given below

// Hello world program using C++

#include <iostream>
using namespace std;

// main() is where program execution begins.

int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.

Program 2:
first number
Step 1: Type the programs given below second number
sum of the first and second number
#include<iostream.h>
void main()
{
int a, b, sum;

cin >> a;
cin >> b;

sum = a + b;
cout << sum << endl;
}
Step 2: Compile the program.
Step 3: Write the output.
Program 3:
4
Step 1: Type the programs given below

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
int a, b;
int result;

// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;

// print out the result:


cout << result;

// terminate the program:


return 0;
}
Step 2: Compile the program.
Step 3: Write the output.

Program 4:
Please enter an integer value : 12
Step 1: Type the programs given below The value you entered is 12 and its
// input/output example
double is 24
#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Step 2: Compile and run the program.
Step 3: Write the output.
Program 5 :
//The following program displays string using
This is a string
appropriate header file.

Step 1: Type the programs given below


#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
Step 2: Compile and run the program.
Step 3: Write the output.

Activity 1B
Activity Outcome: Write and compile a program using C++.
Duration : 30 minutes

Task 2 : Please refer Activity 4B.1 until Activity 4B.4 in DFC 1043. Write and compile the
program using C++. Write the output.

Output #include<iostream>
int main()
Enter grosspay : 500
{
Enter allowance : 12000 float grosspay, allowance, a, b, c, d, salary;

Employee's salary is : 565 cout<<"Enter grosspay : " ;


cin>>grosspay;
cout<<"Enter allowance : " ;
cin>>allowance;
a=0.13*grosspay;
c=a+grosspay;
b=0.05*allowance;
salary=c+d;
cout<<"Employee's salary is : " <<salary;
return 0;
}
Activity 1C
Activity Outcome: Detect the errors in following programs. Write the correct answers, compile
and run a program using C++
Duration : 60 minutes

Task 3 : Miss Suria has provided you with extra tasks. Follow the procedure below step by step.

PROCEDURE OUTPUT
Program 1 :
Correct answer:
#include <iostream>
Step 1: Find the errors in following programs.
using namespace std;

#include “iostream” int main()


{
using namespace std; char name[50];

int main
{ cout<<"Please enter your name:";
char name[50]; cin>>name;
cout<<"Your name is:" << name << endl;
cout >> "Please enter your name: ";
cin <<name; }
cout << Your name is: " name << endl; Output: Please enter your name : tera
Your name is : tera
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.

Program 2 :
Correct answer:
//my second program in C++
Step 1: Find the errors in following programs. #include<iostream>
using namespace std;
/ my second program in C++ int main()
{
include <iostream> cout<<"Hello World!";
using namespace std; cout<<"I'm a C++ program";
}
int main () Output: Hello Wolrd! I'm a C++ program
{
cout << Hello World!
cout >> "I'm a C++ program";
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
Program 3 : Correct answer:
#include<iostream>
using namespace std;
Step 1: Find the errors in following programs. int main()
{
cout<<"Size of char : "<<sizeof(char)<<endl;
#include <iostream> cout<<"Size of int : "<<sizeof(int)<<endl;
cout<<"Size of short int : "<<sizeof(short int)<<endl;
using namespace std; cout<<"Size of long int : "<<sizeof(long int)<<endl;
cout<<"Size of float : "<<sizeof(float)<<endl;
main() cout<<"Size of double : "<<sizeof(double)<<endl;
cout<<"Size of wchar_t : "<<sizeof(wchar_t)<<endl;
{ }
cout << "Size of char : " << sizeof(char) << endl; Output:
Size of char : 1
cout << "Size of int : " << sizeof(int) << endl;
Size of int : 4
cout << "Size of short int : " << sizeof(short int) << endl; Size of short int : 2
cout << "Size of long int : " << sizeof(long int) << endl; Size of long int : 4
Size of float : 4
cout << "Size of float : " << sizeof(float) << endl; Size of double : 8
cout << "Size of double : " << sizeof(double) << endl; Size of wchar_t : 2
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.

Correct answer:
Program 4 : #include<iostream>
using namespace std;
Step 1: Find the errors in following programs. #define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
#include <iostream>
int main()
using namespace std;
{

#define LENGTH 10 int area;


#define WIDTH 5
#define NEWLINE '\n' area=LENGTH*WIDTH;
cout<<area;
int main cout<<NEWLINE;
{ return 0;
}
int area Output:
50
area = LENGTH * WIDTH;
cout >> area;
cout >> NEWLINE;
return 0;
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
Program 5 : Correct answer:
#include<iostream>
Step 1: Find the errors in following programs. using namespace std;
int main()
{
#include <iostream>
int a=21;
using namespace std;
int b=10;
int c;
main()
{ if(a==b)
int a = 21 {
int b = 10; cout<<"Line 1-a is equal to b"<<endl;
int c ; }
else
if( a == b ) {
{ cout<<"Line 1-a is not equal to b"<<endl;
cout << "Line 1 - a is equal to b" << endl }
} if(a<b)
else {
{ cout<<"Line 2 -a is less than b"<<endl;
cout << "Line 1 - a is not equal to b" << endl }
} else
if ( a < b ) {
{ cout<<"Line 2-a is not less than b"<<endl;
cout << "Line 2 - a is less than b" << endl }
} if(a>b)
else {
cout<<"Line 3-a is greater than b"<<endl;
{
}
cout << "Line 2 - a is not less than b" << endl
else
}
{
if ( a > b )
cout<<"Line 3-a is not greater than b"<<endl;
{ }
cout << "Line 3 - a is greater than b" << endl }
} Output:
else Line 1 - a is not equal to b
{ Line 2 - a is not less than b
cout << "Line 3 - a is not greater than b" << endl Line 3 - a is greater than b
}

Step 2: Write the correct answers


Step 3: Compile and run the program.
Step 4: Write the output.
Program 6 : Correct answer:
#include<iostream>
Step 1: Find the errors in following programs. using namespace std;
int main()

#include <iostream> {

using namespace std; int a=21;


int b=10;

main() int c;

{
a = 21; c=a+b;

b = 10; cout<<"Line 1 - Value of c is : " <<c<<endl;

c; c=a-b;
cout<<"Line 2 - Value of c is : " <<c<<endl;
c=a*b;
c = a + b;
cout<<"Line 3 - Value of c is : " <<c<<endl;
cout >> "Line 1 - Value of c is :" << c << endl ;
c=a/b;
c = a - b;
cout<<"Line 4 - Value of c is : " <<c<<endl;
cout >> "Line 2 - Value of c is :" << c << endl ;
c=a%b;
c = a * b;
cout<<"Line 5 - Value of c is : " <<c<<endl;
cout >>"Line 3 - Value of c is :" << c << endl ;
c=a++;
c = a / b;
cout<<"Line 6 - Value of c is : " <<c<<endl;
cout >> "Line 4 - Value of c is :" << c << endl ;
c=a--;
c = a % b;
cout<<"Line 7 - Value of c is : " <<c<<endl;
cout >> "Line 5 - Value of c is :" << c << endl ;
return 0;
c = a++;
}
cout >> "Line 6 - Value of c is :" << c << endl ;
Output :
c = a--;
Line 1 - value of c is : 31
cout >> "Line 7 - Value of c is :" << c << endl ;
Line 2 - value of c is : 11
return 0;
Line 3 - value of c is : 210
} Line 4 - value of c is : 2
Step 2: Write the correct answers Line 5 - value of c is : 1

Step 3: Compile and run the program. Line 6 - value of c is : 21

Step 4: Write the output. Line 7 - value of c is : 22

You might also like