Beruflich Dokumente
Kultur Dokumente
Array elements
In the above declaration array_size defines the number of elements in an array. Each
element of array can be accessed by using a subscript or index of the element.
Example: Declare an array of 5 elements to store age of poeple which are type int.
int age[5];
The following are the way they would be stored in the memory.
Exercise 1: Declare an array of 100 elements to store marks of students which are type
float.
float marks[100];
array by
You can use array members from age[0] to age[9]. But, what if you want to use
element age[10],age[13] or aage[-1]etc. Compiler may not show error using these elements
but, they would return garbage values.
Partial Initialization:
int age[5]={2,4};
Example : Write a C program to read N integers into an array and perform the following:
a) Find the sum of negative numbers
b) Find the sum of positive numbers
c) Find the average of all numbers
#include <stdio.h>
#define MAXSIZE 10
int main()
{
int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/* Summation starts */
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("Sum of all negative numbers = %d\n",negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("Average of all input numbers = %f\n", average);
}
Exercise 3: Write C program to reverse the elements of array. (Example if array contents
are 1 2 3 4 5 after reversing contents of array should be 5 4 3 2 1).
#include<stdio.h>
main()
{
int a[10],temp,i,j,n;
printf("Enter the size of the array");
scanf("%d",&n);
printf("Enter the number of elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("Elements after reversing \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Exercise 5: Assume that you are given temperatures of all 12 months in the years 2014
and 2015 which are stored in two arrays. Write a C program to identify the months
having same temperatures.
#include<stdio.h>
main()
{
int year1[]={23,24,35,40,45,36,23,22,33,36,40,22};
int year2[]={32,24,35,34,41,36,32,20,33,38,42,22};
int i=0;
printf("The following months have same temperatures\n");
for(i=0;i<12;i++)
{
if(year1[i]==year2[i])
printf("%d\n",i+1);
}
You may be wondering how the two dimension arrays are stored in the memory in
linearly. The two dimensional array is treated as array of single dimensional array. In C
language each row is stored one after the other as shown below.
2000
a[0][0]
row 1
2004
a[0][1]
2008
a[0][2]
2012
a[0][3]
2016
a[0][4]
2020
a[0][5]
2024
a[1][0]
row2
2028
a[1][1]
2032
a[1][2]
2036
a[1][3]
2040
a[1][3]
2044
a[1][5]
2048
a[1][6]
Initialization of Multidimensional Arrays
In C, multidimensional arrays can be initialized in different number of ways.
int c[2][3]={{1,3,0}, {-1,5,9}};
OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};
Partial initialization
int c[2][3]={{1,3}, {-1,5}};
Here the first row third column will be 0 i.e c[0][2]=0 and second row third column will
be 0 i.e c[1][2]=0
Exercise 6: Declare an array of 3X3 integers and initialize the array with the values
of your choice such that the complete second row elements must be 0.
Zero initialization
int c[3][3]={0};
if we wish to print the values of the marks array shown above the following is how we
do it.
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf(%f,marks[i][j]);
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
}
}
else
}
sum=sum+a[i][j];
iii)
#include<stdio.h>
int main()
{
int i,j,a[10][10],max[10],rows,columns;
printf("\nEnter the number of Rows and columns : ");
scanf("%d%d",&rows,&columns);
for(i=0;i<rows;i++){
for(j=0;j<columns;j++)
{
printf("\nEnter the Element a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<rows;i++)
{
max[i]=a[i][0];
for(j=0;j<columns;j++)
{
if(a[i][j] > max[i])
max[i]=a[i][j];
}
}
for(i=0;i<rows;i++)
printf("Max in row %d is %d\n",i,max[i]);
return 0;
iv)
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
if(m == n){
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
for( d = 0 ; d < n ; d++ )
scanf("%d",&matrix[c][d]);
for( c = 0 ; c < m ; c++ )
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
printf("Transpose of entered matrix :-\n");
for( c = 0 ; c < n ; c++ ) {
for( d = 0 ; d < m ; d++)
printf("%d\t",transpose[c][d]);
printf("\n");
}
}
else
printf("not a square matrix\n");
return 0;
}
Exercise 7: Write a C program to store students marks in 5 courses and provide a menu
to user where he/she will enter 1,2 or 3 and based on the input the corresponding values
have to be displayed:
1. Find the highest marks for each students.
2. Find the highest mark in each course.
3. Find the average marks in each course.
#include<stdio.h>
main()
{
int a[5][5];
int rows,cols,highest=0,max=0,sum=0;
float avg=0;
printf("enter the number of students and courses");
scanf("%d%d",&rows,&cols);
int i=0,j=0;
//read the marks for each student in different courses
for(i=0;i<rows;i++)
{
printf("enter marks for %d courses for the
student",cols);
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
}
//Find the highest marks for each students.
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(a[i][j] > max)
max=a[i][j];
}
printf("maximum marks for %d student is %d\n",i,max);
max=0;
}
//Find the highest marks for each course.
for(i=0;i<cols;i++)
{
for(j=0;j<rows;j++)
{
sum+=a[j][i];
if(a[j][i] > highest)
highest=a[j][i];
}
avg=(float)sum/rows;
printf("maximum marks for %d course is %d\n",i,highest);
printf("Average marks for %d course is %f\n",i,avg);
highest=0;
sum=0;avg=0;
}
}
Example 1: If the elements of an array are {20, 34, 12, 4, 5, 6} how many
comparisons are required to find the element 4?
We will have to compare 12 with 20, 34, 12, 4 hence 4 comparisons will be required
to find the element 4.
Exercise 1: If the elements of an array are {20, 4, 12, 4, 5, 6} how many
comparisons are required to find the element 4?
We will have to compare 12 with 20, 4, hence 2 comparisons will be
required to find the element 4.
Example 2: Having understood the linear search find all the occurrences of key in
the array.
#include <stdio.h>
int main()
{
int size = 10, no_of_occurences = 0, i, key;
int A[size];
int O[size];
//Initialize array
for(i = 0; i < size; i++){
scanf("%d",&A[i]);
}
//read the key
scanf("%d", &key);
//now do linear search
for(i = 0; i < size; i++){
if(key == A[i]){
O[no_of_occurences] = i;
no_of_occurences++;
}
}
//OUTPUT THE RESULT
for(i = 0; i < no_of_occurences; i++){
printf("%d ",O[i]);
}
return 0;
}
Exercise 2:
b) Find the index of the last occurrence of key in an array.
#include <stdio.h>
int main()
{
int size = 10, last_occurence = 0, i, key;
int A[size];
//Initialize array
for(i = 0; i < size; i++){
scanf("%d",&A[i]);
}
//read the key
scanf("%d", &key);
//now do linear search
for(i = size-1; i >= 0; i--)
{
if(key == A[i]){
last_occurence = i;
break;
}
}
//OUTPUT THE RESULT
printf("%d \n",last_occurence);
return 0;
}
13
Observe that the array is sorted and has odd number of elements.
First comparison will be 13==7
Second comparison will be 9== 13
Third comparison will be 13== 13
Hence 3 comparisons are needed.
Exercise 4: How many comparisons will be required to find 6 and 103 in this
array[]= {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}?
Find 6 in {-1, 5, 6, 18, 19,
Step 1 (middle element is
Step 2 (middle element is
Step 3 (middle element is
Section 3: Sorting
selection sort
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
Insertion sort
In Insertion sort , sorting takes place by inserting a particular element at the
appropriate position, thats why the name- insertion sorting. In the First iteration,
second element A[1] is compared with the first element A[0]. In the second iteration
third element is compared with first and second element. In general, in every
iteration an element is compared with all the elements before it. While comparing if
it is found that the element can be inserted at a suitable position, then space is
created for it by shifting the other elements one position up and inserts the desired
element at the suitable position. This procedure is repeated for all the elements in
the list.
For instance, Take card players. They arrange the cards in a sequential order.
1:
2:
3:
4:
5:
23
23
23
23
15
| 78 45 60 15
78 | 45 60 15
45 78 | 60 15
45 60 78 | 15
23 45 60 78 |
//Initialize array
for(i = 0; i < r; i++){
for(j = 0; j < c; j++){
scanf("%d",&A[i][j]);
}
}
//insertion sort of the columns
for(c1 = 0; c1 < c; c1++){
for(i = 1; i < r; i++)
{
j = i;
while(j > 0 && A[j-1][c1] > A[j][c1]){
temp = A[j][c1];
A[j][c1] = A[j-1][c1];
A[j-1][c1] = temp;
j--;
}
}
}
//output the result
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++){
printf("%d ",A[i][j]);
}
printf("\n");
}
return 0;
}