Lab 2 Arrays
Lab 2 Arrays
Concept Map
What is a data structure?
It is a data organization, management and storage format that enables efficient access and
modification.
Examples are arrays, Lists, Binary trees, Heaps.
Today we will discuss and revise arrays.
Arrays: An array is a collection of data that holds fixed number of values of same type. For
example: int age[100];
Here, the age array can hold maximum of 100 elements of integer type. The size and type of
arrays cannot be changed after its declaration.
Declaring array:
type arrayName [ arraySize];
int students [5];
Initializing array: students = {10, 8, 9, 5,7};
Sample Program
#include <iostream>
using namespace std;
int main () {
int students [ 5], i; // n is an array of 5 integers
// initialize elements of array
cout << "Enter marks" <<endl;
for (int i = 0; i < 5; i++)
{
cin>>students[i]; }
// output each array element's value
for (int i = 0; i < 5; i++)
{cout << "Student" <<i<< "scored" <<students[i] << "marks" <<endl;}
return 0;}
Array Operations
There are some basic operations that can be performed on array which are adding data
element to array, searching and updating particular data element in an array and
deleting data element from array. In this lab the detailed description of these operations
I. Traverse Operation
This operation is to traverse through the elements of an array. Following program traverses
and prints the elements of an array:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{ int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
cout<< "The original array elements are"<<endl;
for(i = 0; i<n; i++) {
cout<< LA[i]<<endl;
}
return 0;}
V. Search Operation
1. First take number of elements in array as input from user and store it in a variable size.
2. Using a loop, take input from user and store it in array (Let the name of the array be
inputArray).
3. Ask user to enter element to be searched. Let it be num.
4. Now, using a for loop, traverse inputArray from index 0 to size-1 and compare num with
every array element. If num is equal to any array element then print a message saying
"Element found at index 4" otherwise print "Element Not Present".
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{ int del; int count;
int array[10],m;
cout<<"Enter the size of your array:";
cin>>m;
for(int i=0;i<m; i++)
{
cin>>array[i];}
cout<<"Enter the number to delete"<<endl;
cin>>del;
Pointers
A pointer is a variable whose value is the address of another variable. Like any variable or
constant, you must declare a pointer before you can work with it. The general form of a
pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of
the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you
use for multiplication. However, in this statement the asterisk is being used to designate a
variable as a pointer. Following are the valid pointer declaration −
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Use of Pointers
There are few important operations, which we will do with the pointers very frequently.
(a) We define a pointer variable.
(b) Assign the address of a variable to a pointer.
(c) Finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the
address specified by its operand.
Example
#include <iostream>
using namespace std;
int main() {
int *pc, c;
c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
c = 11; // The content inside memory address &c is changed from 5 to 11.
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
return 0;
}
Output
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5
NULL Pointers
A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream.
Void Pointer
A void pointer is a pointer that has no associated data type with it. A void pointer can hold
address of any type and can be type casted to any type.
int a = 10;
char b = 'x';
//Assignment
p = arr;
for(int i=0; i<6;i++){
cout<<*p<<endl;
//++ moves the pointer to next int position
p++; }
return 0;}
Output:
1
2
3
4
5
6
5 3 20 8 15 12
c. Function called remove to remove new value after specific value in list.
5 3 8 15 12
If we remove 8 from the array, the array will look like this:
5 3 15 12