Programs in Class Array
Programs in Class Array
Programs in Class Array
// Input phase
printf("How many numbers:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
// Output Phase
printf("Array contents are:");
for(i=0;i<n;i++)
{
printf("%d ", num[i]);
}
return 0;
}
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
return 0;
}
Q.3 Sum of elements of an array
#include <stdio.h>
int main()
{
int i,n;
printf("\n Enter the no of students");
scanf("%d",&n);
float marks[n],total=0;
printf("\n enter marks for %d students",n);
for(i=0;i<n;i++)
{
printf("\n Student %d :",i+1);
scanf("%f",&marks[i]);
total=total+marks[i];
}
printf("\n Sum of %d students = %f",n,total);
printf("\n Avg of %d students = %f",n,total/n);
return 0;
}
Q.4 Highest element in an array
#include <stdio.h>
int main()
{
int i,n;
printf("\n Enter the no of students: ");
scanf("%d",&n);
float marks[n], highest;
printf("\n enter marks for %d students: ",n);
for(i=0;i<n;i++)
{
printf("\n Student %d :",i+1);
scanf("%f",&marks[i]);
}
highest=marks[0];
for(i=0;i<n;i++)
{
if(marks[i]>highest)
highest=marks[i];
}
printf("\n Highest is = %f",highest);
return 0;
}
Q.5 Linear Search in an Array
#include <stdio.h>
int main()
{
int i,n;
printf("\n Enter the no of students: ");
scanf("%d",&n);
int marks[n], key, loc;
printf("\n enter marks for %d students: ",n);
for(i=0;i<n;i++)
{
printf("\n Student %d :",i+1);
scanf("%d",&marks[i]);
}
printf("\n enter the key element");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(marks[i]==key)
{
printf("\n Loc = %d",i);
break;
}
}
if(i==n)
printf("\n NOT FOUND");
return 0;
}
for(i=0;i<n;i++)
{
if(A[i]%2==0)
esum+=A[i]; // esum=esum+A[i];
else
osum+=A[i]; // osum=osum+A[i];
}
if(esum>osum)
printf("\n Even sum is bigger =%d",esum);
else if(osum>esum)
printf("\n ODD sum is bigger =%d",osum);
else
printf("\n Both are same");
return 0;
}
Q.8 Selection Sort in an Array
#include <stdio.h>
int main()
{
int n,i,j,min,temp;
printf("\n Enter the no of ele");
scanf("%d",&n);
int A[n];
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[min]>A[j]) min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
for(i=0;i<n;i++) printf(" %d ",A[i]);
return 0;
}
return 0;
}
Q.10 Binary (base 2) to Decimal (base 10) conversion using an Array (option 1)
#include <stdio.h>
#include<math.h>
int main()
{
int i,j,n,sum=0;
printf("\n Enter the no of bits");
scanf("%d",&n);
int A[n];
printf("\n Enter %d bits (0/1)",n); // 0-7 for octal, 0-15 for hexadecimal
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=n-1,j=0;i>=0,j<=n-1;i--,j++)
sum=sum+A[i]*pow(2,j); // 8 for octal, 16 for hexadecimal
printf("\n Decimal equivalent = %d",sum);
return 0;
}
Q.10 Binary to Decimal conversion without using an Array (option 2)
#include <stdio.h>
#include<math.h>
int main()
{
int n,rem,dec,i;
printf("Enter binary equivalent of number");
scanf("%d",&n);
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i); // 8 for octal, 16 for hexadecimal
++i;
}
printf("\n Decimal equivalent is %d",dec);
return 0;
}