Array and string(1)

799 views
725 views

Published on

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
799
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Array and string(1)

  1. 1. Array and String
  2. 2. Two Dimensional Array <ul><li>#include<conio.h> #include<stdio.h> #define rows 2 </li></ul><ul><li>#define columns 2 void main(void) {     clrscr();     float agents [rows] [columns];     for(int i=0; i<2; i++)     {         for(int j=0; j<2;j++)         {             agents[i][j] = i*j;         }     }     for(i=0; i<2; i++)     {         for(int j=0; j<2;j++)         {             printf(&quot;%f n&quot;, agents[i][j]);         }     }     getch(); } </li></ul>
  3. 3. Initializing Two dimensional Array <ul><li>#include<conio.h> #include<stdio.h> #define rows  2 #define columns 2 float agents[rows][columns] = {{1,1} , {2,2}}; void main(void) {     clrscr();     for(int i=0; i<2; i++)     {         for(int j=0; j<2;j++)         {             printf(&quot;%f n&quot;, agents[i][j]);         }     }     getch(); } </li></ul>
  4. 4. Initializing Three dimensional Array <ul><li>#include<conio.h> #include<stdio.h> void main(void) {     int agents[2][2][3] = {{{1,2,3},{3,2,1}} , {{4,5,6},{6,5,4}}};     clrscr();     for(int i=0; i<2; i++)     {         for(int j=0; j<2;j++)         {             for(int k=0; k<3; k++)             {                 printf(&quot;%d n&quot;, agents[i][j][k]);             }         }     }     getch(); } </li></ul>
  5. 5. Array as Arguments <ul><li>#include<conio.h> #include<stdio.h> int getFirst(int arr[]); void main(void) {     clrscr();     int arr[] = {1,2,3,4};     int First = getFirst(arr);     printf(&quot;%d&quot;,First);     getch(); } int getFirst(int arr[]) {        return arr[0]; } </li></ul>
  6. 6. Two dimensional Array as Arguments <ul><li>#include<conio.h> #include<stdio.h> #define columns 2 #define rows 2 int getFirst(int arr[][columns]); int arr[rows][columns] = {{1,2},{3,4}}; void main(void) {     clrscr();     int First = getFirst(arr);     printf(&quot;%d&quot;,First);     getch(); } int getFirst(int arr[][columns]) {        return arr[0][0]; } </li></ul>
  7. 7.   <ul><li>#include<conio.h> #include<stdio.h> #include<string.h> #define columns 2 #define rows 2 void main(void) {     clrscr();     char arr[] = {'Z','A','B','I','S','T'};     printf(&quot;%s&quot;,arr);     gets(arr);     puts(arr);     printf(&quot;%s&quot;,strlen(arr));     getch(); } </li></ul>
  8. 8. Exercise <ul><li>write a program which gets string and sort alphabetically, and count each character and their position and save it in two dimensional array. </li></ul><ul><li>assume every character cannot be used more than 2 </li></ul><ul><li>Like: char arr[] = {'P','R','O','G','R','A','M','M','E','R'}; </li></ul><ul><li>result[1][1] = 'P'; </li></ul><ul><li>result[1][2] = '1'; </li></ul><ul><li>result[2][1] = 'R'; </li></ul><ul><li>result[2][2] = '3'; </li></ul><ul><li>sort => AEGMMOPRRR </li></ul>

×