Upload
nisarg-pathak
View
37
Download
3
Embed Size (px)
Citation preview
02/05/2023 1
Keypoints:-
C MAIN FORMAT
ONE D ARRAY
TWO D ARRAY
02/05/2023 2
Quick information on format:-
main(){
…………………………………………………………………………………..
}
Function name
Start of program
Program statements
End of program
02/05/2023 3
Finally Array
Fixed size sequence collection of elements of same data type.
Simply a grouping of data types.
Used to list of names or numbers.
02/05/2023 4
Theory related :-
Declaring individual variables?....such as number 0, number 1, and number 99.But in Array
Number[1]
Number[2]
Number[99]
02/05/2023 5
All array consists of continuous memory locations.
Numbers[0] Number[1] Number[2] Number[3] ………………
First element Last element
Lowest address corresponds Highest address corresponds
02/05/2023 6
Three types of Array:-
One dimensional Array
Two dimensional Array
Multi dimensional Array
02/05/2023 7
One dimensional Array:-
The values to the array elements can be assigned as follows
number[0]
number[1]number[2]number[3]
number[4]
35
40
205719
02/05/2023 8
One dimensional Array(includes):-
Declaration of one dimensional Array Initialization of one dimensional Array
Specifies properties of an identifier
It declares what a word (identifier) means.
Commonly used for functions ,variables , constants ….sometimes type definitions also
Assignment of an initial value for data object or variable.
02/05/2023 9
Declaration of one dimensional Array:-
data_type array_name[array_size];
float marks[5]Here, we declared an array , mark, of floating-point type and size 5. meaning , it can hold 5 floating points values.
Int group[10]
It declares the group as an array contain 10 integer constants.
char name [10];
It declares the name as a character array that hold maximum of 10 character.
For example
02/05/2023 10
How to initializ
e?
Type array-name[size]={list of value};
For example Int mark[5]={19,10,8,17,9};
Mark[4]
Mark[3]
Mark[2]
Mark[1]
Mark[0]
19 10
8 17 9
Float total[5]={0.0,15.75,-10};
Another method to initialization an array
Char name[]={ ‘N’, ’I’, ’S’, ’S’, ’O’ ,’N’};
Int mark[]={19,10,8,17,9}
02/05/2023 11
Example of one dimensional array with the help of program:-
Write a program to print the sum of 10 numbers using one D Array
02/05/2023 12
One D array
#include<stdio.h>#include<conio.h>Void main(){ int a[10], I, sum=0; clrscr();
printf(” enter 10 array elements”); for(i=0; i<10; i++;){ scanf(“%d”, &a[i]); sum= sum+a[i];} printf(“ summation of array elements is %d”, sum); getch();}
Header file
Pre defined function
Data types
Pre defined function for printing output
loop
statements
02/05/2023 13
Output int a[10], I, sum=0; clrscr(); printf(” enter 10 array elements”); for(i=0; i<10; i++;){ scanf(“%d”, &a[i]); sum= sum+a[i];} printf(“ summation of array elements is
%d”,sum);
20
1
30 40 50 60 70 80 90 100
0
10
2 3 4 5 6 7 8 9
a[0] a[1] a[8]a[4] a[7]a[6]a[3]a[2] a[5] a[9]
Output=550
02/05/2023 14
2. Write a program of odd & even elements from 1D Array.
02/05/2023 15
#include<stdio.h>#include<conio.h>Void main(){ int a[10], n, esum=0, Osum=0, j; clrscr(); printf(“how many elements?”); scanf(“%d”, &n); printf(“enter array elements”); for(i=0; i<n; i++);{ scanf(“%d”, &a[i]); if(a[i]%2==0){ esum++;} else{ osum++;} printf(“even elements %d \n odd elements %d”, esum,osum); getch();}
For even
For odd
02/0
5/20
23
16
printf(“enter array elements”);
for(i=0; i<n; i++);
{
scanf(“%d”, &a[i]);
if(a[i]%2==0)
{
esum++;
}
else
{
osum++;
}
printf(“even elements %d \n odd elements %d”, esum , osum);
Output 20 35 4010 45 60 70 900 31 2 654 7
Even elements=6
Odd elements=2
02/05/2023
17Two dimensional array:-
Type array_name [row size] [coloumn_size];
02/05/2023 18
How to initialize two dimensional array?
Int table [2] [3]={0,0,0,1,1,1};
Int table [2] [3]={ {0,0,0},{1,1,1}};
Int table [2] [3]={ {0,0,0}, {1,1,1} };
Also initialize in the form of a matrix.
02/05/2023 19
Example of two dimensional array:-
Program to read and display a simple 3*3 matrix
02/05/2023 20
#include<stdio.h>#include<conio.h>Void main(){Int I,j,a[3] [3];Clrscr();Printf(“enter the element of 3*3 matrix:”);For(i=0;i<3;i++)For(j=0;j<3;j++){ printf(“a[%d] [%d]=“,I,j); scanf(“%d”,&a[i] [j]); printf(“the various elements contained in the 3*3 matrix are:\n”); for(i=0;i<3;i++){ for(j=0;j<3;j++) printf(“%d\t”,a[i] [j]);}Getch();}
02/05/2023 21
Enter the elements of the 3*3 matrix:A[0] [0]=1A[0] [1]=2A[0] [2]=3A[1] [0]=4A[1] [1]=5A[1] [2]=6A[2] [0]=7A[2] [1]=8A[2] [2]=9the various elements contained in 3*3 matrix are:1 2 34 5 67 8 9
02/05/2023 22
That’s it…..
02/05/2023 23
02/05/2023 24