0% found this document useful (0 votes)
12 views8 pages

Lecture 18 Arrays 2

The document discusses arrays and functions in C++. It provides examples of declaring and initializing arrays, linear search on arrays, selection sort, using random numbers to populate arrays, and passing arrays to functions. Homework involves sorting arrays, merging arrays, finding the smallest element, and checking if an array is sorted.

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)
12 views8 pages

Lecture 18 Arrays 2

The document discusses arrays and functions in C++. It provides examples of declaring and initializing arrays, linear search on arrays, selection sort, using random numbers to populate arrays, and passing arrays to functions. Homework involves sorting arrays, merging arrays, finding the smallest element, and checking if an array is sorted.

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/ 8

Today lecture:

Arrays

Sample Programs
- Recap the previous lecture
- Correction of Count the number of positive integers
- Linear search
- Selection sort
-
arrays and function
Use of rand() and srand(time(0))

Arrays:
• 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
• Can we save height and weight information in same array? (context
should be same)

Syntax: name

datatype arrayName [size];


int age [10];
int C [10];
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.

Correction of code

int main()
{
int a[3] = { 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 < 3);

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

return 0;

Last lecture 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
• Sort the array elements in descending order

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
//scanf(“%d”, &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<cstdlib> // for rand() and srand()
#include<ctime> // for time(0)
#define SIZE 100

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++)
{
printf( "%d " ,arr[i]);
}
*/
printf("\nPlease enter the positve number ");
scanf("%d", &num);

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


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

return 0;

}
What is the output of the following expressions:

rand() % 6
1+ rand() % 6
rand() % 2
1+ rand() % 2
rand() % 100
1+ rand() % 100

Use to make fair dice, coin


• Make the cricket match simulation
Today lecture:

Example 10: function and array


When an array name is passed to a function it is called by a reference mechanism, whereas
simple array elements whenever are passed to a function it is implemented as call by value.

void getInput(int[], int);


int main()
{
int array[10];
getInput(array, 10);
for (int i = 0; i < 10; i++)
{
cout << array[i] << " ";
}
return 0;
}
void getInput(int a[], int s)
{

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


{
a[i] = i;
}
}
Example 8: Sort an array (Selection sort)
#include<ctime> // for time(0)
#include<cstdlib> // for rand() and srand()
#define SIZE 10

void getInput(int[], int);


int main()
{
srand(time(0));
int array[SIZE];

getInput(array, 10);

cout << "\nBefore sorinting array: ";

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


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

// Selection Sort:
int temp;
for (int i = 0; i < 10; i++)
{
for (int j = i + 1; j < 10; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;

}
}

}
cout << "\nAfter applying Selection Sort: ";
cout << "\nArray in asending order: ";

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


{
cout << array[i] <<" ";
}
return 0;
}
void getInput(int a[], int s)
{

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


{
a[i] = rand() % 100; // populate the array with numbers between 0-> 99
}
}
Homework:
- Void sortArray(int arr, int size);
- void mergeArray(int m[], int sm, int a1[], int s1, int a2[], int s2 );
- int findSmallest(int a[], int size);
- int isSorted(int a[], int size);

Next class…..
• Linear Vs binary search
• Character arrays
o character array and value of \0
o determine the length of the character array
o Comparison of arrays (Note in case of character arrays upper
and lower case array of same characters are different ; ali != ALI)

You might also like