1) The document discusses arrays in C programming, including what arrays are, how to declare and initialize one-dimensional arrays, and how to perform common operations like reading, writing, summing, and finding largest elements in arrays.
2) Examples are provided to demonstrate how to write programs to read and display arrays, calculate sums of array elements, and determine other properties of arrays.
3) Key concepts covered include declaring arrays, initializing arrays, accessing array elements, looping through arrays, and performing calculations using the elements of arrays.
C programming, learn array 2020 week 5 and week 6, Students should know how to define/declare, initialize arrays, and multidimensional arrays types. so they can apply this knowledge during the implementation of software applications.
C programming, learn array 2020 week 5 and week 6, Students should know how to define/declare, initialize arrays, and multidimensional arrays types. so they can apply this knowledge during the implementation of software applications.
Kickstart your data science journey with this Python cheat sheet that contains code examples for strings, lists, importing libraries and NumPy arrays.
Find more cheat sheets and learn data science with Python at www.datacamp.com.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
C Programming : Arrays, One Dimensional Arrays, Two Dimensional Arrays, Three Dimensional Arrays, Operations on Arrays like Insertion, Deletion, Searching, Sorting, Merging, Traversing, Matrix Manipulation like Addition, Multiplication etc. : Visit us at : www.rozyph.com
Kickstart your data science journey with this Python cheat sheet that contains code examples for strings, lists, importing libraries and NumPy arrays.
Find more cheat sheets and learn data science with Python at www.datacamp.com.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
C Programming : Arrays, One Dimensional Arrays, Two Dimensional Arrays, Three Dimensional Arrays, Operations on Arrays like Insertion, Deletion, Searching, Sorting, Merging, Traversing, Matrix Manipulation like Addition, Multiplication etc. : Visit us at : www.rozyph.com
At the end of this lecture students should be able to;
Describe the C arrays.
Practice the declaration, initialization and access linear arrays.
Practice the declaration, initialization and access two dimensional arrays.
Apply taught concepts for writing programs.
Other than some generic containers like list, Python in its definition can also handle containers with specified data types. Array can be handled in python by module named “array“. They can be useful when we have to manipulate only a specific data type values.
Automobile Management System Project Report.pdfKamal Acharya
The proposed project is developed to manage the automobile in the automobile dealer company. The main module in this project is login, automobile management, customer management, sales, complaints and reports. The first module is the login. The automobile showroom owner should login to the project for usage. The username and password are verified and if it is correct, next form opens. If the username and password are not correct, it shows the error message.
When a customer search for a automobile, if the automobile is available, they will be taken to a page that shows the details of the automobile including automobile name, automobile ID, quantity, price etc. “Automobile Management System” is useful for maintaining automobiles, customers effectively and hence helps for establishing good relation between customer and automobile organization. It contains various customized modules for effectively maintaining automobiles and stock information accurately and safely.
When the automobile is sold to the customer, stock will be reduced automatically. When a new purchase is made, stock will be increased automatically. While selecting automobiles for sale, the proposed software will automatically check for total number of available stock of that particular item, if the total stock of that particular item is less than 5, software will notify the user to purchase the particular item.
Also when the user tries to sale items which are not in stock, the system will prompt the user that the stock is not enough. Customers of this system can search for a automobile; can purchase a automobile easily by selecting fast. On the other hand the stock of automobiles can be maintained perfectly by the automobile shop manager overcoming the drawbacks of existing system.
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...Amil Baba Dawood bangali
Contact with Dawood Bhai Just call on +92322-6382012 and we'll help you. We'll solve all your problems within 12 to 24 hours and with 101% guarantee and with astrology systematic. If you want to take any personal or professional advice then also you can call us on +92322-6382012 , ONLINE LOVE PROBLEM & Other all types of Daily Life Problem's.Then CALL or WHATSAPP us on +92322-6382012 and Get all these problems solutions here by Amil Baba DAWOOD BANGALI
#vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore#blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #blackmagicforlove #blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #Amilbabainuk #amilbabainspain #amilbabaindubai #Amilbabainnorway #amilbabainkrachi #amilbabainlahore #amilbabaingujranwalan #amilbabainislamabad
Courier management system project report.pdfKamal Acharya
It is now-a-days very important for the people to send or receive articles like imported furniture, electronic items, gifts, business goods and the like. People depend vastly on different transport systems which mostly use the manual way of receiving and delivering the articles. There is no way to track the articles till they are received and there is no way to let the customer know what happened in transit, once he booked some articles. In such a situation, we need a system which completely computerizes the cargo activities including time to time tracking of the articles sent. This need is fulfilled by Courier Management System software which is online software for the cargo management people that enables them to receive the goods from a source and send them to a required destination and track their status from time to time.
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
Event Management System Vb Net Project Report.pdfKamal Acharya
In present era, the scopes of information technology growing with a very fast .We do not see any are untouched from this industry. The scope of information technology has become wider includes: Business and industry. Household Business, Communication, Education, Entertainment, Science, Medicine, Engineering, Distance Learning, Weather Forecasting. Carrier Searching and so on.
My project named “Event Management System” is software that store and maintained all events coordinated in college. It also helpful to print related reports. My project will help to record the events coordinated by faculties with their Name, Event subject, date & details in an efficient & effective ways.
In my system we have to make a system by which a user can record all events coordinated by a particular faculty. In our proposed system some more featured are added which differs it from the existing system such as security.
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
Presented at NUS: Fuzzing and Software Security Summer School 2024
This keynote talks about the democratization of fuzzing at scale, highlighting the collaboration between open source communities, academia, and industry to advance the field of fuzzing. It delves into the history of fuzzing, the development of scalable fuzzing platforms, and the empowerment of community-driven research. The talk will further discuss recent advancements leveraging AI/ML and offer insights into the future evolution of the fuzzing landscape.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Cosmetic shop management system project report.pdf
Array notes
1. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
1
Unit-4
Arrays
Why we have to go for arrays:
To store 100 integer items, we want 100 integer variables to store, i.e int a1, a2, a3,
…………….., a100;
To store 1000 integer items, we want 1000 integer variables to store, i.e int a1, a2,
a3,…………………, a1000;
Using arrays int a[10]; //10 integer items
int a[1000]; // 1000 integer items
int a[100000]; // 100000 integer items
What is Array?
Array is a collection of similar kind of data items.
i.e int a[10]; // a is an array, we can store 10 integer items
i.e float b[10]; // b is an array, we can store 10 floating items
Note: In array we can store only similar kind of data items, but we can’t store dissimilar kind
of data items.
Arrays has been classified into two types, they are
1) One dimensional array(1D)
2) Two dimensional array(2D)
One dimensional array(1D) – one dimensional array(1D) is a linear list which stores array
elements continuous one after the other in the memory.
How to declare 1D array
datatype array_name[size];
int a[10]; // a is an array which stores 10 integer data items
float b[100]; //b is an array which stores 100 floating data items
long int c[10]; // c is an array which stores 10 long integer items
How to find the size of an array
size of an array is calculated as = size * type of data storing in array
int a[4];
size of this array is = 4*2 i.e size of an array is 4 and type of data storing in array is int
2. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
2
int a[4];
2 bytes 2bytes 2 bytes 2 bytes
a[0] a[1] a[2] a[3]
this array size is 4*2=8
float b[5];
4 bytes 4 bytes 4 bytes 4bytes 4 bytes
b[0] b[1] b[2] b[3] b[4]
so, this array size is = 5*4= 20 bytes
char d[6]={‘a’,’b’,’c’,’d’,’e’};
1 byte 1 byte 1 byte 1byte 1byte 1byte
b[0] b[1] b[2] b[3] b[4] b[5]
so, this array size is = 6*1= 6 bytes
How to initialize 1D array
datatype array_name[size] = {v1,v2,v3,………………….,vn};
i.e v1,v2,v3,…………..,vn are the values stored in the array
Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
float b[4]={2.0, 3.4, 1.6, 7.8};
2.0 3.4 1.6 7.8
a[0] a[1] a[2] a[3]
char d[5]={‘B’,’E’,’F’,’G’,’H’};
B E F G H
a[0] a[1] a[2] a[3] a[4]
Different ways of initializing 1D array
1) Initializing all specified memory locations
2) Partial array initialization
3) Array initialization without size
4) String initialization
3. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
3
Initialization all specified memory locations
In this array, all the memory locations are filled with values.
int a[5]={10,20,30,40,50}; //array a with five elements and the elements are 10,20,30,40,50
are stored in all the five memory locations
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
float b[4]={3.1, 2.6, 7.9, 4.5}; //array a with 4 elements and the elements are 3.1, 2.6, 7.9,
4.5 are stored in all the four memory locations
Partial array initialization
In this array, partial filling of elements in an array.
int a[5]={10,20}; //in this array a, we can store five
elements but here we are storing two elements and the remaining spaces are filled with
zero’s. In this array we can store maximum five elements.
10 20 0 0 0
a[0] a[1] a[2] a[3] a[4]
Array initialization without size
We can declare an array without size
Example1 :
int a[ ]={10,20,30}; //in this array a, size is not specified that time it will see the number of
elements(i.e number of elements are 3 and data to be stored in an array will be int
so, size will be 3*2=6 bytes. So the data stores in the memory as shown below
10 20 30
a[0] a[1] a[2]
Example2:
float b[ ]={3.1, 2.6, 7.9, 4.5}; // in this array b, size is not specified that time it will see the
number of elements(i.e number of elements are 4 and data to be stored in an array will be of
float.
so, size will be 4*4=6 bytes. So the data stores in the memory as shown below
3.1 2.6 7.9 4.5
b[0] b[1] b[2] b[3]
4. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
4
so, array b with 4 elements and the elements are 3.1, 2.6, 7.9, 4.5 are stored in all the four
memory locations
String initialization
Initialize the array with a string(i.e array of characters is called string)
char e[10]=”computer”; //here string computer stores in the string variable e
C o M P u t E r 0 0
e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9]
char d[10]=”abc def”;
d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9]
How To Read An Array Elements
int a[5]; //declare an array a with five elements of type ineteger….but how to read an array
elements from the user or keyboard
int a[5];
a[0] a[1] a[2] a[3] a[4]
note: once we declare an array, memory will be allocated how to read array elements
int a[5];
int i;
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
Note: array initialization starts from zero position in the memory.
How to display array elements
int a[5]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
{
A b C d e F 0 0 0
5. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
5
printf(“%d “,a[i]);
}
Output:
10 20 30 40 50
How to read and display array of five integer elements
#include<stdio.h>
void main()
{
int a[5];
int i;
printf(“read array of five integer numbers”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<5;i++)
{
printf(“%d “,a[i]);
}
1) Write a program to read and display array of 10 floating point numbers
#include<stdio.h>
void main( )
{
float a[10];
int i;
printf(“read array of 10 floating point numbers”);
for(i=0;i<10;i++)
Input:
Read array of five integer numbers
10 20 30 40 50
Output:
Display array elements
10 20 30 40 50
6. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
6
{
scanf(“%f”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<10;i++)
{
printf(“%f “,a[i]);
}
}
Input :
read array of 10 floating point numbers
2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34
Output:
display array elements are
2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34
2) Write a program to read and display array of 8 very long integer numbers.
#include<stdio.h>
void main( )
{
long int a[8];
int i;
printf(“read array of 8 very long integer numbers”);
for(i=0;i<8;i++)
{
scanf(“%ld”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<8;i++)
7. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
7
{
Printf(“%ld”,a[i]);
}
Output:
read array of 8 very long integer numbers
222 45345 56756 666678 789999 34567834 555674567 786786567
display array elements
222 45345 56756 666678 789999 34567834 555674567 786786567
3) Write a program to read and display an array of ‘N’ integer numbers
#include<stdio.h>
void main( )
{
int a[100];
int i,n;
printf(“enter size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
}
Output:
8. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
8
enter size of array
4
Enter array elements
10 20 30 40
display array elements
10 20 30 40
4) Write a C program to find the sum of an array of 5 integer numbers.
#include<stdio.h>
void main( )
{
int a[5];
int i,sum;
printf(“enter all five integer elements”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<5;i++)
{
sum=sum+i;
}
printf(“sum=%d”,sum);
}
Output:
Enter all five integer elements
1 2 3 4 5
Sum=15
9. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
9
5) Write a C program to find the sum of an array of N integer numbers.
#include<stdio.h>
void main( )
{
int a[100];
int i,n, sum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter all integer elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+i;
}
printf(“sum=%d”,sum);
}
Input:
enter the size of an array
4
enter all integer elements
1 2 3 4
Sum=10
10. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
10
6) Write a C program to find the first largest element in array of N integer numbers.
#include<stdio.h>
Void main()
{
inta[100],big;
int i,n;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“read array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
big=a[0];
for(i=0;i<n;i++)
{
If(a[i]>big)
{
big=a[i];
}
}
printf(“biggest element is=%d”,big);
}
Input:
enter the size of an array
4
read array elements
1 2 5 3
Output:
11. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
11
biggest element is = 5
7) Write a C program to find the sum of even numbers and the sum of odd numbers
from an array of N integer numbers.
#include<stdio.h>
void main( )
{
int a[100];
int i,n, esum,osum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter all integer elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
esum=0;
osum=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
printf(“esum=%d”,esum);
printf(“osum=%d”,osum);
}
12. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
12
8) Write a C program to read array A with 10 elements and array B with 10 elements
and find C array, such that C will be given below,
C[0]=A[0]+B[9];
C[1]=A[1]+B[8];
---------------------
---------------------
C[9]=A[9]+B[0];
#include<stdio.h>
void main( )
{
int A[10],B[10],C[10];
int i;
printf(“enter array A elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&A[i]);
}
printf(“enter array B elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&B[i]);
}
for(i=0;i<10;i++)
{
C[i]=C[i-1]+C[i-2];
}
printf(“C array will be ………………..”);
for(i=0;i<10;i++)
{
printf(“%d “,C[i]);
}
13. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
13
}
9) Write a C program to print ‘N’ Fibonacci numbers using arrays.
Note : Fibonacci numbers are 0,1,1,2,3,5,8,13,………………….,N
#include<stdio.h>
Void main()
{
int a[100];
int i,n;
printf(“enter the n value”);
scanf(“%d”,&n);
a[0]=0; //first Fibonacci numbers
a[1]=1; // second Fibonacci numbers
for(i=2;i<n;i++)
{
a[i]= a[i-1]+a[i-2];
}
printf(“Fibonacci numbers are………..”);
for(i=0;i<n;i++)
{
printf(“%d “,a[i]);
}
}
10. Write a C program to find the mean, varience and deviation for an array of N integer data.
Mean is the sum of all the elements by number of elements i.e mean= ∑𝑖=0
𝑛−1
Xi/ n
Varience= ∑𝑖=0
𝑛−1
(𝑋𝑖 − 𝑚𝑒𝑎𝑛)2
n
Deviation=√𝑣𝑎𝑟𝑖𝑒𝑛𝑐𝑒
#include<stdio.h>
14. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
14
Void main( )
{
int x[100],n,i;
float mean, varience, deviation,sum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”, &x[i]);
}
Sum=0;
for(i=0;i<n;i++)
{
sum=sum + x[i];
}
mean=sum/n;
printf(“mean=%f”,mean);
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(𝑋𝑖 − 𝑚𝑒𝑎𝑛) * (𝑋𝑖 − 𝑚𝑒𝑎𝑛) ;
}
varience=sum/n;
deviation=sqrt(varience);
printf(“varience=%f”,varience);
printf(“deviation=%f”,deviation);
}
15. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
15
Searching techniques in C language
How to search an element in an array, whether an element is found in an array or not using
following techniques
• Linear Search
• Binary Search
Sorting Technique in C language
How to sort array elements in ascending order or descender order using the following
techniques
• bubble sort
• selection sort
Searching technique – Linear search
In linear search, searching an key element from the first position element, compare the first
position element with key element, if the first position element is matches with key element
then we can display search is successful and stop searching.
If the first position element is not matching with the key element, then compare with the
second position element with key element, if it is matches then display search is successful
and stop the searching. Once again second position element is not matching with key element
then comparison start with third element, this process will repeat until the end of the element
in an array.
#include<stdio.h>
Void main()
{
int a[100],n,key,I;
printf(“enter the size of the array”);
scanf(“%d”,&n;
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
16. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
16
printf(“enter the key element to be search”);
scanf(“%d”,&key);
//comparison from first element with key element
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf(“item is found”);
exit(0);
}
}
printf(“item is not found”);
}
Input1:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
20
Item is found
Input2:
Enter the size of an array
4
Enter array elements
10 20 40 30
17. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
17
Enter key element
70
Item is not found
Important Note: In the linear searching, input array elements to an array either in sorted
order or un-sorted order
Advantages in linear search
1. In linear search, read array elements in sorted order or un-sorted order.
Disadvantage in linear search
1. In linear search, number of comparison is more.
Binary Search
how the searching will be done in binary search????
In binary search, first we have to read array elements(i.e always in sorted order(i.e in
ascending or descending order)). Once we read array elements, first we have to find the mid
position element, then compare mid position element with key element, if mid position
element is matches with key element then we display item is found and stop searching the
element in an array. But if the mid position element is not matches with key element then we
verify whether key element is greater than the mid or the less than the mid.
If the key element is less the mid element, searching will be done in the left side otherwise
searching will be done in the right part.
So, In binary search, number of comparison is less and is one of the most efficient algorithm
#include<stdio.h>
Void main()
{
int a[100],n ,mid ,key, low, high, i;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
18. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
18
scanf(“%d”,&a[i]);
}
printf(“enter key to be search”);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf(“item is found “);
exit(0);
}
If(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf(“item is not found”);
}
Input1:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
20
Item is found
19. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
19
Input2:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
60
Item is not found
Sorting techniques
Bubble sort
Selection sort
Bubble sort : how to sort the array elements in ascending or descending order
How the sorting will be done like this
In the first pass we will compare first element with second element, if the first position
element in the array is greater than second position element in the array, swap these two
elements in the array otherwise don’t swap, next compare second position elements with third
position element, if the second position element is greater than third position element, swap
those two elements otherwise don’t swap. This will repeat until the first ascending order
element is found . In the second pass, start compare the first position element with the second
position element, if the first position element is greater than second position element , swap
those two numbers otherwise don’t swap, this will repeat. Some sorting will take two passes
or some sorting will take three passes, number of passes will be depend on the complexity of
the problem.
#include<stdio.h>
Void main()
{
int a[100], n, i, temp;
printf(“enter the size of the array”);
scanf(“%d”,&n);
printf(“read array elements”);
for(i=0;i<n;i++)
20. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
20
{
scanf(“%d”,&a[i]);
}
for(j=1; j<n ; j++) //first for loop for the number of passes
{
for(i=0; i<n-j; i++) //comparison starts from the first position element
{
if(a[i]>a[i+1]) //compare first position element with second
{ position element
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf(“sorted elements are……”);
for(i=0;i<n;i++)
{
printf(“%d “,a[i]);
}
}
Input1:
Enter the size of an array
4
Enter array elements
50 20 40 30
The sorted elements are…..
20 30 40 50
21. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
21
Selection Sorting Technique
In the selection sorting, how the sorting will be done ….first search the position where the
first sorted element is there in the array, once we search the first position element, we have to
placed in the first position similarly search the second position element in the array and place
the second position element in the second place, repeat the process until all the elements are
sorted in the proper order.
#include<stdio.h>
Void main( )
{
int a[100],n,i,,pos,temp;
printf(“enter the number of elements”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
If(a[j]>a[pos])
pos=j;
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
printf(“sorted elements are……”);
for(i=0;i<n;i++)
22. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
22
{
printf(“%d “,a[i]);
}
}
Input1:
Enter the size of an array
4
Enter array elements
50 20 40 30
The sorted elements are…..
20 30 40 50
Note: first we have to know how to read N names(or strings) or array of names.
Char a[5][10];
Number of names(or strings)
Length of the names
Size of an array = 5*10*1=50
0 1 2 3 4 5 6 7 8 9
R A M A 0
N I T H I N 0
K U M A R 0
A S H A 0
K A V A N A 0
In this manner array of names will be stored in the memory
How to read array of names is as follows:
Char a[5][10];
int i;
a[0]
a[1]
a[2]
a[3]
a[4]
23. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
23
printf(“read array of names”);
for(i=0;i<5;i++)
{
scanf(“%s”,a[i]);
}
How to display array of names
printf(“read array of names”);
for(i=0;i<5;i++)
{
printf(“%s n”,a[i]);
}
1. Write a C program to read N names and sort the names using bubble sort technique.
#include<stdio.h>
#include<string.h>
void main( )
{
char a[10][30],temp[30];
int n,i;
printf(“enter the N names”);
scanf(“%d”,&n);
printf(“read names”);
for(i=0;i<n;i++)
{
scanf(“%s”, a[i]);
}
//sort the names using bubble sort
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(strcmp(a[i],a[i+1])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[i+1]);
strcpy(a[i+1],temp);
}
}
24. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
24
}
printf(“sorted names are………..”);
for(i=0;i<n;i++)
{
printf(“%sn”, a[i]);
}
}
Note: strcmp() and strcpy() are the string handling functions and these two functions are
present in <string.h>
Two -Dimensional array(2D)
2D array is a array with two subscripts is called 2D array.
How to declare 2D array
datatype arr_name[exp1][exp2];
exp1->number of rows
exp2->number of columns
int a[2][2]; //construct a matrix a with 2 rows and 2 columns
int b[3][4]; //construct a matrix b with 3 rows and 4 columns
how to find the size of the 2D array
int a[2][2];
size of the array a or size of the matrix a is= rows*columns*type of data
= 2*2*2
= 8 bytes
How was structure in the memory
how to initialize the 2D array
datatype arr_name[exp1][exp2]={{a1,a2,a3,……,an},
a[0][0] a[0][1]
a[1][0] a[1][1]
Row0
Row1
0 1
2 bytes
2 bytes
2 bytes
2 bytes
25. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
25
{b1,b2,b3,……,bn},
{c1,c2,c3,………..,cn},
-------------------------
{z1,z2,z3,…………,zn}};
Example:
int a[2][2]={{1,2},
{3,4}};
How it will be stored in the memory as given below
0 1
1 2
3 4
int a[3][4]={{10,20,30,40},
{50,60,70,80},
{90,100,110,120}};
0 1 2 3
10 20 30 40
50 60 70 80
90 100 110 120
Different ways of initializing 2D array
1. Initializing all specified memory location
2. Partial array initialization
Initializing all specified memory locations
Example:
int a[3][4]={{5,20,3,4},
{50,60,90,80},
{1, 2, 7, 8} } ;
Row 0
Row 1
Row 0
Row 1
Row 2
26. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
26
0 1 2 3
5 20 3 4
50 60 90 80
1 2 7 8
Partial array initialization
Example:
int a[3][3]={{10,20},
{30},
{40,50}};
How the data will be stored in the memory as given below in the table
0 1 2
10 20 0
30 0 0
40 50 0
How to read the matrix elements
int a[2][2]; i.e 2-rows and 2-columns
size of the array a is =2*2*2=8 bytes
coll 0 coll1
for(i=0;i<2;i++) // i for rows
{
for(j=0;j<2;j++) //j for columns
{
scanf(“%d”,&a[i][j]);
Row 0
Row 1
Row 2
Row 0
Row 1
Row 2
Row 0
Row 1
2 bytes
2 bytes 2 bytes
2 bytes
27. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
27
}
}
How to display matrix elements
for(i=0;i<2;i++) // i for rows
{
for(j=0;j<2;j++) //j for columns
{
printf(“%d ”,a[i][j]);
}
Printf(“n”);
}
How to read and display matrix elements(i.e read 2X2 matrix)
#include<stdio.h>
Void main( )
{
Int a[2][2],i,j;
printf(“read matrix a elements”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix a elements are……..”);
for(i=0;i<2;i++)
{
1 2
2 3
Read matrix a elements
matrix a elements are……
28. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
28
for(j=0;j<2;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
}
}
How to read and display matrix whose order is 4X4
#include<stdio.h>
Void main( )
{
int a[4][4],i,j;
printf(“read matrix a elements”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix a elements are……..”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
1 2
2 3
1 2 3 4
5 6 7 8
9 10 11 12
Read matrix a elements
matrix a elements are……
1 2 3 4
5 6 7 8
9 10 11 12
29. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
29
}
}
1. Write a C program to read and display a matrix whose order is mXn
#include<stdio.h>
void main( )
{
int a[10][10], i, j,m, n;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix elements are….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”)
}}
enter the order(mXn) of the matrix
2 2
Read matrix a elements
30. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
30
3 4
4 6
matrix elements are……
3 4
4 6
2. Write a C program by reading matrix and find the sum of all the elements in a
matrix.
#include<stdio.h>
void main( )
{
int a[10][10], m, i, j, n,sum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
31. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
31
printf(“sum=%d”,sum);
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 4
2 6
Sum=13
3. Write a C program by reading a matrix and find the first largest element in that
matrix
#include<stdio.h>
void main( )
{
int a[10][10], m, n,big,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
big=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
32. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
32
{
If(a[i][j]>big)
{
big=a[i][j];
}
}
}
printf(“the biggest element is=%d”,big);
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
5 9
The biggest element is = 9
4. Write a C program by reading a matrix and find its transpose matrix
Note: Transpose matrix is interchanging of rows and columns
#include<stdio.h>
void main( )
{
int a[10][10], m, n,big,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
33. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
33
{
scanf(“%d”,&a[i][j]);
}
}
printf(“ transpose matrix is……….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d “, a[j][i]);
}
printf(“n”);
}
enter the order(mXn) of the matrix
1 2
Read matrix a elements
3 4
5 6
Transpose matrix is……..
3 5
4 6
5. Write a C program by reading a matrix and find sum of each row
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
34. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
34
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf(“sum of %d row= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 row is = 3
Sum of 2 row is = 7
6. Write a C program by reading a matrix and find the sum of each column
#include<stdio.h>
void main( )
35. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
35
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[j][i];
}
printf(“sum of %d row= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 column is = 4
36. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
36
Sum of 2 column is = 6
7. Write a C program by reading a matrix and find sum of each row and sum of
each column
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
// sum of each row
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf(“sum of %d row= %d”,i+1,sum);
}
//sum of each column
37. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
37
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[j][i];
}
printf(“sum of %d column= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 row is = 3
Sum of 2 row is = 7
Sum of 1 column = 4
Sum of 2 column = 6
8. Write a C program by reading a matrix and find the trace of a matrix
Note:trace of a matrix is the sum of the principal diagonal elements
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
38. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
38
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
sum=sum+a[i][i];
}
printf(“trace of the matrix is=%d”,sum);
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
trace of the matrix is = 5
9. Write a C program by reading a matrix and find the norm of the matrix
(Note: Norm of a matrix is the square root of the sum of the squares of all the elements
in a matrix)
#include<stdio.h>
void main( )
{
int a[10][10], m, n, sum,i,j;
39. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
39
float nsum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j]*a[i][j];
}
}
nsum=sqrt(sum);
printf(“nsum = %f ”, nsum);
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
nsum = 5.47722557
40. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
40
10. Write a C program to perform addition of two matrix
#include<stdio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10], m, n, i, j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“read matrix b elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// sum of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
41. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
41
}
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Read matrix b elements
1 2
3 4
C matrix is……….
2 4
6 8
11. Write a C program to perform subtraction of two matrix
#include<stdio.h>
void main( )
{
42. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
42
int a[10][10],b[10][10],c[10][10], m, n, i, j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“read matrix b elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// sum of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=b[i][j]-a[i][j];
}
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
43. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
43
{
for(j=0;j<n;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Read matrix b elements
7 8
9 10
C matrix is……….
6 6
6 6
12. Write a C program to perform multiplication of two matrix
#include<stdio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10], m, n, p, q ,i, j, k,sum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
44. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
44
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“enter the order(pXq) of the matrix”);
scanf(“%d%d”,&p,&q);
printf(“read matrix b elements”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// multiplication of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<q ; j++)
{
sum=0;
for(k=0;k<n ; k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
45. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
45
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
{
for(j=0;j<q ;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
enter the order(pXq) of the matrix
2 2
Read matrix b elements
1 2
3 4
C matrix is……….
7 10
15 22