Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Programs in Class Array

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programs in Class Array

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1 DIMENSIONAL ARRAY

Q.0 Sum & average of n numbers

// General structure of data processing with arrays


#include<stdio.h>
int main()
{
int num[10],n,i;
int sum = 0;

// Input phase
printf("How many numbers:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}

// Data processing phase


for(i=0;i<n;i++)
{
sum = sum+num[i];
}

// Output Phase
printf("Array contents are:");
for(i=0;i<n;i++)
{
printf("%d ", num[i]);
}

printf("\nSum = %d Average = %.2f\n",sum,(float)sum/n);

return 0;
}

Q.1 Input and output in an array


#include <stdio.h>
int main()
{
int marks[5],i;
printf("\n enter marks for 5 students");
for(i=0;i<5;i++)
{
scanf("%d", &marks[i]);
}
printf("\n Here are marks for 5 students");
for(i=0;i<5;i++)
{
printf("\n %d",marks[i]);
}
return 0;
}
Q.2 Input and print the array in reverse order
#include <stdio.h>
int main()
{
int n=5;
int i,arr[n];
printf("\n Enter 5 elements");

for(i=0;i<n;i++)
scanf("%d",&arr[i]);

// printing in reverse order


for(i=n-1;i>=0;i--)
printf("%d \t",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;
}

Q.6 Binary search in an Array


#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter %d integers:", n);
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
printf("\n Enter value to find: ");
scanf("%d", &key);
low = 0;
high = n - 1;

while (low <= high)


{
mid = (low + high)/2;
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("\n %d found at location: %d", key, mid+1);
break;
}
else
high = mid - 1;
}

if(low > high)


printf("\n Not found! %d isn't present in the list", key);
return 0;
}

Q.7 EvenSum and OddSum in an Array


#include <stdio.h>
int main()
{
int n,i,esum=0,osum=0;
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++)
{
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;
}

Q.9 Decimal (base 10) to Binary (base 2) conversion using Array


#include <stdio.h>
#include<math.h>
int main()
{
int n,A[32]={0},i,j;
printf("\n Enter any decimal number");
scanf("%d",&n);
while(n>0)
{
A[i]=n%2; // 8 for octal, 16 for hexadecimal
n=n/2; // 8 for octal, 16 for hexadecimal
i++;
}
printf("\n Decimal equivalent =");
for(j=i-1;j>=0;j--)
printf("%d",A[j]);

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;
}

You might also like