Practical Notebook BSSE 1st

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

1

Practical Notebook
Prepared By:
Student Name: Ahmed

Roll No: 123

Subject
Programming Fundamentals CSI-301 4(3-1)

DEPARTMENT OF SOFTWARE ENGINEERING


DIVISIONAL PUBLIC SCHOOL AND COLLEGE FAISALABAD
2023
2

Certificate

This is to certify that Ahmed bearing Registration No. 2022-GCUF-


07123 is a hardworking student at DPSC and has completed the
Practical Notebook of subject Programming Fundamentals Subject
Code CSI-301 at the Department of Software Engineering, Divisional
Public School and College Faisalabad is an authentic work and carried
out under my supervision and guidance.

Supervisor
Fahad Rasheed Signature

Head of Department Signature


3

Acknowledgement
I carried the work reported in this Practical Notebook under the
supervision of the Supervisor Mr. Fahad Rasheed, at Divisional Public
School and College Faisalabad. I as a result of this declare that this
notebook and the contents of the practical notebook are the product of
my practical implementation and no part has been copied from any
other written or published source (except the references, standard
mathematical or genetics models / equation / formulas / protocol and
recommended book etc.).

Student Name:- Ahmed


Registration No:- 2022-GCUF-07123
Signature
4

Contents
Introduction to Programming ....................................................................................................................... 7
1.1 Problem Solving:................................................................................................................................ 7
1.2 Program: ............................................................................................................................................ 7
1.3 C++: ................................................................................................................................................... 7
Introduction To C++ ....................................................................................................................................... 8
2.1 Object Oriented:................................................................................................................................ 8
2.2 Brevity: .............................................................................................................................................. 8
2.3 Header Files:...................................................................................................................................... 8
2.4 Breakpoints: ...................................................................................................................................... 8
Programming In C++...................................................................................................................................... 9
Overflow and Underflow: ......................................................................................................................... 9
Program 3.1 ............................................................................................................................................... 9
Define Directive:........................................................................................................................................ 9
Program 3.2 ............................................................................................................................................... 9
Program 3.3 ............................................................................................................................................. 10
Compound Assignment Operations: ....................................................................................................... 10
Program 3.4 ............................................................................................................................................. 11
Postfix Increment Operator:.................................................................................................................... 11
Prefix Increment Operator: ..................................................................................................................... 11
Program 3.5 ............................................................................................................................................. 11
Program 3.6 ............................................................................................................................................. 12
Postfix Decrement Operator: .................................................................................................................. 12
Prefix Decrement Operator: .................................................................................................................... 13
Program 3.7 ............................................................................................................................................. 13
Program 3.8 ............................................................................................................................................. 13
Program 3.9 ............................................................................................................................................. 14
Explicit Casting: ....................................................................................................................................... 14
Program 3.10 ........................................................................................................................................... 14
Input And Output ........................................................................................................................................ 16
Output: .................................................................................................................................................... 16
Program 4.1 ............................................................................................................................................. 16
Floating point numbers: .......................................................................................................................... 16
5

Program 4.2 ............................................................................................................................................. 16


Program 4.3 ............................................................................................................................................. 17
Cout Statement: ...................................................................................................................................... 17
Program 4.4 ............................................................................................................................................. 17
Setw Statement:...................................................................................................................................... 18
Program 4.5 ............................................................................................................................................. 18
Setprecision Manipulator:....................................................................................................................... 18
Program 4.6 ............................................................................................................................................. 18
Setfill Manipulator: ................................................................................................................................. 19
Program 4.8 ............................................................................................................................................. 19
ASCII Code: .............................................................................................................................................. 20
Program 4.11 ........................................................................................................................................... 20
Quotient And Remainder: ....................................................................................................................... 20
Program 4.12 ........................................................................................................................................... 20
Program 4.15 ........................................................................................................................................... 21
' If ' Statement: ........................................................................................................................................ 22
Program 5.1 ............................................................................................................................................. 22
Program 5.2 ............................................................................................................................................. 22
' If else ' Statement:................................................................................................................................. 23
Program 5.8 ............................................................................................................................................. 23
Leap Year: ................................................................................................................................................ 23
Program 5.9 ............................................................................................................................................. 23
Nested ' If ' Statement:............................................................................................................................ 24
Program 5.17 ........................................................................................................................................... 24
AND Operator: ........................................................................................................................................ 25
Program 5.18 ........................................................................................................................................... 25
NOT Operator: ......................................................................................................................................... 25
Program 5.21 ........................................................................................................................................... 25
' goto ' Statement: ................................................................................................................................... 26
Program 5.30 ........................................................................................................................................... 26
Looping Structures ...................................................................................................................................... 27
While Loop: ............................................................................................................................................. 27
Program 6.1 ............................................................................................................................................. 27
Program 6.4 ............................................................................................................................................. 27
6

'do while' Loop: ....................................................................................................................................... 28


Program 6.18 ........................................................................................................................................... 28
'for' Loop: ................................................................................................................................................ 28
Program 6.23 ........................................................................................................................................... 28
Program 6.24 ........................................................................................................................................... 29
'continue' Statement: .............................................................................................................................. 29
Program 6.34 ........................................................................................................................................... 29
'break' Statement: ................................................................................................................................... 30
Program 6.35 ........................................................................................................................................... 30
Nested for Loop:...................................................................................................................................... 30
Program 6.41 ........................................................................................................................................... 30
Program 6.43 ........................................................................................................................................... 31
Arrays: ..................................................................................................................................................... 32
Program 7.1 ............................................................................................................................................. 32
Program 7.2 ............................................................................................................................................. 32
Two Dimensional Array: .......................................................................................................................... 33
Program 7.14 ........................................................................................................................................... 33
Initializing 2-D Arrays: ............................................................................................................................. 34
Program 7.15 ........................................................................................................................................... 34
7

Introduction to Programming

1.1 Problem Solving:


Problem solving refers to the cognitive process of finding solutions to
complex or challenging issues or situations. It involves identifying and defining a
problem, gathering relevant information, generating potential solutions, evaluating
those solutions, and implementing the best course of action. Problem solving often
requires critical thinking, creativity, logical reasoning, and decision-making skills.
Techniques of problem solving are as follows:
• Program
• Algorithm
• Flowchart

1.2 Program:
A program is a set of instructions or a sequence of code written in a
programming language that is designed to perform specific tasks or operations when
executed by a computer. It is a logical representation of a desired computation or
algorithm that tells the computer what operations to carry out and in what order.
Programs can range from simple scripts that automate repetitive tasks to complex
software applications that provide extensive functionality.

1.3 C++:
C++ is a general-purpose programming language that was developed as an
extension of the C programming language. It was designed with a focus on
efficiency, flexibility, and low-level programming, while also providing high-level
abstractions and support for object-oriented programming (OOP) concepts.
8

Introduction To C++
2.1 Object Oriented:
Object-oriented programming (OOP) is a programming paradigm
that organizes code around objects, which are instances of classes. It is a way of
structuring and designing software applications based on the concept of objects that
interact with each other through methods, properties, and messages.

2.2 Brevity:
Brevity refers to the quality of being concise and succinct in code. It means
writing code in a concise and efficient manner, reducing unnecessary repetition,
verbosity or complexity. Brevity aims to make the code easier to read, understand,
and maintain.

2.3 Header Files:


Header files are files that contain declarations and definitions of
functions, classes, variables, constants, and other programming elements. They are
typically used to declare the interface of a module or library, allowing other parts of
the code to use its functionality without needing to know the implementation details.

2.4 Breakpoints:
A breakpoint is a debugging feature provided by development
environments and debuggers. It is a designated point in your code where program
execution will pause, allowing you to inspect the state of your program, variables,
and data structures at that specific location.

9

Programming In C++
Overflow and Underflow:
An integer overflow occurs when the result of an arithmetic operation
exceeds the maximum value that can be represented by the data type.On the other
hand, an integer underflow occurs when the result of an arithmetic operation is
smaller than the minimum value that can be represented by the data type.

Program 3.1
Write a program that explains the concept of overflow and underflow.

#include<iostream>
using namespace std;
int main()
{
short testVar=32767;
cout<<testVar<<endl;
testVar=testVar+1;
cout<<testVar<<endl;
testVar=testVar-1;
cout<<testVar<<endl;
}

Define Directive:
A directive is a preprocessor directive that provides instructions to the
compiler before the actual compilation process begins. Directives are not part of the
C++ language itself but are interpreted by the preprocessor, which performs text
manipulation tasks before the code is compiled.

Program 3.2
Write a program that inputs the radius of a circle and displays the
circumference by using formula 2𝜋𝑅. Store the value of 𝜋 in a constant by using
DEFINE directive.
10

#include<iostream>
using namespace std;
int main()
#define PI 3.141
{
float r,area;
cout<<"Enter radius:";
cin>>r;
area=2.0*PI*r;
cout<<"Area="<<area;
}

Program 3.3
Write a program that perform all mathematical operations on two variables.

#include<iostream>
using namespace std;
int main()
{
int a,b;
a=10;
b=5;
cout<<"a+b="<<a+b<<endl;
cout<<"a-b="<<a-b<<endl;
cout<<"a*b="<<a*b<<endl;
cout<<"a/b="<<a/b<<endl;
cout<<"a%b="<<a%b<<endl;
}

Compound Assignment Operations:


Compound assignment operations are shorthand notations that combine
an arithmetic or bitwise operation with an assignment. They allow you to perform
an operation on a variable and update its value in a single statement. Compound
assignment operations are commonly used to make code more concise and efficient.
11

Program 3.4
Write a program that performs all compound assignment operation on an
integer.

#include<iostream>
using namespace std;
int main()
{
int a;
a=10;
cout<<"value of a:"<<a<<endl;
a+=5;
cout<<"value of a after a+=5:"<<a<<endl;
a-=5;
cout<<"value of a after a-=5:"<<a<<endl;
a*=2;
cout<<"value of a after a*=2:"<<a<<endl;
a/=2;
cout<<"value of a after a/=2:"<<a<<endl;
a%=2;
cout<<"value of a after a%=2:"<<a<<endl;
}

Postfix Increment Operator:


The postfix increment operator is denoted by two plus signs (++). It
increments the value of the variable by 1 and returns the original value before the
increment.

Prefix Increment Operator:


The prefix increment operator is denoted by two plus signs (++). It
increments the value of the variable by 1 and returns the updated value after the
increment.

Program 3.5
Write a program that explains the difference of postfix increment operator and
prefix increment operator used as independent expression.
12

#include<iostream>
using namespace std;
int main()
{
int a,b,x,y;
a=b=x=y=0;
a++;
b=a;
++x;
y=x;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
cout<<"x="<<x<<endl<<"y="<<y<<endl;
}

Program 3.6
Write a program that explains the difference of postfix increment operator
and prefix increment operator used as part of larger expression.

#include<iostream>
using namespace std;
int main()
{
int a,b,x,y;
a=b=x=y=0;
b=a++;
y=++x;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
cout<<"x="<<x<<endl<<"y="<<y<<endl;
}

Postfix Decrement Operator:


The postfix decrement operator (--) is applied after the variable it
operates on. It first evaluates the expression and then decreases the value of the
variable by 1. The result of the expression is the original value of the variable before
the decrement.
13

Prefix Decrement Operator:


The prefix decrement operator (--) is applied before the variable it operates
on. It first decreases the value of the variable by 1 and then evaluates the expression.
The result of the expression is the updated value of the variable after the decrement.

Program 3.7
Write a program that explains the difference of postfix decrement operator
and prefix decrement operator used as independent expression.

#include<iostream>
using namespace std;
int main()
{
int a,b,x,y;
a=b=x=y=0;
a--;
b=a;
--x;
x=y;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
}

Program 3.8
Write a program that explains the difference of postfix decrement operator
and prefix decrement operator used as part of large expression.

#include <iostream>
using namespace std;
int main()
{
int a,b,x,y;
a=b=x=y=0;

b=a--;
y=--x;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
14

cout<<"x="<<x<<endl<<"y="<<y<<endl;
}

Program 3.9
Write a program that solves the following expressions:
a*b / (- c * 31 % 13 ) * d
Assuming the values of variables are as follows:
a=10, b=20, c=15, d=8, e=40

#include<iostream>
using namespace std;
int main()

{
int a,b,c,d,r;
a=10;
b=20;
c=15;
d=8;
r=a*b/(-c*31%13)*d;
cout<<"result of expression is:"<<r<<endl;
}

Explicit Casting:
explicit casting is a way to explicitly convert a value from one data type
to another.

Program 3.10
Write a program that divides two float variables and finds the remainder by
using explicit casting.

#include <iostream>
using namespace std;
int main()
{
15

float a,b;
int c;
a=10.3;
b=5.2;
c=(int)a%(int)b;
cout<<" result is "<<c;
}
16

Input And Output

Output:
Any information processed by and sent out from a computer or other electronic
device is considered output.

Program 4.1
Write that displays a message and values of integer and characters variables.

#include <iostream>
using namespace std;
int main()
{
int n=10;
char ch='*';
cout<<"Testing output...";
cout<<n;
cout<<ch;
}

Floating point numbers:


Floating-point numbers are a data type used to represent real numbers
with fractional parts. They are typically used for calculations involving non-integer
values.

Program 4.2
Write a program that adds two floating point numbers and shows the sum on
screen.

#include <iostream>
using namespace std;
int main()
17

{
float var1,var2,res;
var1=24.27;
var2=41.50;
res=var1+var2;
cout<<var1<<" + "<<var2<<" = "<<res;
}

Program 4.3
Write a program to calculate and print the square of area with given height and
width.

#include <iostream>
using namespace std;
int main()
{
int height,width,area;
height=5;
width=4;
area=height*width;
cout<<"Area of Square = "<<area;
}

Cout Statement:
The cout statement is a part of the Standard Library's iostream header and is
used for output operations. It is commonly used to display or print information to
the console or other output streams.

Program 4.4
Write a program to display the following output using single cout statement.
*
**
***
****
18

#include <iostream>
using namespace std;
int main()
{
cout<<" *\n **\n ***\n ****";
}

Setw Statement:
The setw manipulator is used to control the width of the output field when
printing values. It is part of the <iomanip> header and can be used with the
std::setw() function.

Program 4.5
Write a that explains the use of setw statement.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n=3928;
double d=91.5;
char str[]="OOP using C++";
cout<<"("<<setw(5)<<n<<")"<<endl;
cout<<"("<<setw(8)<<d<<")"<<endl;
cout<<"("<<setw(16)<<str<<")"<<endl;
}

Setprecision Manipulator:
The setprecision manipulator is used to set the precision (number of
decimal places) for floating-point values when outputting them to a stream. It is
defined in the <iomanip> header.

Program 4.6
Write a program that displays the values of different variables using
setprecision manipulator.
19

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double r,n1=132.364,n2=26.91;
r=n1/n2;
cout<<" "<<r<<endl;
cout<<setprecision(5)<<" "<<r<<endl;
cout<<setprecision(4)<<" "<<r<<endl;
cout<<setprecision(3)<<" "<<r<<endl;
cout<<setprecision(2)<<" "<<r<<endl;
cout<<setprecision(1)<<" "<<r<<endl;
}

Setfill Manipulator:
setfill is a manipulator that sets the fill character used by stream insertion
operations. It is part of the <iomanip> header and can be used in conjunction with
std::setw to control the width and alignment of output.

Program 4.8
Write a program to display the values of different variables using setfill
manipulator.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="OOP using C++";
cout<<setw(20)<<setfill('*')<<str<<endl;
cout<<setw(20)<<setfill('@')<<str<<endl;
cout<<setw(20)<<setfill('=')<<str<<endl;
}
20

ASCII Code:
The integer values to represent ASCII characters. The ASCII standard defines a
set of 128 characters, each corresponding to a unique integer value from 0 to 127.

Program 4.11
 Write a program that inputs a character and displays its ASCII code.

#include <iostream>
using namespace std;
int main()
{
char charac;
cout<<"Enter the character:";
cin>>charac;
int num1=charac;
cout<<"The ASCII code for "<<charac<<" is "<<num1<<""<<endl;
}

Quotient And Remainder:


The quotient is the result of integer division, which represents the whole
number part of the division operation. On other hand, The remainder is the integer
value that remains after dividing one number by another. It represents the "leftover"
or the fractional part that cannot be evenly divided.

Program 4.12
Write a program that inputs dividend and divisor. It then calculates and displays
the quotient and remainder.

#include <iostream>
using namespace std;
int main()
{
int div,dis,q,r;
cout<<"Enter dividend & divisor:";
21

cin>>div>>dis;
q=div/dis;
r=div%dis;
cout<<"Quotient="<<q<<endl;
cout<<"Remainder="<<r;
}

Program 4.15
Write a program that inputs distance traveled and the speed of vehicle. It
calculates the time required to reach the destination and displays it.

#include <iostream>
using namespace std;
int main()
{
double distance,time,speed;
cout<<"Enter distance traveled in miles:";
cin>>distance;
cout<<"Enter speed of vehichle(mph):";
cin>>speed;
time=distance/speed;
cout<<"Time required to reach desyination"<<time<<"hours"<<endl;

}





22

Conditional Structures
' If ' Statement:
The if statement is used for conditional execution of code. It allows you to
specify a condition, and if that condition evaluates to true, the code inside the if
block will be executed.

Program 5.1
Write a program that inputs marks and displays "congratulations! you have
passed." if the marks are 40 or more.

#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter your marks: ";
cin>>marks;
if(marks>=40)
cout<<"Congratulations! You have passed.";
}

Program 5.2
Write a program that inputs two numbers and finds whether both are equal.

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers:\n";
cin>>a>>b;
if(a==b)
cout<<"Both are equal";
}
23

' If else ' Statement:


An if-else statement is a control flow statement used to make decisions
based on certain conditions. It allows your program to execute different blocks of
code depending on whether a particular condition is true or false.

Program 5.8
Write a program that inputs a number and finds whether it is even or odd using
if-else structure.

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number: ";
cin>>n;
if(n%2==0)
cout<<n<<" is even.";
else
cout<<n<<" is odd.";
}

Leap Year:
A leap year is a year that has one additional day, February 29th, compared to a
regular year.A year is a leap year if it is divisible by 4.

Program 5.9
Write a program that inputs a year and finds whether it is a leap year or not using
if else structure.

#include<iostream>
using namespace std;
int main()
{
int y;
24

cout<<"Enter a year: ";


cin>>y;
if(y%4==0)
cout<<y<<" is a leap year";
else
cout<<y<<" is not a leap year";
}

Nested ' If ' Statement:


Nested if statements are used when you want to have multiple levels of
conditions. A nested if statement is an if statement that is placed inside another if
statement. It allows you to test for multiple conditions and execute different blocks
of code based on those conditions.

Program 5.17
Write a program that inputs three numbers and displays whether all numbers
are equal or not by using nested if conditions.

#include<iostream>
using namespace std;
int main()
{

int a,b,c;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a==b)
if(b==c)
cout<<"All numbers are equal.";
else
cout<<"Numbers are different.";
else
cout<<"Numbers are different.";
}
25

AND Operator:
The "AND" operator is denoted by the double ampersand (&&). It is a logical
operator that performs a logical conjunction, evaluating to true if both of its
operands are true and false otherwise.

Program 5.18
Write a program that inputs three numbers and displays the maximum numbers
by using logical operators.

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<"Maximum number is "<<a;
else if(b>a&&a>c)
cout<<"Maximum number is "<<b;
else
cout<<"Maximum number is "<<c;
}

NOT Operator:
The "NOT" operator is represented by the exclamation mark (!). It is a unary
logical operator that performs the logical negation of its operand. It reverses the
logical state of a condition, resulting in true if the condition is false, and false if the
condition is true.

Program 5.21
Write a program that inputs a number and displays whether it is even or odd by
using logical operator "!".
#include<iostream>
using namespace std;
26

int main()
{
int n;
cout<<"Enter any number: ";
cin>>n;
if(!(n%2==0))
cout<<"You enter odd number.";
else
cout<<"You enter even number.";
}

' goto ' Statement:


The goto statement is a control transfer statement that allows you to transfer
the program's control to a specified label within the same function or block.

Program 5.30
Write a program that displays "C++" by using goto statement.

#include<iostream>
using namespace std;
int main()
{
int n=1;
loop:
cout<<n<<":C++"<<endl;
n++;
if(n<=5)goto loop;
cout<<"End of program";
}
27

Looping Structures
While Loop:
A while loop is a control flow statement that repeatedly executes a block of
code as long as a specified condition is true.

Program 6.1
Write a program that displays "Pakistan" for five times using while loop.

#include<iostream>
using namespace std;
int main()
{
int n=1;
while(n<=5)
{
cout<<"Pakistan"<<endl;
n++;
}
}

Program 6.4
Write a program that displays five numbers with their squares using while loop.

#include<iostream>
using namespace std;
int main()
{
int n=1;
while(n<=5)
{
cout<<n<<" "<<n*n<<endl;
n++;
}
}
28

'do while' Loop:


The do-while loop is a control flow statement that allows you to repeatedly
execute a block of code while a specified condition is true.

Program 6.18
Write a program that displays counting from 10 to 1 using do-while loop.

#include<iostream>
using namespace std;
int main()
{
int c=10;
do
{
cout<<c<<endl;
c=c-1;
}
while(c>=1);

'for' Loop:
A for loop is a control flow statement used to repeatedly execute a block of code
a certain number of times. It consists of three main parts: initialization, condition,
and increment/decrement.

Program 6.23
Write a program that displays counting from 1 to 5 using for loop.

#include<iostream>
using namespace std;
int main()
{
int n;
for(n=1;n<=5;n++)
cout<<n<<endl;
}
29

Program 6.24
Write a program that displays product of all odd numbers from 1 to 10 using for
loop.

#include<iostream>
using namespace std;
int main()
{
long int product=1;
int c;
for(c=1;c<=10;c=c+2)
product*=c;
cout<<"Result is "<<product;
}

'continue' Statement:
The continue statement is used within loops to skip the remaining code in
the current iteration of the loop and proceed to the next iteration. It is commonly
used to selectively skip certain iterations based on a specific condition. The continue
statement is only applicable within loops such as for, while, and do-while.

Program 6.34
Write a program that displays the sum of following series:
1+3+5+7+....100

#include<iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=1;i<100;i++)
{
if(i%2==0)
continue;
sum=sum+i;
}
30

cout<<"The sum is "<<sum;


}

'break' Statement:
The break statement is a control flow statement that is used to immediately exit
a loop or switch statement. It is typically used to terminate the execution of a loop
when a certain condition is met or to exit a switch statement once a matching case is
found.

Program 6.35
Write a program that inputs number from the user using for loop. If the number
is greater than 0 ,it is displayed and next number is input. The program exists the
loop if the number is 0 or negative using break statement.

#include<iostream>
using namespace std;
int main()
{
int x,num;
for(x=1;x<=5;x++)
{
cout<<"Enter a number: ";
cin>>num;
if(num<=0)
break;
cout<<"You enter "<<num<<endl;
}
}

Nested for Loop:


A nested for loop is a loop construct where one loop is placed inside another
loop. This allows you to iterate over multiple levels or dimensions of data structures
or perform repetitive tasks that require multiple levels of iteration.

Program 6.41
Write a program that displays the following block using nested for loop.
31

#include<iostream>
using namespace std;
int main()
{
int m,n;
for(m=1;m<=5;m++)
{
for(n=1;n<=5;n++)
cout<<"*";
cout<<endl;
}
}

Program 6.43
Write a program that displays the following shape using nested for loops.

#include<iostream>
using namespace std;
int main()
{
int i,j,s;
for(i=5;i>=1;i--)
{
for(s=1;s<=5-i;s++)
cout<<" ";
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
32

Arrays
Arrays:
An array is a fixed-size collection of elements of the same data type that are stored
in contiguous memory locations. Arrays provide a way to store multiple values of
the same type under a single name.

Program 7.1
 Write a program that inputs three integer from user and stores them in an array.
It then displays all values in array without using loop.

#include<iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Enter three integers: "<<endl;
cin>>arr[0];
cin>>arr[1];
cin>>arr[2];
cout<<"The values in array are: \n";
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
}

Program 7.2
Write a program that inputs five integer from user and stores them in an array. It
then displays all values in the array using loops.

#include<iostream>
using namespace std;
int main()
{
33

int arr[5],i;

for(i=0;i<5;i++)
{
cout<<"Enter an integer: ";
cin>>arr[i];
}
cout<<"The values in array are: \n";
for(i=0;i<5;i++)
cout<<arr[i]<<endl;
}

Two Dimensional Array:


A two-dimensional array is an array of arrays, where each element of the outer
array is itself an array. It provides a way to store and manipulate data in a tabular
format with rows and columns.

Program 7.14
Write a program that stores integer values in an array of 2 rows and 4 columns.

#include<iostream>
using namespace std;
int main()
{
int arr[2][4],i,j;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
{
cout<<"Enter an integer: ";
cin>>arr[i][j];
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;
34

}
}

Initializing 2-D Arrays:


Two-dimensional arrays may be initialized by following their declaration with
a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the
elements of the first row to zero and the second row to one. The initialization is done
row by row.

Program 7.15
Write a program that initializes a 2-D array of 2 row and 3 columns and then
displays its values.

#include<iostream>
using namespace std;
int main()
{
int i,j,arr[2][3]={15,21,9,84,33,72};
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<"\t";
cout<<endl;
}
}

You might also like