0% found this document useful (0 votes)
25 views10 pages

Arrays 2

The document provides an introduction to programming concepts like arrays, sorting, and merging sorted arrays. Code snippets are given to demonstrate different algorithms for sorting arrays and merging two sorted arrays. Examples and questions are included to help understand the concepts.

Uploaded by

gameramit7
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)
25 views10 pages

Arrays 2

The document provides an introduction to programming concepts like arrays, sorting, and merging sorted arrays. Code snippets are given to demonstrate different algorithms for sorting arrays and merging two sorted arrays. Examples and questions are included to help understand the concepts.

Uploaded by

gameramit7
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/ 10

Introduction to Programming

Raghavendra Kanakagiri

Fall 2023

Introduction to Programming Fall 2023 1 / 10


Quiz 2

The below code will be available in Quiz 2. No other sheets are allowed.
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < time .h >
4 int main ( void )
5 {
6 int arr1 [10] , arr2 [10];
7 srand ( time (0) ) ;
8 printf ( " Enter 10 numbers :\ n " )
9 for ( int i = 0; i < 10; i ++) {
10 arr1 [ i ] = rand () % 100;
11 scanf ( " % d " , & arr2 [ i ]) ;
12 }
13 for ( int i = 0; i < 10; i ++) {
14 printf ( " % d " , arr1 [ i ] + arr2 [ i ]) ;
15 }
16 printf ( " \ n " ) ;
17 return 0;
18 }
Introduction to Programming Fall 2023 2 / 10
Characters and Real Numbers

1 # include < stdio .h >


2 int main ( void )
3 {
4 float val ;
5 char c ;
6 printf ( " Enter a real number :\ n " ) ;
7 scanf ( " % f " , & val ) ;
8 printf ( " Enter a character :\ n " ) ;
9 scanf ( " % c " , & c ) ; / / a w h i t e s p a c e b e f o r e % c
10 printf ( " number : %f , char : % c \ n " , val , c ) ;
11
12 int x ;
13 x = val ;
14 char y ;
15 y = ’A ’;
16 printf ( " Int val is : % d new char : % c \ n " , x , y ) ;
17
18 return 0;
19 }
Introduction to Programming Fall 2023 3 / 10
Sorting

Given an array of size n such that each element is in the range of [0, 3] i.e. 0 ≤ ai ≤ 3, sort the
array.
1 // this is a single line comment in C. Comments are ignored by
the compiler
2 /* this is a multi - line
3 c o m m e n t in C */
4 int main ( void )
5 {
6 int n ;
7 scanf ( " % d " , & n ) ;
8 int arr [ n ];
9 // seed the pseudo - random number generator
10 srand ( time (0) ) ;
11 for ( int i = 0; i < n ; i ++) {
12 arr [ i ] = rand () % 4;
13 }
14 // initialize all the elements of count to 0
15 int count [4] = {0};
Introduction to Programming Fall 2023 4 / 10
Sorting

1 // iterate over the array and count the number of occurences of


each element
2 for ( int i = 0; i < n ; i ++) {
3 // if arr [i] == 0, count [0]++
4 // if arr [i] == 1, count [1]++
5 // if arr [i] == 2, count [2]++
6 // if arr [i] == 3, count [3]++
7 count [ arr [ i ]]++;
8 }
9 // Fill the array in sorted order
10 int k = 0;
11 for ( int i = 0; i < 4; i ++) {
12 // when i == 0; count [i] = number of 0s in the array
13 for ( int j = 0; j < count [ i ]; j ++) {
14 arr [ k ++] = i ;
15 }
16 }
17 return 0;
18 }
Introduction to Programming Fall 2023 5 / 10
Sorting

Sort an array of n elements in ascending order.


1 int main ( void )
2 {
3 int n ;
4 scanf ( " % d " , & n ) ;
5 int arr [ n ];
6 srand ( time (0) ) ;
7 for ( int i = 0; i < n ; i ++) {
8 arr [ i ] = rand () % 100;
9 }
10 for ( int i = 0; i < n - 1; i ++) {
11 // when i == 0; pick the largest element and put it at the
end ; the l a r g e s t e l e m e n t b u b b l e d up to the end !
12 // now treat the array as an array of size n - 1 and repeat
the process
13 // when i == 1; pick the l a r g e s t e l e m e n t and put it at the
end ( but the end is n - 2 since the last e l e m e n t is
already sorted )
Introduction to Programming Fall 2023 6 / 10
Sorting

1 for ( int j = 0; j < n - i - 1; j ++) {


2 if ( arr [ j ] > arr [ j + 1]) {
3 // swap arr [ j ] and arr [ j + 1]
4 // can you do this without using a temporary variable ?
5 int temp = arr [ j ];
6 arr [ j ] = arr [ j + 1];
7 arr [ j + 1] = temp ;
8 }
9 }
10 }
11 return 0;
12 }

Introduction to Programming Fall 2023 7 / 10


Sorting

What is the output if n = 4 and arr[4] = {4, 3, 2, 1}?


How many comparisons are made?
What is the output if n = 4 and arr[4] = {1, 2, 3, 4}?
How many comparisons are made?
Can you modify the code to make it more efficient?

Introduction to Programming Fall 2023 8 / 10


Merge two sorted arrays

Merge two sorted arrays of size n and m into a single sorted array of size n + m.
1 int main ( void )
2 {
3 int n , m ;
4 scanf ( " % d % d " , &n , & m ) ;
5 int arr1 [ n ] , arr2 [ m ];
6 // assume arr1 and arr2 are filled with sorted elements
7 int arr3 [ n + m ];
8 int i = 0 , j = 0 , k = 0;
9 while ( i < n && j < m ) {
10 // if arr1 [ i ] < arr2 [ j ] , copy arr1 [i] to arr3 [k] and
increment i and k
11 if ( arr1 [ i ] < arr2 [ j ]) {
12 arr3 [ k ++] = arr1 [ i ++];
13 }
14 else {
15 arr3 [ k ++] = arr2 [ j ++];
16 }
17 }
Introduction to Programming Fall 2023 9 / 10
Merge two sorted arrays

1 // copy the remaining elements from arr1 and arr2


2 while ( i < n ) {
3 arr3 [ k ++] = arr1 [ i ++];
4 }
5 while ( j < m ) {
6 arr3 [ k ++] = arr2 [ j ++];
7 }
8 return 0;
9 }

Introduction to Programming Fall 2023 10 / 10

You might also like