Practical 7.1

Posted: January 23, 2012 in Lab 3

Problem statement- Implement a program that reads in a list of integers (up to 20 of them), and then performs a sequence of operations on them. The minimum operations that your program must perform are as follows:
• Sum: print out the sum of the integers in your array.
• Print: print out the array
• Print backward: print out the array in reverse order
• Reverse: reverse the array
• Drop first: remove the first element of the array

#include<stdio.h>
int main()
{
int arr[20],rev[20],i,j=0,s=0,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
//sum
for(i=0;i<n;i++)
s=s+arr[i];
printf("sum=%d\n",s);
//print
printf("Array is= ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
//print backward
printf("\nPrint backward\n");
for(i=(n-1);i>=0;i--)
printf("%d ",arr[i]);
//reverse array
for(i=(n-1);i>=0;i--)
rev[j++]=arr[i];
printf("\nReverse\n");
for(i=0;i<n;i++)
printf("%d ",rev[i]);
//drop first
for(i=0;i<(n-1);i++)
arr[i]=arr[i+1];
printf("\nAfter dropping first element\n");
for(i=0;i<(n-1);i++)
printf("%d ",arr[i]);
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s