-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from ishitakapoor26/Greatest
Largest element in an array
- Loading branch information
Showing
6 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <math.h> | ||
#include <stdio.h> | ||
int main() { | ||
int low, high, number, originalNumber, rem, count = 0; | ||
double result = 0.0; | ||
printf("Enter two numbers(intervals): "); | ||
scanf("%d %d", &low, &high); | ||
printf("Armstrong numbers between %d and %d are: ", low, high); | ||
|
||
// iterate number from (low + 1) to (high - 1) | ||
// In each iteration, check if number is Armstrong | ||
for (number = low + 1; number < high; ++number) { | ||
originalNumber = number; | ||
|
||
// number of digits calculation | ||
while (originalNumber != 0) { | ||
originalNumber /= 10; | ||
++count; | ||
} | ||
|
||
originalNumber = number; | ||
|
||
// result contains sum of nth power of individual digits | ||
while (originalNumber != 0) { | ||
rem = originalNumber % 10; | ||
result += pow(rem, count); | ||
originalNumber /= 10; | ||
} | ||
|
||
// check if number is equal to the sum of nth power of individual digits | ||
if ((int)result == number) { | ||
printf("%d ", number); | ||
} | ||
|
||
// resetting the values | ||
count = 0; | ||
result = 0; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
char str[100]; | ||
char *p; | ||
int vCount=0,cCount=0; | ||
|
||
printf("Enter any string: "); | ||
fgets(str, 100, stdin); | ||
|
||
//assign base address of char array to pointer | ||
p=str; | ||
|
||
//'\0' signifies end of the string | ||
while(*p!='\0') | ||
{ | ||
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' | ||
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u') | ||
vCount++; | ||
else | ||
cCount++; | ||
//increase the pointer, to point next character | ||
p++; | ||
} | ||
|
||
printf("Number of Vowels in String: %d\n",vCount); | ||
printf("Number of Consonants in String: %d",cCount); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// C program to find maximum in arr[] of size n | ||
#include <stdio.h> | ||
|
||
// C function to find maximum in arr[] of size n | ||
int largest(int arr[], int n) | ||
{ | ||
int i; | ||
|
||
// Initialize maximum element | ||
int max = arr[0]; | ||
|
||
// Traverse array elements from second and | ||
// compare every element with current max | ||
for (i = 1; i < n; i++) | ||
if (arr[i] > max) | ||
max = arr[i]; | ||
|
||
return max; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = {10, 324, 45, 90, 9808}; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
printf("Largest in given array is %d", largest(arr, n)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* structure is used to return two values from minMax() */ | ||
#include<stdio.h> | ||
struct pair | ||
{ | ||
int min; | ||
int max; | ||
}; | ||
|
||
struct pair getMinMax(int arr[], int n) | ||
{ | ||
struct pair minmax; | ||
int i; | ||
|
||
/*If there is only one element then return it as min and max both*/ | ||
if (n == 1) | ||
{ | ||
minmax.max = arr[0]; | ||
minmax.min = arr[0]; | ||
return minmax; | ||
} | ||
|
||
/* If there are more than one elements, then initialize min | ||
and max*/ | ||
if (arr[0] > arr[1]) | ||
{ | ||
minmax.max = arr[0]; | ||
minmax.min = arr[1]; | ||
} | ||
else | ||
{ | ||
minmax.max = arr[1]; | ||
minmax.min = arr[0]; | ||
} | ||
|
||
for (i = 2; i<n; i++) | ||
{ | ||
if (arr[i] > minmax.max) | ||
minmax.max = arr[i]; | ||
|
||
else if (arr[i] < minmax.min) | ||
minmax.min = arr[i]; | ||
} | ||
|
||
return minmax; | ||
} | ||
|
||
/* Driver program to test above function */ | ||
int main() | ||
{ | ||
int arr[] = {1000, 11, 445, 1, 330, 3000}; | ||
int arr_size = 6; | ||
struct pair minmax = getMinMax (arr, arr_size); | ||
printf("nMinimum element is %d", minmax.min); | ||
printf("nMaximum element is %d", minmax.max); | ||
getchar(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// A Simple Merge based O(n) solution to find | ||
// median of two sorted arrays | ||
#include <stdio.h> | ||
|
||
/* This function returns median of ar1[] and ar2[]. | ||
Assumption in this function: | ||
Both ar1[] and ar2[] are sorted arrays */ | ||
int getMedian(int ar1[], int ar2[], int n, int m) | ||
{ | ||
int i = 0; /* Current index of input array ar1[] */ | ||
int j = 0; /* Current index of input array ar2[] */ | ||
int count; | ||
int m1 = -1, m2 = -1; | ||
|
||
// Since there are (n+m) elements, | ||
// There are following two cases | ||
// if n+m is odd then the middle | ||
//index is median i.e. (m+n)/2 | ||
if((m + n) % 2 == 1) { | ||
for (count = 0; count <= (n + m)/2; count++) { | ||
if(i != n && j != m){ | ||
m1 = (ar1[i] > ar2[j]) ? ar2[j++] : ar1[i++]; | ||
} | ||
else if(i < n){ | ||
m1 = ar1[i++]; | ||
} | ||
// for case when j<m, | ||
else{ | ||
m1 = ar2[j++]; | ||
} | ||
} | ||
return m1; | ||
} | ||
|
||
// median will be average of elements | ||
// at index ((m+n)/2 - 1) and (m+n)/2 | ||
// in the array obtained after merging ar1 and ar2 | ||
else { | ||
for (count = 0; count <= (n + m)/2; count++) { | ||
m2 = m1; | ||
if(i != n && j != m){ | ||
m1 = (ar1[i] > ar2[j]) ? ar2[j++] : ar1[i++]; | ||
} | ||
else if(i < n){ | ||
m1 = ar1[i++]; | ||
} | ||
// for case when j<m, | ||
else{ | ||
m1 = ar1[j++]; | ||
} | ||
} | ||
return (m1 + m2)/2; | ||
} | ||
} | ||
|
||
/* Driver program to test above function */ | ||
int main() | ||
{ | ||
int ar1[] = {900}; | ||
int ar2[] = {5, 8, 10, 20}; | ||
|
||
int n1 = sizeof(ar1)/sizeof(ar1[0]); | ||
int n2 = sizeof(ar2)/sizeof(ar2[0]); | ||
printf("%d", getMedian(ar1, ar2, n1, n2)); | ||
getchar(); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include<stdio.h> | ||
void TOH(int n,char x,char y,char z) { | ||
if(n>0) { | ||
TOH(n-1,x,z,y); | ||
printf("\n%c to %c",x,y); | ||
TOH(n-1,z,y,x); | ||
} | ||
} | ||
int main() { | ||
int n=3; | ||
TOH(n,'A','B','C'); | ||
} |