0% found this document useful (0 votes)
47 views

5 6 Array

The document discusses data structures and arrays in C programming. It defines a data structure as the organization of data in memory and provides examples like arrays, linked lists, stacks, and queues. It then covers key concepts about arrays like declaring and initializing arrays, one-dimensional and multi-dimensional arrays, accessing array elements, taking input from the user, and some common operations like sorting, searching and reversing arrays.

Uploaded by

G tarik
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

5 6 Array

The document discusses data structures and arrays in C programming. It defines a data structure as the organization of data in memory and provides examples like arrays, linked lists, stacks, and queues. It then covers key concepts about arrays like declaring and initializing arrays, one-dimensional and multi-dimensional arrays, accessing array elements, taking input from the user, and some common operations like sorting, searching and reversing arrays.

Uploaded by

G tarik
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Data structure

 A Data structure is the organization of data in


memory.
 It defines how data are stored in computer
memory and the relationship among these data
 Also provide facilities to manipulate data in
memory
 Examples:
 Array, Linked List, Stack
 Queue, Graph, Tree
Ab xyz PQ
[0] 02.0
[1] 12.5 [0] [1] [2]

[2] 92.1
[3] 55.4
[4] 6.22
10 20 17 30 0
[0] [1] [2] [3] [4]
Types of Array
 One Dimentional Array

10 20 5 8 13

Three Dimentional Array


 Two Dimentional Array

14 56 78
45 23 31
12 56 32
Array
 Array organize similar type of data in
contigious memory location.

12 90 57 20

 This array containing all integer data


 An array can store either integer or float or
character type data
Example of declaring/creating Array in C

int roll [ 5] ;

Size of
Data Type of Array Array/Number of
element in array
Name of Array
Declaring Arrays

 To declare an array in C, a programmer


specifies
 Thetype of the elements
 Name of the Array and
 Number of elements(size of the array)

 Like this:
 ArrayType ArrayName [ArraySize];

Number of
elements in
Array
Create/declare an Array in C

 int roll[5];

roll
[0] ]1[ [2] [3] [4]

 This array named as “roll” can store 5 integer


numbers
Assigning value in an Array

int roll[5]={10,20,17,30,9};
printf(“%d”, roll[4])
printf(“%d”, roll[2])

roll 10 20 17 30 9
[0] [1] [2] [3] [4]
int roll[5];
 roll[0]=10;
 roll[1]=20;
 roll[2]=17; printf(“%d”, roll[4])
printf(“%d”, roll[2])
 roll[3]=30;
 roll[4]=9;

roll 10 20 17 30 9
[0] [1] [2] [3] [4]
 How to declare a 2D array ?
 int abc[2][3];
How to take input from Keyboard
 int roll[5];
 scanf(“%d”,&roll[0]);
20
 scanf(“%d”,&roll[1]); 23
10
 scanf(“%d”,&roll[2]); 36
23
 scanf(“%d”,&roll[3]);
 scanf(“%d”,&roll[4]);
Advantages of arrays:
Advantage of Array
 Accessing Array is very fast.
 It is capable of storing many elements at a time
 It allows random accessing of elements
 i.e. any element of the array can be randomly
accessed using indexes.
 You can use one name for similar objects and save
then with the same name but different indices. 

 Arrays are very useful when you are working with


sequences of the same kind of data (similar to the
first point but has a different meaning).
Disadvantages of Array
Disadvantage of Array
 Can’t store different type of data
 Predetermining the size of the array is a
must.
 There is a chance of memory wastage
or shortage.
 To delete one element in the array, we
need to traverse throughout the array.
 Sometimes it's not easy to operate with
many indices arrays. 
Store some data (5 data) in an array and
shows it
 main()
 {
 int roll[5],i;
 for(i=0;i<=4;i=i+1)
 {
 scanf("%d",&roll[i]);
 }
 for(i=0;i<=4;i=i+1)
 {
 printf(“\t %d",roll[i]);
 }
 }
Store some data in array and shows it in
reverse order
 main()
 {
 int roll[5],i;
 for(i=0;i<=4;i++)
 {
 scanf("%d",&roll[i]);
 }
 for(i=4;i>=0;i--)
 {
 printf("%d",roll[i]);
 }
 getch();
 }
Takes some values from array and calculate
summation
 #include<stdio.h>
 main()
{
 int a[5]={2,6,1,3,7},i,s=0;
 for(i=0;i<=4;i=i+1)
 {
 s=s+a[i];
 }
 printf("%d",s);
}
How can u change the program for
adding 10 numbers
#include<stdio.h>
main()
{
 int A,B,Sum;
 scanf("%d %d" , &A, &B);
 Sum=A+B;
 printf("%d" , Sum);

}
Take 10 integer numbers from user and calculate
summation
 main()
{
 int a[10],i,s=0;
 for(i=0;i<=9;i++)
 {
 scanf("%d",&a[i]);
 s=s+a[i];
 }
 printf("%d",s);
}
Stem: solve the program by using
loop
 #include<stdio.h>
 main()
{
 int a,b,c, S;
 scanf(“%d %d %d" , &a, &b, &c);
 S=a+b+c;
 printf("%d",S);
}
Stem

[0] [1] [2] [3] [4]

Write a C program to take user data from


keyboard and store in the stem above.
How can the show the data at 2nd
position?
main()
{

 int a[5]={9,7,1,12,5},mx,i;

 mx=a[0];
 for(i=0;i<=4;i++)
 {
 if(a[i]>mx)
 mx=a[i];
}
 printf("maximum=%d",mx);
}
Count how many charecter and
digit in a string
 For 0 to 9 ASCII 48 to 57
 For A to Z and a to z ASCII 65 to 96 and 97 to
122
Sequential search using array
 main()
{
 int array[6], i,q;
 printf("Give 6 numbers of the Array ");
 for(i=1;i<=6;i++)
 {
 scanf("%d",&array[i]);
 }
 printf("Give a number to search in this array");
 scanf("%d",&q);
 for (i=1;i<=6;i++)
 {
 if (q==array[i])
 {
 printf("Found");
 break;
 }
 }
 getch();
 }
Sorting

 #include<conio.h>
 #include<stdio.h>
 main()
 {
 int n,i,j,tmp;

 int a[6];
 for(i=1;i<=6;i++)
 {
 scanf("%d",&a[i]);
 }

 for (i=1; i<=6;i++)
 {
 for (j=i+1; j<=6;j++)
 {
 if (a[i]>a[j])
 {
 tmp=a[i];
 a[i]=a[j];
 a[j]=tmp;
 }
 }
 }

 for(i=1;i<=6;i++)
 {
 printf("%d",a[i]);
 }
 getch();
 }
Reverse a string
 main()
 {
 int l,i,j;
 char a[20]={"RED WHITE"},tmp;
 l=strlen(a);
 j=l-1;

 for(i=0;i<l/2;i++)
 {
 tmp=a[i];
 a[i]=a[j];
 a[j]=tmp;
 j--;
 }
 puts(a);
 }
this program find how many 'x' in a
input string
 #include<conio.h>
 #include<stdio.h>
 #include<string.h>
 main()
 {
 char a[20];
 int i,l;
 gets(a);
 l=strlen(a);
 int count=0;
 for(i=0;i<l;i++)
 {
 if (a[i]=='x')
 count++;
 }
 printf(“%d”,count)
 getch();
 }
finds the largest number from a given
array
 main()
 {
 int a[4]={4,61,1,7},i;
 int max=a[0];
 for(i=0;i<=3;i++)
 {
 if (max<a[i])
 {
 max=a[i];
 }
 }
 printf("\n the maximum number is= %d",max);
 }
this program takes an integer value
and reverse it
 #include<conio.h>
 #include<iostream.h>

 void main()
 {
 clrscr();
 int n,x,r;
 cin>>n;
 do {
 r=n%10;
 x=n/10;
 n=x;
 cout<<r;
 }while(n>0);

 getch();
 }
this program takes n number of integer and
reverse it (not only shows it)
 #include<conio.h>
 #include<iostream.h>

 void main()
 {
 clrscr();
 int n,i,j,a[10],b[10];
 cin>>n;
 for(i=0;i<n;i++)
 {
 cin>>a[i];
 }
 // copy from a[] to b[]
 i=n-1;
 for(j=0;j<n;j++)
 {
 b[j]=a[i];
 i--;
 }
this program takes n number of integer and
reverse it use only one array which it used to
store first
 #include<conio.h>
 #include<iostream.h>
 void main()
 {
 int n,i,j,a[10],tmp;
 cin>>n;
 for(i=0;i<n;i++)
 {
 cin>>a[i];
 }
 j=n-1;
 for(i=0;i<n/2;i++)
 {
 tmp=a[i];
 a[i]=a[j];
 a[j]=tmp;
 j--;
 }
 for(i=0;i<n;i++)
 {
 cout<<a[i];
 }
 }
 // copy from b[] to a[]
 j=0;
 for(i=0;i<n;i++)
 {
 a[i]=b[j];
 j++;
 }
 // show the first array (it has been changed in reverse order)
 for(i=0;i<n;i++)
 {
 cout<<a[i];
 }

 getch();
 }
Adding two matrix( both are 2x3) A &
B and produce matrix C
 #include<conio.h>
 #include<iostream.h>
 main()
 {
 int a[2][3]={{1,2,3},{4,5,6}}, b[2][3]={{2,4,6},{8,10,12}},c[2][3];
 int i,j;
 cout<<"matrix A \n";
 for (i=0;i<=1;i++)
 {
 for(j=0;j<=2;j++)
 {
 cout<<a[i][j];
 }
 cout<<"\n";
 }
 cout<<"matrix B \n";
 for (i=0;i<=1;i++)
 {
 for(j=0;j<=2;j++)
 {
 cout<<b[i][j];
 }
 cout<<"\n";
 }
 cout<<"matrix C=A+B \n";
 for (i=0;i<=1;i++)
 {
 for(j=0;j<=2;j++)
 {
 c[i][j]=a[i][j]+b[i][j];
 cout<<c[i][j];
 }
 cout<<"\n";
 }

 getch();
C Program to Sort the Array in an Ascending
Order
 #include <stdio.h>
  
 void main()
 {
 int i, j, a, n, number[30];
  
 printf("Enter the value of N \n");
 scanf("%d", &n);
 printf("Enter the numbers \n");
 for (i = 0; i < n; ++i)
 scanf("%d", &number[i]);
 for (i = 0; i < n; ++i)
 {
 for (j = i + 1; j < n; ++j)
 {
 if (number[i] > number[j])
 {
 a = number[i];
 number[i] = number[j];
 number[j] = a;
 }
 }
 }
 printf("The numbers arranged in ascending order are given below \n");
 for (i = 0; i < n; ++i)
 printf("%d\n", number[i]);
 }
 $ cc pgm66.c
 $ a.out
 Enter the value of N
 6
 Enter the numbers
 3
 78
 90
 456
 780
 200
 The numbers arranged in ascending order are given below
 3
 78
 90
 200
 456
 780
Linear search c program

 #include <stdio.h>
  int main()
 {
 int array[100], search, c, n;
  
 printf("Enter the number of elements in array\n");
 scanf("%d",&n);
  
 printf("Enter %d integer(s)\n", n);
  
 for (c = 0; c < n; c++)
 scanf("%d", &array[c]);
  
 printf("Enter the number to search\n");
 scanf("%d", &search);
  
 for (c = 0; c < n; c++)
 {
 if (array[c] == search) /* if required element found */
 {
 printf("%d is present at location %d.\n", search, c+1);
 break;
 }
 }
 if (c == n)
 printf("%d is not present in array.\n", search);
  
 return 0;
 }
C Program to Read an Array and
Search(Binary) for an Element
 #include <stdio.h>
 void main()
 {
 int array[20];
 int i, low, mid, high, key, size;
  
 printf("Enter the size of an array\n");
 scanf("%d", &size);
 printf("Enter the array elements\n");
 for (i = 0; i < size; i++)
 {
 scanf("%d", &array[i]);
 }
 printf("Enter the key\n");
 scanf("%d", &key);
 /* search begins */
 low = 0;
 high = (size - 1);
 while (low <= high)
 {
 mid = (low + high) / 2;
 if (key == array[mid])
 {
 printf("SUCCESSFUL SEARCH\n");
 return;
 }
 if (key < array[mid])
 high = mid - 1;
 else
 low = mid + 1;
 }
 printf("UNSUCCESSFUL SEARCH\n");
 }

You might also like