Skip to main content

Posts

Showing posts with the label Array

Find the maximum length of the subarray in increasing order.

For example we have an array {5,4,6,-2,-1,0,1,9}. The biggest sub-array in increasing order is: {-2,-1,0,1,9}. So the length is 5. #include<stdio.h> void main () { int arr[8] = {5,4,6,-2,-1,0,1,9},i,arrn[8],count=1,j=0,max; printf("The given array is: "); for(i=0;i<8;i++) printf(" %d" ,arr[i]); // printing the actual array - unnecessary step. printf("\n"); for(i=0;i<7;i++){ if(arr[i]<arr[i+1]) { count++; } else { arrn[j]=count; // adding the length of sub array count=1; j++; } } arrn[j]=count; // adding the final length max=arrn[0]; printf("Length of sub arrays in increasing order"); for(i=0;i<=j;i++){ if(max<arrn[i]) max=arrn[i]; printf(" %d",arrn[i]); // printing the new array - unnecessary step. } printf("\n Maximum Length of the substring: %d",max); // print max va

Find the maximum occurrence of a character in a string

#include<stdio.h> #include<conio.h> int main() { char str[20], ch; int count = 0, i,j=0 , max; int arr[10]; printf("\nEnter a string : "); scanf("%s", &str); printf("\nEnter the character to be searched : "); scanf("%c", &ch); for (i = 0; str[i] != '\0'; i++) { if (str[i] == ch) count++; else if(count != 0) { arr[j] = count; j++; count = 0; } } arr[j]=count; max = 0; for(i=0;i<=j;i++) if(max<arr[i]) max=arr[i]; if (max == 0) printf("\nCharacter '%c'is not present", ch); else { printf("\nMaximum occurrence of character '%c' : %d", ch, max); } return (0); } Output   Print   Output Enter a string : aabbbaaab Enter the character to be searched : Maximum occurrence of character 'a' : 3

Accept Array elements

#include<stdio.h> void main() { int arr[10], i, j, k, size; printf("\nEnter array size : "); scanf("%d", &size); printf("\nAccept Numbers : \n"); for (i = 0; i < size; i++) { printf("Data [%d]: ",i+1); scanf("%d", &arr[i]); } printf("\nThe Array is : "); for (i = 0; i < size; i++) printf("%d ", arr[i]); getch(); } Output   Print   Download Code Output Enter array size : 5 Accept Numbers : Data [1]: 2 Data [2]: 6 Data [3]: 7 Data [4]: 1 Data [5]: 2 The Array is : 2 6 7 1 2