Columns A[0] A[0][0] = 20 A[0][1] = 30

Similar documents
GE6151 COMPUTER PROGRAMMING REG.2013 NOTES

Lecture 6 Sorting and Searching

Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES. Sushil Kumar Saini Mo.

UNIT 7. SEARCH, SORT AND MERGE

Introduction to Arrays

Sorting & Searching. Hours: 10. Marks: 16

Questions Bank. 14) State any four advantages of using flow-chart

Types of Data Structures

International Journal Of Engineering Research & Management Technology

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

To store the total marks of 100 students an array will be declared as follows,

Write a C program using arrays and structure

Long Questions. 7. How does union help in storing the values? How it differs from structure?

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Subject: Computer Science

Unit 3 Decision making, Looping and Arrays

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Multiple Choice Questions ( 1 mark)

F.E. Sem. II. Structured Programming Approach

Problem: Read 10 numbers from the keyboard and store them

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

EENG 212 Lab 2. Recursive Functions

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Objectives of This Chapter

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Arrays, Vectors Searching, Sorting

Structured programming

Principles of Programming. Chapter 6: Arrays

Government Polytechnic Muzaffarpur.

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Sorting. Task Description. Selection Sort. Should we worry about speed?

Quick Sort. CSE Data Structures May 15, 2002

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

(i) Describe in detail about the classification of computers with their features and limitations(10)

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Question Bank (SPA SEM II)

Initialisation of an array is the process of assigning initial values. Typically declaration and initialisation are combined.

Computer Science & Engineering 150A Problem Solving Using Computers

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

UNIT III ARRAYS AND STRINGS

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

'C' Programming Language

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

List of Practical for Class XII Computer Science

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

One Dimension Arrays 1

Matrices. Jordi Cortadella Department of Computer Science

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

ARRAYS(II Unit Part II)

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

Programming for Engineers Arrays

Functions. Arash Rafiey. September 26, 2017

Data Structures and Algorithms for Engineers

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

UNIT I : OVERVIEW OF COMPUTERS AND C-PROGRAMMING

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

C-LANGUAGE CURRICULAM

PERFORMANCE OF VARIOUS SORTING AND SEARCHING ALGORITHMS Aarushi Madan Aarusi Tuteja Bharti

Unit-2 Divide and conquer 2016

Downloaded from

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Logical Coding, algorithms and Data Structures

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Arrays and Applications

Sorting. Bringing Order to the World

Q 1. Attempt any TEN of the following:

Cpt S 122 Data Structures. Sorting

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

O(n): printing a list of n items to the screen, looking at each item once.

CS256 Applied Theory of Computation

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Chapter 10 - Notes Applications of Arrays

Code No: R Set No. 1

PDS Class Test 2. Room Sections No of students

LECTURE 08 SEARCHING AND SORTING ARRAYS

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Introduction to string

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Computers Programming Course 11. Iulian Năstac

Searching an Array: Linear and Binary Search. 21 July 2009 Programming and Data Structure 1

String can be represented as a single-dimensional character type array. Declaration of strings

CS 261 Data Structures. Big-Oh Analysis: A Review

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Transcription:

UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always start from 0 It is represented using [ ] square brackets. Write a program to find sum of array elements. int i, n,sum, a[0]; printf( enter how many numbers ); printf( enter numbers one by one: ); for( i=0; i<n; i++) sum = sum + a[i]; printf( answer = %d, sum); Enter how many numbers Enter numbers one by one 0 0 0 0 0 0. List out any six string handling functions. Strcat() to concatenate a string (join) Strupr() to change to Upper case (Capital letters) Strlwr() to change to Lower case ( Small letters) Strlen() to find length of string Strrev() to reverse a string Strcmp() to compare two strings Strcpy() to copy one string to another string. What is a two-dimensional array? It is an array in which rows and columns are the two dimensions Two square brackets are used for rows and columns More number of elements can be stored Ex:- Int a[][] = 0,0, 0,0 ;- Rows / A[0] A[] Columns A[0] A[0][0] = 0 A[0][] = 0 A[] A[][0] = 0 A[][] = 0. What is a string? A string is a one dimensional array to store characters It is represented inside double quotes It is also called as sequence of characters Array is used to store the string. Ex:- char a[0] = Mechanical ; M e c h a n i c a l A[0] A[] A[] A[] A[] A[] A[] A[] A[] A[]

Part B ( and mark questions). Write a C program to arrange numbers in ascending order and descending order. Ascending order:- int i, j, temp; int a[0] =,,,,,,,,, 0 ; for( i=0; i<0; i++) for(j=i+; j<0; j++) if ( a[i] > a[j] ) temp = a[i]; a[i] = a[j]; a[j] = temp; printf( answer= ); for( i=0; i<0; i++) printf( %d, a[i]); Descending order:- int i, j, temp; int a[0] =,,,,,,,,, 0 ; for( i=0; i<0; i++) for(j=i+; j<0; j++) if ( a[i] < a[j] ) temp = a[i]; a[i] = a[j]; a[j] = temp; printf( answer= ); for( i=0; i<0; i++) printf( %d, a[i]); 0 0. Write a C program for addition of two matrices int i, j; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) scanf( %d,&b[i][j]); for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] + b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X B in X 0

. Write a C program for subtraction of two matrices int i, j; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) scanf( %d,&b[i][j]); for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] - b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X 0 B in X.Write a program for multiplication of two matrices int i, j, k; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) for(k=0; k<=; k++) c[i][j] = c[i][j] + a[i][j] * b[i][j]; for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] - b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X B in X 0 0 0

. Write a C program for transpose of a matrix int i, j; int a[][]; printf( enter matrix A in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( answer = ); for( j=0; j<; i++) for(i=0; i<; j++) printf( %d,a[i][j]); in X. Write a program to find sum of diagonal elements of a matrix int i, j, sum; int a[][]; printf( enter matrix in x ); for( i=0; i<; i++) for(j=0; j<; j++) for( i=0; i<; i++) for(j=0; j<; j++) if(i==j) sum = sum + a[i][j]; printf( answer = %d, sum); in X. Write a C program to find determinant of a matrix int i, j, det; int a[][]; printf( enter matrix in x ); for( i=0; i<; i++) for(j=0; j<; j++) det = a[0][0] * a[][] - a[][0] * a[0][]; printf( answer = %d, det); Enter x matrix

.Explain some string handling functions with example C programs. Strcat() to concatenate two strings (join) mechengg char s[0] = mech ; char s[0] = engg ; strcat(s,s); printf( %s,s); Strlen() to find length of a string 0 int a; char s[0]= mechanical ; a=strlen(a); printf( %d,a); Strcpy() to copy one string to another string mechanical char s[0]= mechanical ; char s[0]= ; strcpy(s,s); printf( %s,s); strrev() to reverse a string lacinahcem char s[0]= mechanical ; printf( %s,strrev(s) ); strupr() to change letters to upper case (Captials) MECHANICAL char s[0]= mechanical ; printf( %s,strupr(s) ); strlwr() to change letters to lower case (small letter) mechanical char s[0]= MECHANICAL ; printf( %s,strlwr(s) ); strset() to change letters to some other letters zzzzzzzzzz char s[0]= mechanical ; strset(s,z); printf( %s,s );. Explain binary search with an example. It follows divide and conquer technique. Key means, search element. Key is compared with middle element. If key > middle, ignore first half of elements If key < middle, ignore second half of elements This process is repeated recursively. Until the element is found. Input: sorted array of elements Output: element found (or) not found. Sorted array Key Steps:- Find middle element of array Compare it with key Binary search Element Found Element not found If middle < key, repeat steps and for st half of array If middle > key, repeat steps and for nd half of array if middle == key, element is found.

Sorted array: 0, 0, 0, 0, 0, 0 Key : 0 Here, left = 0, right = 0, mid = (0 + 0) / = Therefore, mid = 0 or 0. We take mid = 0 0 = 0? (false). 0 < 0? (true) So, take second half of elements: 0, 0, 0 Sorted array: 0, 0, 0 Key : 0 Here, left = 0, right = 0, mid = (0 + 0) / = 0 0 = 0? (true) Therefore, element found at th position. Program:- int a[0], i, n, key, c, left, right, mid; printf( enter the sorted array elements ); printf( enter the key ); scanf( %d,&key); right = n-; while(left<=right) mid = (left+right)/; if(key==a[mid] c=; else if(key < a[mid]) right = mid -; else left = mid +; if ( c ==0 ) printf( element not found ); else printf( element found ); Enter number of elements Enter sorted array 0 0 0 0 0 0 Enter key 0 Element found Advantages Efficient and faster Uses divide and conquer Suitable for large elements Disadvantages Not suitable for unsorted array Not suitable for dynamically changing data 0. Explain linear search with an example. The process of finding the key from the collection. Compare all the elements one by one. No technique is used Input: unsorted array of elements Output: element found (or) not found unsorted array key linear search element found element not found Steps:- Read the first element Compare with key If true, element is found. If not true, compare next element. Continue step,, and till the last element. Unsorted array:,,,,,, 0 Key : 0? (false). Go to next 0? (false). Go to next 0? (false). Go to next 0? (false). Go to next 0? (true) element found Program:- int a[0], key, c, i; printf( enter the sorted array elements ); printf( enter the key ); scanf( %d,&key);

if(a[i] = = key) c = ; break; if( c ==0) printf( element not found ); else printf( element found ); Enter number of elements Enter sorted array 0 Enter key Element found Advantages Easy and faster Array can be unsorted Suitable for small number of elements Disadvantages Not suitable for large number of elements Slower method Very basic technique. Explain bubble sort with an example It the oldest and easiest sorting. In this sorting, the elements are arranged in some technique. Take first element, compare it with next element. If first > second, swap the elements. Then, again compare st element with next element Continue this process until, each element is compared with every other element. Unsorted array Bubble sort Sorted array Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) for(i=n-; i>0; i--) for(j=0; j<=i; j++) if(a[j]>a[j+]) temp = a[j]; a[j] = a[j+]; a[j+]= temp; printf( answer= ); printf( %d,a[i]); Enter number of elements Enter elements Advantages Easy technique simple Suitable for small number of elements Disadvantages Not suitable for large number of elements Not efficient Very basic technique

. Explain insertion sort with example Insertion sort is the process of taking elements from the list one by one and inserting them at correct position in the array. It is the simplest sorting. Unsorted array Sorted array Steps:- Take first element in the list Then take nd element in the list Check if it is less than st Insert at correct position Then take next element, insert at correct position Repeat this process till all elements are sorted. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) For(i=0; i<s; i++) Temp = a[i]; J = i=; While( ( temp < a[j] ) && (j>=0) ) A[j+] = a[j]; J= j-; A[j+]=temp; printf( answer= ); printf( %d,a[i]); Insertion sort Enter number of elements Enter elements 0 0 0 0 0 Answer=0 0 0 0 0 Advantages Disadvantages Easy technique Not suitable for large number of elements Simple and stable method expensive Suitable for small number of elements. Explain selection sort with example It is one of the basic sorting technique. It is used to sort the elements in ascending order It is based on comparison and swapping. Take the first element and compare it with all the other elements. The elements are swapped if first one is greater than the other element. Unsorted array Steps:- selection sort sorted array Take first element Compare it with next element If st < nd, compare st element with rd element If st > nd element, swap them. Continue this process till all the elements are in ascending order.

Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) for(j=i+; j<n; j++) if(a[i] > a[j]) temp = a[i]; a[i]=a[j]; a[j]=temp; printf( answer= ); printf( %d,a[i]); Advantages Easy technique Simple method Suitable for small number of elements Disadvantages Not suitable for large number of elements Slow process. Explain merge sort with example It uses divide and conquer technique Any number of elements can be sorted The unsorted array is divided into smaller pieces. Divided until single element. Then it is merged (joined) in the increasing order. Both dividing and merging is done recursively. Unsorted array Enter number of elements Enter elements Merge sort Answer= Sorted array Program:- Refer book page number:.. Differentiate entry checked and exit checked conditional constructs with an example. (or) Differentiale WHILE loop and Do..WHILE loop constructs with an example. WHILE loop Entry checked loop Top-tested loop Condition is checked at the entry Loop will not execute if condition is FALSE Execute loop until condition is satisfied While(condition) Body of the loop int a=0; while(a<) printf( %d,a); a++; 0

Do..While loop:- Exit checked loop Bottom tested loop Condition is checked at the end If condition is false, loop will execute one time Execute the loop until the condition is true. Do Body of the loop; while(condition); Example program:- int a=0; do printf( %d,a); a++; while(a<);. Write a C program to generate Fibbonacci series int n, fib, f, f, i; for(i=; i<=n;i++) fib = f + f; printf( %d,fib); f = f; f = fib; 0 Enter number of elements 0