0% found this document useful (0 votes)
10 views7 pages

Lecture 17 Arrays

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Lecture 17 Arrays

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Today lecture: Arrays

Motivation of arrays
Initialization of Arrays
Sample Programs
- Average age of students
- Count the number of positive integers
- Sum of the square of array elements
- Copying
- Linear search
Use of #define SIZE 100
Use of rand() and srand(time(NULL))
Motivation of arrays:
Let us consider an example about calculation of average age of 10 students. At first, we will declare
10 variables to store the age of each student and then sum up all the ages and divide this with 10 to
get the average age. Suppose, we have 100 students instead of 10, we have to declare 100 variables
i.e. one for each student’s age. Is there any other way to deal with this problem? Arrays are possible
solution to the problem.

Arrays:
Array is a special data-type. If we have a collection of data of same type as in the case of storage of
ages of 100 students, arrays can be used. Arrays are data structure in which identical data types are
stored. The concept of arrays is being explained furtherin the following parts of the lecture.

• Arrays are like the data structure in which identical data types are
stored.
• An array has a name, data type, and size
• They occupy the consecutive area of memory

Syntax: name

datatype arrayName [size];


int age [10];
int C [10];
Usage of array
// initialization of array
int age[10] = {0,0,0,0,0,0,0,0,0,0};
OR

int age[10] = {0};

OR

for (int i = 0; i < 10; i++)


{
age[i] = 0;
}

Example 1: // find the average age of 10 students


int main()
{
int age[10] = {0};

// initialization of array

for (int i = 0; i < 10; i++)


{
age[i] = 0;
}
// find the average age of 10 students
int sum = 0;
for (int i = 0; i < 10; i++)
{
cout <<"Enter the age of stdunt: " << i + 1;
cin >> age[i];
sum = sum + age[i];
}
cout << "Average age is %d" << sum / 10;
return 0;
}
Example 2: // Count the positive number read from the user until -1 is entered
Write a program that reads positive integers from the user and stores these ones
in an array. Users can enter a maximum of 100 numbers. Stop taking input when a
user enters -1.
int main()
{
int a[100] = {0}, i = 0, input =0;

do
{
cout <<"Entered the positive number (enter -1 for end of input):";
cin >> input;
if (input != -1)
{
a[i] = input;
}
i++;
} while (input != -1 && i < 100);

printf("Total positve numbers are : %d", i - 1);


return 0;
}

Example 3: // copy the arrays

Both arrays must have same datatype and size


int main()
{
int a[100] = {0};
int b[100];

for (int i = 0; i < 100; i++)


{
b[i] = a[i];
}

cout << "Element at b[5] = ", b[5]);


return 0;
}
Example 4:
// Take the sum of squares of 10 different numbers stored in an array.
int main()
{
int a[10], sumOfSquare = 0;
// initialize the array
for (int i = 0; i < 10; i++)
{
a[i] = i; // take it from user
}

for (int i = 0; i < 10; i++)


{
sumOfSquare += a[i] * a[i];
}

cout <<"The sum of the square of array elements are: " << sumOfSquare;
return 0;
}

Example 4: use constant SIZE to make it easier to maintain the


program
#define SIZE 10
int main()
{

int arr[SIZE]
for (i = 0; i < SIZE; i++)
{
arr[i] = i;
}

return 0;
}
Example 5: Linear search (Guess a number)

#define SIZE 100

int main()
{

int arr[SIZE];
int num=0, flag = 0, i = 0;

for (i = 0; i < SIZE; i++)


{
arr[i] = i
//cin >>>arrp[i];
}
/*
for (i = 0; i < SIZE; i++)
{
cout << arr[i];
}
*/
cout <<"\nPlease enter the positve number ";
cin >> num;

for (i = 0; i < SIZE; i++) // linear search


{
if (arr[i] == num)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout <<"Congratulations!, we found the integer at " << i;
}
else
{
cout << "Sorry number is not found!";
}

return 0;

}
Example 6: Use the random function
• The function is rand() and is in the standard library.
• To access this function, we need to include <cstdlib> library in our program.
• This function will return a random number.
The number can be between 0 and 32767.
#include <iostream>
#include<cstdlib> // for rand() and srand()
#define SIZE 10

using namespace std;

int main()
{
srand(time(0));// To make rand() as the real random number generator

int arr[SIZE];
int num = 0, flag = 0, i = 0;

for (i = 0; i < SIZE; i++)


{

arr[i] = rand();
}

for (i = 0; i < SIZE; i++)


{
cout << arr[i] <<" ";
}

cout << "\nPlease enter the positve number ";


cin >> num;

for (i = 0; i < SIZE; i++) // linear search


{
if (arr[i] == num)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << "Congratulations!, we found the integer at " << i;
}
else
{
cout << "Sorry number is not found!";
}

return 0;
}
What is the output of the following expression:

rand() % 6
1+ rand() % 6 => Use to make fair dice
rand() % 2
1+ rand() % 2 = > Use to make fair coin

Use to make fair dice, coin


• Make the cricket match simulation.

Homework:
• Reverse the array elements from arrayA and store them in arrayB
• Comparing the two arrays (identical or not)
• Sort the array elements in ascending and descending order

You might also like