Sunteți pe pagina 1din 15

Topic: Arrays

Single Dimensional Arrays


1 Write a C program to read an array of N elements and print them.
2 Write a C program to read an array of N elements and print them in
reverse order.
3 Write a C program to find the Sum(positive numbers & negative
numbers separately) and Average of the array numbers.-
4 Write a C program to find the Largest and Smallest numbers in the
array list.
5 Write a C program to copy the contents of one array into another
array.
6 Write a C program to count the number of elements that are
duplicated in the array list.
7 Write a C program to print only the unique values that are present in
the array list.
8 Write a C program to count the occurrence of each element in the
array list.
9 Write a c program to separate the Odd and Even numbers from the
array list and print them.
10 Write a C program to read N elements and print the Odd and Even
index position elements separately.
11 Write a C program to sort the elements within the array list –
Ascending Order
Descending Order
12 Write a C program to read N elements from the user and search
whether a particular element is present within the array list or not
using Binary Search method
13 Write a C program to read N elements from the user and search
whether a particular element is present within the array list or not
using Linear Search method
14 Write a C program to merge two arrays of the same size, sort them
and print the merged list.
Program 1: Write a C program to read an array of N elements and print
them.
Solution:
#include <stdio.h>
#define MAXSIZE 50
int main()
{
int array[MAXSIZE],i;
printf ("\n Enter the value to store in the Array (Max 50):");
scanf("%d", &num);
printf("\n Enter %d numbers one by one \n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("\n The Array elements are as follows - \n");
for (i = 0; i < num; i++)
{
printf("\n Element %d: %d",i+1,array[i]);
}
return 0;
}
Program 2: Write a C program to read an array of N elements and print
them in reverse order.
Solution:
#include <stdio.h>
#define MAX 50
int main()
{
int i,n,a[MAX];
printf("Input the number of elements to store in the array (MAX 50): ");
scanf("%d",&n);
printf("Input %d number of elements one ny one :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
printf("\n The values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("\n Element %d: %d",i+1,a[i]);
}

printf("\n Array Elements in reverse \n");


for(i=n-1;i>=0;i--)
{
printf("\n Element %d: %d",i+1,a[i]);
}
return 0;
}
Program 3: Write a C Program to find the Sum(positive numbers &
negative numbers separately) and Average of the array numbers.
Solution:
#include <stdio.h>
#define MAXSIZE 50
int main()
{
int array[MAXSIZE];
int i,num,neg_sum=0,pos_sum=0;
printf ("\n Enter the value to store in the Array (Max 50):");
scanf("%d", &num);
printf("Enter %d numbers one by one \n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}

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


{
if (array[i] < 0)
{
neg_sum = neg_sum + array[i];
}
else
{
pos_sum = pos_sum + array[i];
}
}
total = neg_sum + pos_sum;
average = total / num;
printf("\n Sum of all negative numbers =%d",neg_sum);
printf("\n Sum of all positive numbers =%d",pos_sum);
printf("\n Total Sum of the Array is :%f",total);
printf("\n Average =%.2f",average);
return 0;
}
Program 4: Write a C program to find the Largest and Smallest numbers in
the array list.
Solution:
#include <stdio.h>
#define MAXSIZE 50
int main()
{
int array[MAXSIZE],num,i,largest,smallest;
printf ("\n Enter the value to store in the Array (Max 50):");
scanf("%d", &num);
printf("\n Enter %d numbers one by one \n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
largest = array[0];
smallest = array[0];
for (i = 1; i < num; i++)
{
if (largest < array[i])
largest = array[i];
}
for (i = 1; i < num; i++)
{
if (smallest > array[i])
smallest = array[i];
}
printf("\n largest element present in the given array is : %d", largest);
printf("\n smallest element present in the given array is : %d", smallest);
return 0;
}
Program 5: Write a C program to copy the contents of one array into
another array.
Solution:
#include <stdio.h>
#define MAX 50
int main()
{
int arr1[MAX], arr2[MAX];
int i, n;
printf("\n Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("\n Input %d elements one by one \n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/* copying elements of 1st array into 2nd array.*/
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}

printf("\n Array 1 \n");


for(i=0; i<n; i++)
{
printf("% 5d", arr1[i]);
}
printf("\n Array 2 \n");
for(i=0; i<n; i++)
{
printf("% 5d", arr2[i]);
}
return 0;
}
Program 6: Write a C program to count the number of elements that are
duplicated in the array list.
Solution:
#include <stdio.h>
#define MAX 50
int main()
{
int arr1[MAX];
int arr2[MAX];
int arr3[MAX];
int n,mm=1,ctr=0;
int i, j;
printf("\n Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("\n Input %d elements one by one \n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

/* copy in other array */


for(i=0;i<n; i++)
{
arr2[i]=arr1[i];
arr3[i]=0;
}
/* mark the elements are duplicate */
for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
{
arr3[j]=mm;
mm++;
}
}
mm=1;
}

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


{
if(arr3[i]==2)
{
ctr++;
}
}
printf("The total number of duplicate elements found in the array is: %d
\n", ctr);
return 0;
}
Program 7: Write a C program to print only the unique values that are
present in the array list.
Solution 1:
#include <stdio.h>
#define MAX 50
int main()
{
int array[MAX], num;
int i,j;
printf ("\n Enter the value to store in the Array (Max 50):");
scanf("%d", &num);
printf("\n Enter %d numbers one by one \n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}

printf("Unique Elements\n");
for(i = 0; i < num; i++)
{
for (j=0; j<i; j++)
{
if (array[i] == array[j])
break;
}

if (i == j)
{
printf("%d \t", array[i]);
}
}
return 0;
}
Solution 2:
#include <stdio.h>
int main()
{
int arr[10], FreqArr[10], i, j, Count, Size;
printf("\n Please Enter Number of elements in an array:");
scanf("%d", &Size);
printf("\n Please Enter %d elements of an Array\n", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
FreqArr[i] = -1;
}
for (i = 0; i < Size; i++)
{
Count = 1;
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
Count++;
FreqArr[j] = 0;
}
}
if(FreqArr[i] != 0)
{
FreqArr[i] = Count;
}
}
printf("\n List of Unique Array Elemnts in this Array are : ");
for (i = 0; i < Size; i++)
{
if(FreqArr[i] == 1)
{
printf("%d\t", arr[i]);
}
}
return 0;
}
Program 8: Write a C program to count the occurrence of each element in
the array list.
Solution:
#include <stdio.h>
int main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
printf("\n Input the number of elements to be stored in the array :");
scanf("%d",&n);

printf("\n Enter %d numbers one by one \n",n);


for (i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
fr1[i] = -1;
}

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


{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}

if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\n The frequency of all elements of array \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("\n %d occurs %d times", arr1[i], fr1[i]);
}
}
return 0;
}
Program 9: Write a c program to separate the Odd and Even numbers from
the array list and print them.
Solution:
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("\n Input %d elements one by one \n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}

printf("\n The Even elements are \n");


for(i=0;i<j;i++)
{
printf("%d\t",arr2[i]);
}

printf("\n The Odd elements are \n");


for(i=0;i<k;i++)
{
printf("%d\t", arr3[i]);
}
return 0;
}
Program 10: Write a C program to read N elements and print the Odd and
Even index position elements separately.
Solution:
#include <stdio.h>
int main()
{
int arr1[20];
int i,n;
printf("\n Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("\n Input %d elements one by one \n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\n Odd Index Position Elements are - \n");
for(i=1;i<n;i+=2)
{
printf("%d\t", arr1[i]);
}
printf("\n Even Index Position Elements are - \n");
for(i=0;i<n;i+=2)
{
printf("%d\t", arr1[i]);
}
return 0;
}
Program 11: Write a C program to sort the elements within the array list –
 Ascending Order
 Descending Order
Solution:
#include <stdio.h>
int main()
{
int arr1[50];
int n, i, j, tmp;
printf("\n Enter the Number of Elements to be inserted in to the array:
");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
// Sort
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] < arr1[i]) if(arr1[j] > arr1[i]) For Decending Order
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\n Sorted Array List - Ascending Order \n");
for(i=0; i<n; i++)
{
printf("%d\t", arr1[i]);
}
return 0;
}
Program 12: Write a C program to read N elements from the user and
search whether a particular element is present within the array list or not
using Binary Search method
Solution:
#include <stdio.h>
int main()
{
int array[20];
int i, low, mid, high, key, size;
printf("\n Enter the Np. of Elements to be Inserted: ");
scanf("%d", &size);
printf("\n Enter %d elements \n",size);
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("\n Enter the Element to Search for: ");
scanf("%d", &key);
low = 0;
high = (size - 1);
while (low <= high)
{
mid = (low + high) / 2;
if (key == array[mid])
{
printf("\n SUCCESSFUL SEARCH");
return 0;
}
if (key < array[mid])
high = mid - 1;
else
low = mid + 1;
}
printf("\n UNSUCCESSFUL SEARCH");
return 0;
}
Program 14: Write a C program to merge two arrays of the same size, sort
them and print the merged list.
Solution:
#include <stdio.h>
int main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
printf("\n Input the number of elements to be stored in the first array:
");
scanf("%d",&s1);
printf("\n Input %d elements \n",s1);
for(i=0;i<s1;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\n Input the number of elements to be stored in the second array:
");
scanf("%d",&s2);
printf("\n Input %d elements \n",s2);
for(i=0;i<s2;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr2[i]);
}

s3 = s1 + s2;

for(i=0;i<s1; i++)
{
arr3[i] = arr1[i];
}
for(j=0;j<s2; j++)
{
arr3[i] = arr2[j];
i++;
}

for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{
if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}

printf("\n The merged array in decending order \n");


for(i=0; i<s3; i++)
{
printf("%d\t", arr3[i]);
}
return 0;
}

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