KEMBAR78
Programming data structure concept in array ppt | PPTX
PROGRAMMING AND DATA STRUCTURES
ARRAY
Mrs. A.SEETHA
Assistant Professor
Information Technology Department
S.A.Engineering College
GATE:SYLLABUS
Programming in C
Recursion.Arrays,stack,queues,linked
lists,trees,binary search trees,binary
heaps,graphs,binary heaps, graphs.
ARRAY
Memory Management (16) 3
Array is a collection of similar data type.
Syntax:
Data type_ array name[]={};
Representation of Array:
Representation of Array:
The representation of an array can be defined by its declaration. A declaration
means allocating memory for an array of a given size.
ARRAY DECLARATIONS.
5
int arr[5]; // This array will store integer type element
char arr[10]; // This array will store char type element
float arr[20]; // This array will store float type element
Types of arrays:
Memory Management (16) 6
There are mainly three types of arrays:
One Dimensional Array
Two Dimensional Array
Three-Dimensional Array
1. One Dimensional Array:
7
1. One Dimensional Array:
It is a list of the variable of similar data types.
It allows random access and all the elements can be accessed with the help
of their index.
The size of the array is fixed.
For a dynamically sized array, vector can be used
Representation of 1D array:
2. Two Dimensional Array:
8
2. Two Dimensional Array:
It is a list of lists of the variable of the same data type.
It also allows random access and all the elements can be accessed with the
help of their index.
It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
Its dimension can be increased from 2 to 3 and 4 so on.
Representation of 2 D array:
Three-Dimensional Array:
9
Three-Dimensional Array:
A Three Dimensional Array or 3D array in C is a collection of two-dimensional
arrays. It can be visualized as multiple 2D arrays stacked on top of each other.
Declaration of Three-Dimensional Array:
Memory Management (16) 10
Declaration of Three-Dimensional Array:
We can declare a 3D array with x 2D arrays each having y rows and z columns
using the syntax shown below.
Syntax:
data_type array_name[x][y][z];
Example of Array:
Memory Management (16) 11
#include<stdio.h>
#include<conio.h>
int main ()
{
int values[5];
printf(“Enter 5 integers”);
for(int i=0;i<5;++i)
{
Scanf(“%dn”,values[i]);
Printf(“Display the integers”);
for(int i=0;i<5;++i)
{
Printf(“%dn”,values[i]);
}
Return 0;
}
OUTPUT
?
Memory Management (16) 12
Question - 1
• Question 1: A program P reads in 500 integers in the range [0..100]
representing the scores of 500 students. It then prints the frequency of
each score above 50. What would be the best way for P to store the
frequencies?
• (a) An array of 50 numbers.
(b) An array of 100 numbers.
(c) An array of 500 numbers.
(d) A dynamically allocated array of 550 numbers
• Answer: (a)
Explanation: An array of 50 numbers is correct.
Question - 2
• Question 2: The minimum number of comparisons required to determine if an integer
appears more than n/2 times in a sorted array of n integers is
• (a) O(n)
(b) O(log(n))
(c) O(nlog(n))
(d) O(1)
• Answer: (b)
Explanation: If you answered Theta(1), then think of examples {1, 2, 2, 2, 4, 4}, {1, 1, 2,
2, 2, 2, 3, 3} The Best way to find out whether an integer appears more than n/2 times
in a sorted array(Ascending Order) of n integers, would be binary search approach.
• The First occurrence of an element can be found out in O(log(n)) time using divide and
conquer technique,lets say it is i.
• The Last occurrence of an element can be found out in O(log(n)) time using divide and
conquer technique,lets say it is j.
• Now number of occurrence of that element(count) is (j-i+1). Overall time complexity =
log n +log n +1 = O(log(n)).
Question - 3
• Question 3: Consider an array consisting of –ve and +ve numbers. What
would be the worst case time complexity of an algorithm to segregate the
numbers having same sign altogether i.e all +ve on one side and then all -
ve on the other ?
• (a) O(N)
(b) O(Nlog(N))
(c) O(N*N)
(d) O(Nlog(log(N)))
• Answer: (c)
Explanation: Here we can use the partition algorithm of quick sort for
Question 4
Question 7: Let A[1…n] be an array of n distinct numbers. If i < j and A[i] >
A[j], then the pair (i, j) is called an inversion of A. What is the expected
number of inversions in any permutation on n elements ?
(a) n(n-1)/2
(b) n(n-1)/4
(c) n(n+1)/4
(d) 2nlog(n)
Answer: (b)
Explanation: There are n(n-1)/2 pairs such that i < j. For a pair (ai, aj),
probability of being inversion is 1/2. Therefore expected value of inversions =
1/2 * (n(n-1)/2) = n(n-1)/4.
Question – 5
• Question 5: Consider the following array declaration in the ‘C’ language:
• int array 1[] = {2, 3};
int array2[3]={9};
What will be the output of the following print statement?
printf(“%d, %d”, array1 [1], array2 [2]);
(a) 2, 0
(b) 3, 0
(c) 2, 9
(d) 4, 9
• Answer: (b)
Question - 5
Determine the maximum length of the cable (in km) for transmitting data at a
rate of 500 Mbps in an Ethernet LAN with frames of size 10,000 bits. Assume
the signal speed in the cable to be 2,00,000 km/s.[GATE CSE 2013]
(A) 1
(B) 2
(C) 2.5
(D) 5
Answer (B)
Explanation for Question - 5
Given Data
Bandwidth: 500 Mbps
Frame Size: 10,000 bits
Signal Speed : 2,00,000 km/s
Tt >= 2*Tp
Length/Bandwidth >= 2* distance/Signal speed
Length =2*distance *Bandwidth / Signal Speed
10000 bits = 2 * distance * 500 * 106
b/s
2 * 105
km/s
10000 bits * 2 * 105
km/s = distance = 2km
2 * 500 * 106
b/s
so, answer is (B)  2km
THANK YOU
20

Programming data structure concept in array ppt

  • 1.
    PROGRAMMING AND DATASTRUCTURES ARRAY Mrs. A.SEETHA Assistant Professor Information Technology Department S.A.Engineering College
  • 2.
  • 3.
    ARRAY Memory Management (16)3 Array is a collection of similar data type. Syntax: Data type_ array name[]={};
  • 4.
    Representation of Array: Representationof Array: The representation of an array can be defined by its declaration. A declaration means allocating memory for an array of a given size.
  • 5.
    ARRAY DECLARATIONS. 5 int arr[5];// This array will store integer type element char arr[10]; // This array will store char type element float arr[20]; // This array will store float type element
  • 6.
    Types of arrays: MemoryManagement (16) 6 There are mainly three types of arrays: One Dimensional Array Two Dimensional Array Three-Dimensional Array
  • 7.
    1. One DimensionalArray: 7 1. One Dimensional Array: It is a list of the variable of similar data types. It allows random access and all the elements can be accessed with the help of their index. The size of the array is fixed. For a dynamically sized array, vector can be used Representation of 1D array:
  • 8.
    2. Two DimensionalArray: 8 2. Two Dimensional Array: It is a list of lists of the variable of the same data type. It also allows random access and all the elements can be accessed with the help of their index. It can also be seen as a collection of 1D arrays. It is also known as the Matrix. Its dimension can be increased from 2 to 3 and 4 so on. Representation of 2 D array:
  • 9.
    Three-Dimensional Array: 9 Three-Dimensional Array: AThree Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be visualized as multiple 2D arrays stacked on top of each other.
  • 10.
    Declaration of Three-DimensionalArray: Memory Management (16) 10 Declaration of Three-Dimensional Array: We can declare a 3D array with x 2D arrays each having y rows and z columns using the syntax shown below. Syntax: data_type array_name[x][y][z];
  • 11.
    Example of Array: MemoryManagement (16) 11 #include<stdio.h> #include<conio.h> int main () { int values[5]; printf(“Enter 5 integers”); for(int i=0;i<5;++i) { Scanf(“%dn”,values[i]); Printf(“Display the integers”); for(int i=0;i<5;++i) { Printf(“%dn”,values[i]); } Return 0; }
  • 12.
  • 13.
    Question - 1 •Question 1: A program P reads in 500 integers in the range [0..100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies? • (a) An array of 50 numbers. (b) An array of 100 numbers. (c) An array of 500 numbers. (d) A dynamically allocated array of 550 numbers • Answer: (a) Explanation: An array of 50 numbers is correct.
  • 14.
    Question - 2 •Question 2: The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is • (a) O(n) (b) O(log(n)) (c) O(nlog(n)) (d) O(1) • Answer: (b) Explanation: If you answered Theta(1), then think of examples {1, 2, 2, 2, 4, 4}, {1, 1, 2, 2, 2, 2, 3, 3} The Best way to find out whether an integer appears more than n/2 times in a sorted array(Ascending Order) of n integers, would be binary search approach. • The First occurrence of an element can be found out in O(log(n)) time using divide and conquer technique,lets say it is i. • The Last occurrence of an element can be found out in O(log(n)) time using divide and conquer technique,lets say it is j. • Now number of occurrence of that element(count) is (j-i+1). Overall time complexity = log n +log n +1 = O(log(n)).
  • 15.
    Question - 3 •Question 3: Consider an array consisting of –ve and +ve numbers. What would be the worst case time complexity of an algorithm to segregate the numbers having same sign altogether i.e all +ve on one side and then all - ve on the other ? • (a) O(N) (b) O(Nlog(N)) (c) O(N*N) (d) O(Nlog(log(N))) • Answer: (c) Explanation: Here we can use the partition algorithm of quick sort for
  • 16.
    Question 4 Question 7:Let A[1…n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. What is the expected number of inversions in any permutation on n elements ? (a) n(n-1)/2 (b) n(n-1)/4 (c) n(n+1)/4 (d) 2nlog(n) Answer: (b) Explanation: There are n(n-1)/2 pairs such that i < j. For a pair (ai, aj), probability of being inversion is 1/2. Therefore expected value of inversions = 1/2 * (n(n-1)/2) = n(n-1)/4.
  • 17.
    Question – 5 •Question 5: Consider the following array declaration in the ‘C’ language: • int array 1[] = {2, 3}; int array2[3]={9}; What will be the output of the following print statement? printf(“%d, %d”, array1 [1], array2 [2]); (a) 2, 0 (b) 3, 0 (c) 2, 9 (d) 4, 9 • Answer: (b)
  • 18.
    Question - 5 Determinethe maximum length of the cable (in km) for transmitting data at a rate of 500 Mbps in an Ethernet LAN with frames of size 10,000 bits. Assume the signal speed in the cable to be 2,00,000 km/s.[GATE CSE 2013] (A) 1 (B) 2 (C) 2.5 (D) 5 Answer (B)
  • 19.
    Explanation for Question- 5 Given Data Bandwidth: 500 Mbps Frame Size: 10,000 bits Signal Speed : 2,00,000 km/s Tt >= 2*Tp Length/Bandwidth >= 2* distance/Signal speed Length =2*distance *Bandwidth / Signal Speed 10000 bits = 2 * distance * 500 * 106 b/s 2 * 105 km/s 10000 bits * 2 * 105 km/s = distance = 2km 2 * 500 * 106 b/s so, answer is (B)  2km
  • 20.