Sunteți pe pagina 1din 10

Program to Calculate Sum & Average of an Array

#include <stdio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;

printf ("Enter the value of N \n");


scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/* Summation starts */
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
2.Program to Calculate Sum of all Elements of an Array
using Pointers as Arguments
#include <stdio.h>

void main()
{
static int array[5] = { 200, 400, 600, 800, 1000 };
int sum;

int addnum(int *ptr);

sum = addnum(array);
printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
int index, total = 0;
for (index = 0; index < 5; index++)
{
total += *(ptr + index);
}
return(total);
}

3. Program to Find the Sum of Contiguous Subarray within


a 1 D Array of Numbers which has the Largest Sum
#include <stdio.h>
#include <stdlib.h>

int maxSubArraySum(int a[], int size, int *begin, int *end)


{
int max_so_far = 0, max_end = 0;
int i, current_index = 0;

for (i = 0; i < size; i++)


{
max_end = max_end + a[i];
if (max_end <= 0)
{
max_end = 0;
current_index = i + 1;
}
else if (max_so_far < max_end)
{
max_so_far = max_end;
*begin = current_index;
*end = i;
}
}
return max_so_far;
}

int main()
{
int arr[] = {10, -2, 15, 9, -8, 12, 20, -5};
int start = 0, end = 0;
int size = sizeof(arr) / sizeof(arr[0]);

printf(" The max sum is %d", maxSubArraySum(arr, size, &start, &end));


printf(" The begin and End are %d & %d", start, end);
getchar();
return 0;
}

4. Program to Put Even & Odd Elements of an Array in 2


Separate Arrays
#include <stdio.h>

void main()
{
long int ARR[10], OAR[10], EAR[10];
int i, j = 0, k = 0, n;

printf("Enter the size of array AR \n");


scanf("%d", &n);
printf("Enter the elements of the array \n");
for (i = 0; i < n; i++)
{
scanf("%ld", &ARR[i]);
fflush(stdin);
}
/* Copy odd and even elements into their respective arrays */
for (i = 0; i < n; i++)
{
if (ARR[i] % 2 == 0)
{
EAR[j] = ARR[i];
j++;
}
else
{
OAR[k] = ARR[i];
k++;
}
}
printf("The elements of OAR are \n");
for (i = 0; i < j; i++)
{
printf("%ld\n", OAR[i]);
}
printf("The elements of EAR are \n");
for (i = 0; i < k; i++)
{
printf("%ld\n", EAR[i]);
}
5. 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]);

6.Program to Sort the N Names in an Alphabetical Order


#include <stdio.h>
#include <string.h>

void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;

printf("Enter the value of n \n");


scanf("%d", &n);
printf("Enter %d names n", \n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
}

7.Program to Read an Array and Search for an Element


1. #include <stdio.h>
2.
3. void main()
4. {
5. int array[20];
6. int i, low, mid, high, key, size;
7.
8. printf("Enter the size of an array\n");
9. scanf("%d", &size);
10. printf("Enter the array elements\n");
11. for (i = 0; i < size; i++)
12. {
13. scanf("%d", &array[i]);
14. }
15. printf("Enter the key\n");
16. scanf("%d", &key);
17. /* search begins */
18. low = 0;
19. high = (size - 1);
20. while (low <= high)
21. {
22. mid = (low + high) / 2;
23. if (key == array[mid])
24. {
25. printf("SUCCESSFUL SEARCH\n");
26. return;
27. }
28. if (key < array[mid])
29. high = mid - 1;
30. else
31. low = mid + 1;
32. }
33. printf("UNSUCCESSFUL SEARCH\n");
34. }

8.Program to accept Sorted Array and do Search using


Binary Search
1. #include <stdio.h>
2.
3. void main()
4. {
5. int array[10];
6. int i, j, num, temp, keynum;
7. int low, mid, high;
8.
9. printf("Enter the value of num \n");
10. scanf("%d", &num);
11. printf("Enter the elements one by one \n");
12. for (i = 0; i < num; i++)
13. {
14. scanf("%d", &array[i]);
15. }
16. printf("Input array elements \n");
17. for (i = 0; i < num; i++)
18. {
19. printf("%d\n", array[i]);
20. }
21. /* Bubble sorting begins */
22. for (i = 0; i < num; i++)
23. {
24. for (j = 0; j < (num - i - 1); j++)
25. {
26. if (array[j] > array[j + 1])
27. {
28. temp = array[j];
29. array[j] = array[j + 1];
30. array[j + 1] = temp;
31. }
32. }
33. }
34. printf("Sorted array is...\n");
35. for (i = 0; i < num; i++)
36. {
37. printf("%d\n", array[i]);
38. }
39. printf("Enter the element to be searched \n");
40. scanf("%d", &keynum);
41. /* Binary searching begins */
42. low = 1;
43. high = num;
44. do
45. {
46. mid = (low + high) / 2;
47. if (keynum < array[mid])
48. high = mid - 1;
49. else if (keynum > array[mid])
50. low = mid + 1;
51. } while (keynum != array[mid] && low <= high);
52. if (keynum == array[mid])
53. {
54. printf("SEARCH SUCCESSFUL \n");
55. }
56. else
57. {
58. printf("SEARCH FAILED \n");
59. }
60. }
61.
62.
63.
64.

9.

S-ar putea să vă placă și