Arrays
Arrays
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int a[MAX_SIZE], b[MAX_SIZE];
int i, n;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<n; i++)
{
b[i] = a[i];
}
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
for(i=0; i<n; i++)
{
printf("%d\t", b[i]);
}
return 0;
}
Output:
no of elements in an array: 7
array elments are: 1 3 4 2 5 6 7
at which position element should be deleted: 5
elements after deleting 1 3 4 2 6 7
Program to count frequency of
each element of array
#include<stdio.h>
int main(){
int a[100],freq[100];
int i,j,count,n;
printf("no of elements in an array: ");
scanf("%d",&n);
printf("array elements are: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
freq[i]=-1;
}
for(i=0;i<n;i++){
count=1;
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
count+=1;
freq[j]=0;
}
}
if(freq[i]!=0){
freq[i]=count;
}
}
printf("\nFrequency of all elements of array : \n");
for(i=0; i<n; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", a[i], freq[i]);
}
}
return 0;
}
Output:
Frequency of all elements of array :
2 occurs 2 times
3 occurs 2 times
4 occurs 1 times
1 occurs 1 times
C program to print all unique
elements in array
#include<stdio.h>
#define size 100
int main(){
int n,i,j,count,a[size],freq[size];
printf("no of elements in array: ");
scanf("%d",&n);
printf("arrays elements are: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
freq[i]=-1;
}
for(i=0;i<n;i++){
count=1;
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
count++;
freq[j]=0;
}
}
if(freq[i]!=0){
freq[i]=count;
}
}
for(i=0;i<n;i++){
if(freq[i]==1){
printf("%d ",a[i]);
}
}
return 0;
}
Output:
no of elements in array: 7
arrays elements are: 1 2 3 4 5 5 4 6
123
C program count total duplicate
elements in array
#include<stdio.h>
#define size 100
int main(){
int i,j,count=0,a[size],n;
printf("total no of elements in an array");
scanf("%d",&n);
printf("array elements:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
count++;
break;
}
}
}
printf("no of duplicate elements in an array %d",count);
return 0;
}
Output:
total no of elements in an array5
array elements:1 2 3 2 1
no of duplicate elements in an array 2
}
}
for(i=0;i<n;i++){
printf("%d",a[i]);
}
return 0;
}
}
else if(i<n1){
c[k]=a[i];
i++;
}
else{
c[k]=b[j];
j++;
}
}
printf("final sorted array");
for(k=0;k<n1+n2;k++){
printf("%d ",c[k]);
}
return 0;
}
Output:
total no of elements in an array: 5
array elements are: 4 8 7 3 2
array elements in ascending order2 3 4 7 8
array elements in descending order8 7 4 3 2
}
int ec=0;
int oc=0;
for(i=0;i<n;i++){
if(b[i]%2==0){
e[ec]=b[i];
ec++;
}
else{
o[oc]=b[i];
oc++;
}
}
printf("\neven soryted elements:");
for(i=0;i<ec;i++){
printf("%d ",e[i]);
}
printf("\nodd sorted elements:");
for(i=0;i<oc;i++){
printf("%d ",o[i]);
}
}
OUTPUT:
no of elements in an array:10
array elements :6 5 7 4 8 3 9 2 10 1
Output:
array elements: 1 2 3 4 5 6 7 8 9 10
how many shifts: 5
array elements before rotation:1 2 3 4 5 6 7 8 9 10
array elements after rotation: 6 7 8 9 10 1 2 3 4 5
OUTPUT:
array elements: 1 2 3 4 5 6 7 8 9 10
how many shifts: 3
array elements before rotation:1 2 3 4 5 6 7 8 9 10
array elements after rotation: 8 9 10 1 2 3 4 5 6 7
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int c;
scanf("%d",&c);
int s=n/c;
int z=0;
int newarray[s][c];
printf("%d\n",s);
for(int i=0;i<s;i++)
{
for(int j=0;j<c;j++)
{
newarray[i][j]=arr[z];
z++;
}
}
for(int i=0;i<s;i++)
{
printf("[");
for(int j=0;j<c;j++)
{
printf("%d",newarray[i][j]);
if(j<c-1)
{
printf(",");
}
}
printf("]");
printf("\n");
}
int r=n%c;
if(r!=0)
{
printf("[");
for(int i=1;i<=r;i++)
{
printf("%d",arr[n-i]);
if(i<r)
printf(",");
}
printf("]");
}
}
OUTPUT:
8
1
2
3
4
5
6
7
8
3
2
[1,2,3]
[4,5,6]
[8,7]