Beruflich Dokumente
Kultur Dokumente
Introduction to Computer
Science & Programming
CSC 101
Lab Manual
NAME: _______________________________
ID: __________________________________________
SECTION: _____________________________________
DEGREE: _____________________________________
IQRA UNIVERSITY,
ISLAMABAD CAMPUS
CSC101 Intro to CS & Progg
Introduction to Computer Science and Programming
(List of Labs)
Lab #1________________________________________________________________________
Introduction to Windows 8
Lab #2________________________________________________________________________
Create a Table of Contents
Lab #3________________________________________________________________________
Introduction to Microsoft PowerPoint
Lab #4________________________________________________________________________
Introduction to Microsoft Excel 2007
Lab #5_______________________________________________________________________
Pivot Tables in Excel 2007-A
Lab #6_______________________________________________________________________
Pivot Tables in Excel 2007-B
Lab #7________________________________________________________________________
Programming, Debugging, Microsoft Visual C++
Lab #8________________________________________________________________________
Variables, Arithmetic Operators
Lab #9________________________________________________________________________
Decision Control Statements
Lab #10_______________________________________________________________________
for loop, while loop
Lab #11_______________________________________________________________________
Do-While loop
Lab #12_______________________________________________________________________
Continue, Switch, Break
CSC101 Intro to CS & Progg
Lab #13_______________________________________________________________________
Functions
Lab #14_______________________________________________________________________
Functions (Call By Reference and By Value)
Lab #15_______________________________________________________________________
Arrays
Lab #16_______________________________________________________________________
Lab Project
CSC101 Intro to CS & Progg
LAB NO. 1:
Introduction to windows 8
Introduction:
An introduction of the whole course and a brief description of windows 8 and its features.
Overview of course
What is OS?
Understanding of Windows 8
Operating system
Program used to control computer, link between user and internal working of computer and
programs.
Windows History:
1995–1998: Windows 95—the PC comes of age (and don't forget the Internet)
1998–2000: Windows 98, Windows 2000, Windows Me—Windows evolves for work and play
Lab 01: Create a Bibliography
CSC101 Intro to CS & Progg
2001–2005: Windows XP—Stable, usable, and fast
Windows 8:
System software gets installed when the operating system is installed on the computer while
application software is installed according to the requirements of the user.
System software includes programs such as compilers, debuggers, drivers, assemblers while
application software includes media players, word processors, and spreadsheet programs.
Generally, users do not interact with system software as it works in the background whereas
users interact with application software while doing different activities.
A computer may not require more than one type of system software while there may be a
number of application software programs installed on the computer at the same time.
System software can run independently of the application software while application software
cannot run without the presence of the system software.
We generally write a computer program using a high-level language. A high-level language is one
which is understandable by us humans. It contains words and phrases from the English (or
other) language. But a computer does not understand high-level language. It only understands
program written in 0's and 1's in binary, called the machine code. A program written in high-
level language is called a source code. We need to convert the source code into machine code
and this is accomplished my compilers and interpreters. Hence, a compiler or an interpreter is a
program that converts program written in high-level language into machine code understood by
the computer.
Interpreter Compiler
LAB No. 2
Create a Table of Contents
Introduction:
You create a table of contents by applying heading styles — for example, Heading 1, Heading
2, and Heading 3 — to the text that you want to include in the table of contents. Microsoft
Word searches for those headings and then inserts the table of contents into your document.
When you create a table of contents this way, you can automatically update it if you make
changes in your document.
Microsoft Word 2010 provides a gallery of automatic table of contents styles. Mark the table of
contents entries, and then click the table of contents style that you want from the gallery of
options.
You can also create a custom table of contents with the options you choose and any custom
styles that you've applied by using the Table of Contents dialog box.
This article explains how to add a table of contents. It does not cover tables of authorities or
tables of figures.
The easiest way to create a table of contents is to use the built-in heading styles. You can also
create a table of contents that is based on the custom styles that you have applied. Or you can
assign the table of contents levels to individual text entries.
For example, if you selected text that you want to style as a main heading, click the style
called Heading 1 in the Quick Style gallery.
If you don't see the style that you want, click the arrow to expand the Quick Style gallery.
If the style that you want does not appear in the Quick Style gallery, press CTRL+SHIFT+S to
open the Apply Styles task pane. Under Style Name, click the style that you want.
NOTE If you want to specify more options — for example, how many heading levels to
show — click Insert Table of Contents to open the Table of Contentsdialog box. To find out
more about the different options, see Format the table of contents.
1. On the References tab, in the Table of Contents group, click Table of Contents, and then
click Insert Table of Contents.
Printed document If you are creating a document that readers will read on a printed page,
create a table of contents in which each entry lists both the heading and the page number
where the heading appears. Readers can turn to the page that they want.
Online document For a document that readers will read online in Word, you can format
the entries in the table of contents as hyperlinks, so that readers can go to a heading by
clicking its entry in the table of contents.
LAB NO. 3
Introduction to Microsoft PowerPoint
Introduction:
Microsoft PowerPoint, part of Microsoft Office, creates and plays presentations. A presentation
is something a speaker makes to an audience, typically using a computer and LCD projector to
display material in a lecture hall or auditorium. PowerPoint works a lot like Microsoft Word, and
the assumption here is that you are familiar with Word.A PowerPoint presentation is made up of
"slides" that are individual frames or screens of information. To create a presentation, create the
slides. A PowerPoint file (*.ppt) is a collection of slides, typically for one and only one
presentation, although files can be linked together to make up compound presentations.
Double click quickly on the PowerPoint 2007 icon on the Windows desktop
(see image on right), or click the Start button in the lower left corner of the
screen, and then click All Programs, next move your cursor over Microsoft
Office, then click Microsoft PowerPoint 2007.
In this tutorial, whenever we indicate that you need to click a mouse button, it will mean to
click the left mouse button – unless we indicate that you should click the right mouse button.
So, always move the cursor over the “place” we indicate and “click left” unless we tell you
otherwise.
If you have been using PowerPoint 97, 2000, XP/2002 or 2007 this will be a wonderful
journey. You will find many exciting new features and enhancements. Almost everything
is “graphical.” When PowerPoint opens, the appearance of the screen will be very
In the image below you’ll immediately see that the Menu Bar has been replaced by Tabs and
Ribbons. The Tabs and Ribbons are then divided into Groups. We’ll be working with these
new features in detail as we move through the tutorial.
Notice, in the image on the last page, that the screen is “sort of” divided into three sections.
Across the top are the Microsoft Office Button, the Quick Access Toolbar and the Tabs,
Ribbons and Groups (indicated on the last page).
Slide 1
In PowerPoint 2007 a Slide Layout named Title Slide always appears first. PowerPoint
“thinks” that you want to start your presentation with a title. So, logically, the Title Slide
appears in the main section of the screen.
After you understand PowerPoint a bit more, you can choose any of the layouts you desire. We’ll
show you how to do this as we proceed through the tutorial.
You will notice, in the lower left corner of the screen, Slide 1 of 1 is
indicated.
You will also see that your screen looks like the image below.
Now we’ll have some fun and create a PowerPoint 2007 presentation on how to make a
Peanut Butter and Jelly sandwich.
Place your cursor in the “Click to add title” box and Click the left mouse button. Your text
box, after you click, will look similar to the one below.
To insert the text in this formatted text box, we simply enter (type-in) the title: How to Make
a Great PBJ – go ahead and type this text in the box.
When you move your cursor arrow over the button you will see a Microsoft Help Text box
appear that says New Slide.
LAB NO. 4
Introduction to Microsoft Excel 2007
Objectives of this Lab:
IF Statement
Starts excel:
1. Open the task pane: Menubar > View > Taskpane
2. Click Task pane drop‐down arrow; view task pane windows
Workbook: A collection of related worksheets contained within a single file.
3. Open the file: first‐gradebook.xls
4. Save the file as first‐gradebookSolution.xls.
1. Click in cell B3, the cell containing Ackerman’s grade on the first test. Cell
B3 is now the active cell (surrounded by a heavy border). You can see the cell
name the three in the name box and the formula is displayed in the formula
box.
2. Click in cell B4 (or press the down arrow) to make it the active cell; check
the name box and the formula box to see the contents of B4.
3. Click cell E3, Ackerman’s semester average. Active cell displays the computed
grade, but formula bar displays the formula.
4. Click the Anthro264 tab to view a different worksheet within the same
workbook
5. Click the Anthro 341 have to view this worksheet; Click the Anthro 128 tab
return to the original worksheet.
1. Click in cell F4. You will notice a small black square in the lower right corner
of the selected square.
2. If you put your cursor over it, your cursor becomes a crosshair. While your
cursor is a crosshair, click and drag from cell F4 down to cell F25.
3. All of the student’s test totals should fill in automatically.
4. Click in cell F13. You will see the formula: =SUM(B13:E13)
5. Click in cell F20. You will see the formula: =SUM(B20:E20)
6. These formulas are all based on our original formula in cell F4. As we dragged
the formula down, the cell references also moved down.
Calculate the test average using autoSum with the Average function:
1. Click in cell G3 (test average), then click
the down arrow next to the autoSum
button.
2. Select Average. Excel will automatically select the cell range B3:F3
3. We do not want cell E3 selected. E3 is the test total. Click and drag to adjust
the cell range to B3:E3
4. Press the Enter key. You will get a test average of 82.75.
Completing the Calculations Using the AutoFill option:
1. Click in cell G4. You will notice a small black square in the lower right corner
of the selected square. If you put your cursor over it, your cursor becomes a
crosshair. While your cursor is a crosshair, click and drag from cell G4 down to
cell G25.
2. All of the student’s test averages will fill in automatically.
3. Notice that they appear as a number with decimal places. To remove the
decimal places, click the Decrease Decimals button (in the Numbers section
on the Home tab of the ribbon) twice.
Repeat the same Average autoSum calculations on row 27, Class Average:
1. Click in cell B27. Go to the autoSum button drop‐down arrow and select the
Average function
2. The formula should read: =Average(B3:B26)
Lab 04: Introduction to Microsoft Excel 2007
CSC101 Intro to CS & Progg
4. Move your mouse pointer onto the column's edge; the pointer becomes a double
headed arrow.
5. Double click and the column will automatically resize to the correct width.
12. Click OK. You should get the result that Ackerman passed
13. Use the AutoFill function to drag the formula down through cell G25. Only two people Failed the
course.
1. Click Key F1. This will open the “help window” (which strongly resembles the help task pane).
Lab: 04: Microsoft Excel Basics
CSC101 Intro to CS & Progg
LAB NO. 5
Pivot Tables in Excel 2007-A
Introduction:
A Pivot Table is way to present information in a report format. The idea is that you can click
drop down lists and change the data that is being displayed. For example, choose just one student
from a drop down list and view only his or her scores. Pivot tables are a lot easier to grasp when
you see them in action. Here's the one we're going to create in this section:
Look at Row 4. This shows that the student is Elisa. If we click Elisa's drop down arrow, we'll
see this:
The Subject and Month cells also have drop down lists. So we could view only January's scores,
and just for Art and English, for example.
So this is a Pivot Table - a report that we can manipulate by selecting items from drop down lists.
Let's make a start.
The first thing you need for a Pivot Table is some data to go in it.
Highlight the data that will be going in to your Pivot Table. On the Excel 2007 menu bar,
click Insert. From the Insert menu, locate the Tables Panel:
On the Tables panel click Pivot Tables. The Create Pivot Tables dialogue box appears:
In the dialogue box above, the data that we highlighted is in the Table/Range textbox. You can
select different cells by clicking the icon to the right of the Table/Range textbox. You can also
specify an external data source, such as a text file, for the data in your Pivot Table.
We've selected a New Worksheet as the place where the Pivot Table will be placed. Click OK.
It helps to have a look again at what we're trying to create. Here's the completed Pivot Table
again:
Now take a look at the Pivot Table Field List image again, the one above the completed pivot
table. It has tick boxes for Month, Subject, Student, and Score. These are column headings from
the original spreadsheet data. We've put the Month in cell A7 on our Pivot Table, Subject is in
cell B6, Student is in cell B4, and Score is the Average scores in cells B8 to G10. You'll see how
it works, though.
The idea is that you tick a box in the Pivot Table Field List, and then drag it to the four areas
below. Excel 2007 will take care of the rest.
Excel will create a basic (and messy) Pivot Table for you. But we're going to put our 4 fields into
the 4 areas below. Here's the 4 areas we can drag to:
For the Report Filter, we want the name of a Student. For the Column Labels, we want the
Subject, and for the Row Labels, we'll just have the Month. The Values will be the Average
scores.
If you look at the Field areas after you have ticked all four boxes, however, you may see
something like this:
So click on Student in the Row Labels box. Hold down your left mouse button, and then drag it
in to the Report Filter box. If you don't fancy dragging and dropping, simply click the Student
item with your left button. From the menu that appears, select Move to Report Filter:
The Pivot Table on your spreadsheet will look a lot different, too. It should be looking like this:
LAB NO. 6
Pivot Tables in Excel 2007-B
The reason why the scores from our Pivot Table are so strange is because Excel 2007 is using the
wrong formula. It's using a Sum total when we want it to use an Average.
The numbers have all been added up. But we want averages, instead. To change the formula,
click on Sum of Score under the Values field area:
Change the Formula from Sum to Average, and then click OK. Your Average formula won't be
formatted to any decimal places. So highlight you data. On the Home menu in Excel 2007, locate
the Number panel. Format your Averages so that it has no decimal places. Your Pivot Table will
then look like this:
Almost there!
Look at cells A3, B3 and A4 above. These all have the not very descriptive names of Average of
Score, Column Labels, and Row Labels. You can click inside of these cells and type your own
headings, in exactly the same way as you would to enter text in a normal cell.
In the new version of the Pivot Table below, we have renamed these cells. We've also centred the
data.
Only one thing left to do - spruce up the table by adding a bit of colour.
Click anywhere on your Pivot Table to highlight it. Now look at the menu bar at the top of Excel
2007. You'll notice a Design menu. Click on this to see the various design options.
Select Banded Rows and see what happens. Now click Banded Columns.
Next to this panel, there are lots of Pivot Table Styles to chose from. Select one that catches your
eye. Here's our finished Pivot Table again, only with a different Style:
There's a lot more you can do with Pivot Tables, but we hope that this introduction has whetted
your appetite!
LAB No. 7
Programming, Debugging, Microsoft
Visual C++
Introduction:
The aim of this lab is to create a new project in Microsoft Visual C++ and debug the very first
program in this environment.
Making a project
Debugging
How to create a small program in Visual C++ 6.0:
Step 1:
First of all click on Start Menu All Programs Microsoft Visual Studio 6.0 Visual C++
6.0
Step 2:
This would launch the following window. Remember this step is very important you have to be
careful. Visual C++ 6.0 is a very powerful software so you need to be clear on what you want to
do. Click on the Windows Console Application and then click on the project name write
“20090145_Lab3”. Change the roll number according to your own rollnumber. Then click OK.
Step 3:
After clicking the OK in the step 2, you will see the following window. Let the empty project
option selected and click on Finish. After that you will see another window click OK
Step 4:
Now go to File Menu again and click “New…” you will see the following window. Select the
option C/ C++ header file and write the name of the file as 2009145Lab3.c {Change the
registration number according to your registration number}
Remember that your compiler will behave according to the extension of the file so make sure to
name the file as .c file not any other name.
Click on the OK button and you will be given a white open space to write the program.
Step 5:
#include <iostream.h>
int main()
return 0;
Step 6:
Now go to Build Menu and click “Compile” ,and after that again go to Build and click on
“Execute”. You will see “My first program of C is so easy” on a new black screen.
Debugging:
Debugging is the name given to the process of removing bugs (errors) from computer programs.
Step through the program one statement at a time, either "over" or "into" functions.
Run the program up to a certain point (either to the cursor or to a "breakpoint") and then
stop.
Show the values (or contents) of variables at each point during the execution of the
program.
In this lab we will use step over only to execute your code line by line.
#include<iostream.h>
int main()
return 0;
First compile the above program and then Execute it, all the three statements will be printed on
the screen. Now again compile the file and execute one statement at a time by clicking on Debug
→Step Over (or F10). You will see that on the output screen (black screen) the statements will
appear one by one.
LAB No. 8
Variables, Arithmetic Operators
Objectives of this lab:
We are going to start from the previous lab to refresh your mind.
Step 1:
First of all click on Start Menu All Programs Microsoft Visual Studio 6.0 Visual C++ 6.0
This would launch the following window
Step 2:
Go to File Menu and click the following window click New ..
This would launch the following window. Remember this step is very important you have to be
careful. Visual C++ 6.0 is a very powerful software so you need to be clear on what you want to
do. Click on the Windows Console Application and then click on the project name write
“20090145_Lab2”. Change the roll number according to your own rollnumber. Then click OK.
Step 3:
After clicking the OK in the step 2. You will see the following window. Let the empty project
option selected and click on Finish. After that you will see another window click OK
Step 4:
Now go to File Menu again and click “New…” you will see the following window. Select the
option C/ C++ header file and write the name of the file as 2009145Lab3.c {Change the
registration number according to your registration number}
Remember that your compiler will behave according to the extension of the file so make sure to
name the file as .c file not any other name.
Click on the OK button and you will be given a white open space to write the program.
Step 5:
int main()
return 0;
int main()
/*Initializing the
variables
*/
a=10; ch=’b’;
return 0;
In this example you saw various ways of declaring variables of various data types and how to
write comments in C programs. Also, notice the use of “\n” at the end of the cout statements. It
simply adds a carriage return which ends the current line.
Write the following program in the editor and see the effect of the program and compare your
result with the following output.
#include <iostream.h>
int main()
{
int a = 72;
char b = 'A';
cout<<"a equals \n"<<a;
cout<<"a equals \n"<<a;
cout<<"b equals \n"<<b;
cout<<"b equals \n"<<b;
return 0;
}
Output:
a equals 72
a equals H
b equals 65
b equals A
The reason why this works is because a character constant is just an integer from 0 to 255.
int main()
int a,b;
cout<<“Enter value of a: “;
cin>>a;
cout<<“Enter value of b: “;
cin>>b;
cin>>a;
cout<<” “;
cin>>b;
cout<<a<<” “<<b;
return 0;
After writing this program you will see that how one can add two numbers and following the
same way we can use subtraction, multiplication and division operators.
#include <iostream.h>
int main()
int a,b;
int sum;
cin>>a;
cin>>b;
sum=a+b;
cout<<sum<<”\n”;
return 0;
LAB NO. 9
Decision Control Statements
Objectives of this lab:
Comparison/Relational Operators
Logical Operators
If Statement
If - else statement
Else - if Statement
Starting from a very simple form of if statement
Example: Write a program in which it takes a number from keyboard as an input and if the
number is greater than 100 it prints “The number is greater than hundred”.
Code:
#include <iostream.h>
int main()
int number ;
cout<<“Enter an integer\n”;
cin>>number;
if ( number >100 )
return 0;
int main()
int a,b ;
cin>>a;
cin>>b;
if ( a >=b )
Cout<<a<<”\t”<<b<<”\t”<<a-b;
else
cout<<b<<”t”<<a<<”\t”<<b-a;
return 0;
int main()
int number ;
cout<<“Enter an integer\n”;
cin>>number;
if ( number <100 )
if ( number <50)
else
else
Write a program which takes marks as input and shows the out put as follows:
Marks Output
int main()
int marks;
cout<<“Enter an marks\n”;
cin<<marks<<endl;
return 0;
}
87 – 100 Grade A
80 - 87 Grade B+
72 – 80 Grade B
67 – 72 Grade C+
60 - 67 Grade C
below 60 Failed
Take Home:
1. Write a program, which takes age as input from user and prints appropriate message
depending upon the following conditions:
2. Write a program which takes 3 numbers as input e.g. a = 30, b = 54 and c = 6 and print
output as follows:
Sample output:
Min number entered is 6
Max number entered is 54
(These are the values of variables which are taken from keyboard).
3. Write a C program that take a number N as input and display on the screen whether N is odd
or even. (Hint: if N is divided by 2 and its remainder is 0 then it is even, use % as remainder
operator)
Sample output:
Enter a number: 8
8 is even
OR
LAB NO. 10
for loop, while loop
Objectives of this lab:
#include <iostream.h>
int main()
int count;
cout<<"Hello\n";
return 0;
#include <iostream.h>
int main()
int count;
Count=2*count;
cout<<count<<endl;
return 0;
int main()
Cout<<"\n”<<i<<j;
return 0;
Example 3:
int i = 0;
cout<<“\n”<< i;
Example 4:
int i = 0;
cout<<“\n”<<i;
i++;
Example 6:
int i = 0;
for( ; ; ){
cout<<“\n”<< i;
i++;
Example 7:
#include<iostream.h>
int main(){
int i=0,j=10;
cout<<"\n"<<i<<j;
i++;
return 0;
#include <iostream.h>
int main()
int counter = 1;
return 0;
Our 2nd example is based on a while-loop that keeps on running until a certain condition is
reached (a certain value is entered by the user).
int main()
cin>>flag;
while(flag != -1) {
cin>>flag;
return 0;
Take Home
1. Write a program to generate a list of first 100 odd numbers using while, do while and for
loops.
Example:
Output:
3 * 2 = 6
3 * 4 = 12
3 * 6 = 18
3 * 8 = 24
……………………….
……………………….
3 * 14 = 42
3. Write a program to find the sum of digits of the number entered by the user also print it in
reverse order.For example, user enters 1234, the sum should be 10 and the program should print
4321. (HINT: use modulus operator)
4. Write a program to ask the user his/her CGPA and print his/her grade accordingly. If grade is
C or better give good remarks otherwise leave an advice. (User is a FAST graduate)
5. Write a program which has the following output screen. (use loops to control output of the
program)
For sine, cosine, Tangent and Square root you can use functions available in math.h
library.
7. Write a program that takes as input any number of seconds (as int) and then converts
it in hours, minutes and seconds. For example, if you enter 7802 the program should
print:
2 hrs 10 mins 2 secs
This is an easy way to evaluate sin(x) or cos(x), by increasing the number of terms the accuracy
of the result is increased.
Requirements
You are required to implement a program which takes as input:
• The “actual” value using the library functions sin and cos found in the header file <math.h>
(use the Deitel book as reference to see how this can be accomplished)
• The error in your value as compared to the actual value (calculated value – original value)
X= (Angle in Degress) * π
180
Note that π (pi) is a constant value and should be taken as:
3.1415926535897932384626433832795
LAB NO. 11
Do-While loop
Objectives of this Lab:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
int main( )
do
return 0;
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
do
cout << "Invalid mark. Try again. " << endl << endl;
Return 0;
sum = 0;
counter = 0;
do
counter++;
cout << "The average of the marks entered is " << sum/counter << endl;
Above code is incomplete, write complete code in the editor and show the output.
LAB NO. 12
Continue, Switch and Break Statements
Switch statement
Break Statement
Continue statement
Switch Statement:
#include <iostream.h>
int main() {
int a;
cin>>a;
switch (a) {
case 1:
case 2:
case 3:
case 4:
return 0;
#include <iostream.h>
int main() {
int a;
cin>>a;
switch (a) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
default:
return 0; }
Continue Statement:
This does the opposite of break. Try the example below and compare it with the one above.
int index=0;
for(index=1;index<=10;index++)
{
if(index==4||index==5)
continue;
cout<<index;
}
cout<<"\nLoop terminated” ;
LAB No. 13
Functions
Objectives of this lab:
#include<iostream.h>
void main(){
int i=0;
for(;i<10;i++)
cout<<"*";
Lets go one step ahead, function asterisks (int a) with a single argument.
#include<iostream.h>
void main(){
for(;i<num;i++)
cout<<"*";
return sum;
Let’s do another example of making a program using functions which will tell us whether the
input number is even or odd.
#include <iostream.h>
void main()
cin>>number;
if(test==0)
else
int remainder;
remainder= n%2;
if(remainder==1)
return 0;
else
return 1;
int remainder;
remainder= n%2;
if(remainder==1)
return 0;
else
return 1;
void main()
cin>>number;
if(test==0)
else
void main()
int number;
cin>>number;
else
int remainder;
remainder= n%2;
if(remainder==1)
else
return 1;
#include <iostream.h>
void main()
float n1,n2,n3,result;
cin>>n1>>n2>>n3;
return average;
LAB 14
Functions (Call By Reference and By
Value)
Objectives of this lab:
Call by Value:
Example 1:
int main( ) {
int i = 8;
func( i );
return 0;
i = i + 10;
Example 2:
void interchange(int,int);
int
x=50,
y=70;
interchange(x,y);
cout<<“ x= ”<<x<<” y= ”<<y;
return 0;
void interchange(int,int)
{
int z1;
z1=x1;
x1=y1;
y1=z1;
cout<<“x1 is = “<<x1<<”y1 is “<<y1;
}
Call by Reference:
Example 3:
#include <iostream.h>
void interchange(int&,int&);
int main()
interchange(x,y);
int z1;
z1=x1;
x1=y1;
y1=z1;
Here the function is called by reference. In other words address is passed by using symbol “&”
and the value is accessed by using symbol “*”.
The main difference between them can be seen by analyzing the output of program1 and
program2.
LAB NO. 15
Arrays
Objectives of this lab:
Declaration of Array
Initialization of Array
Printing arrays
Copying arrays
Code
#include <stdio.h>
int main()
int age;
age=23;
cout<<endl<< age;
return 0;
1) Declaration of Array
#include <iostream.h>
int main()
age[1]=34;
age[2]=65;
age[3]=74;
return 0;
2) Initialization of Array
It is like a variable, an array can be initialized. To initialize an array, we provide initializing
values which are enclosed within curly braces in the declaration and placed following an
equals sign after the array name. Here is an example of initializing an integer array.
int age[4]={23,34,65,74};
3) Printing arrays
#include <iostream.h>
int main()
int age[4];
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
return 0;
How about printing out each of the values separately? Try this:
#include <iostream.h>
int main()
int age[4];
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
cout<<age[0]<<endl;
cout<<age[1]<<endl;
cout<<age[2]<<endl;
cout<<age[3]<<endl;
return 0;
Lines (10) through line (13) produce the output we are expecting.
Thus there is no single statement in the language that says "print an entire array to the screen".
Each element in the array must be printed to the screen individually.
4) Copying arrays
Suppose that after filling our 4 element array with values, we need to copy that array to another
array of 4 int ? Try this:
int main()
int age[4];
int same_age[4];
int i=0;
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
for (;i<4;i++)
same_age[i]=age[i];
for (i=0;i<4;i++)
cout<<same_age[i]<<endl;
return 0;
cin>>a[0]; // this will scan the value for the very first location of the array.
cout<<a[0]<<endl;
You can also scan the entire elements of the array using a loop.