0% found this document useful (0 votes)
12 views6 pages

Week7 CP Lab Programs

The document outlines Week 7's objective of exploring arrays, including defining and initializing 1-D and 2-D arrays, and performing linear searches. It includes suggested activities such as finding the minimum and maximum values in an array, reversing an array, calculating the 2's complement of a binary number, and eliminating duplicate elements. Additionally, it provides C programming examples for each activity, demonstrating the implementation of these concepts.

Uploaded by

shivakumar86639
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Week7 CP Lab Programs

The document outlines Week 7's objective of exploring arrays, including defining and initializing 1-D and 2-D arrays, and performing linear searches. It includes suggested activities such as finding the minimum and maximum values in an array, reversing an array, calculating the 2's complement of a binary number, and eliminating duplicate elements. Additionally, it provides C programming examples for each activity, demonstrating the implementation of these concepts.

Uploaded by

shivakumar86639
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WEEK 7:

Objective: Explore the full scope of Arrays construct namely defining and initializing 1-D and 2-D
and more generically n-D arrays and referencing individual array elements from the defined array.
Using integer 1-D arrays, explore search solution linear search.
Suggested Experiments/Activities:
Tutorial 7: 1 D Arrays: searching.
Lab 7:1D Array manipulation, linear search
i) Find the min and max of a 1-D integer array.
ii) Perform linear search on1D array.
iii) The reverse of a 1D integer array
iv) Find 2’s complement of the given binary number.
v) Eliminate duplicate elements in an array.

i.Write a C program to find the min and max of a 1-D integer array.

#include<stdio.h>
int main()
{
int i,n,a[50],max,min;
printf("enter the size of array:"); \\Input size of the array
scanf("%d",&n);
/* Input array elements */
printf(" Enter the values of array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
/* Assume first element as maximum and minimum */
max=a[0];
min=a[0];
\\Find maximum and minimum in all array elements.
for(i=0;i<n;i++)
{ \\ If current element is greater than max
if(a[i]>max)
{
max=a[i];
}
\\If current element is smaller than min
if(a[i]<min)
{
min=a[i];
}
}
printf("\nThe maximum number in given array is %d",max);
printf("\nThe minimum number in given array is %d",min);
return 0;
}

Input:
Enter the size of array: 10
Enter the values of array: 10 12 30 14 45 6 17 1 29 50
Output: The maximum number in given array is 50
The minimum number in given array is 1

ii.Write a C program to perform linear search on a 1D array.

#include<stdio.h>
int main()
{
int a[50],i,n,key,flag=0;
printf("Enter the size of array:");
scanf("%d",&n);

printf("Enter %d values of array\n", n);


for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
printf("Enter the number to search:");
scanf("%d",&key);
for(i=0;i<n;i++)
{ if(key==a[i])
{ flag=1;
break;
}
}
if(flag==1)
{ printf(" Search element %d is present at index %d", key,i);
}
else
{ printf(" Search element %d is not present in array", key);
}
return 0;
}

Input:
Enter the size of array: 10
Enter 10 values of array: 4 9 2 1 5 7 10 8 3 6
Enter the number to search: 10
Output: Search element 10 is present at index 6

iii.Write a C program on the reverse of a 1D integer array?

#include<stdio.h>
int main()
{ int i,n,a[100];
printf(" the size of an array :"); //Input size of the array
scanf("%d",&n);
printf("enter elements into an array:");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
printf("Array elements are\n :");
for(i=0;i<n;i++)
{ printf("%d\t",a[i]);
}
printf("\n Array elements in reverse order:");
for(i=n-1;i>=0;i--)
{ printf("%d\t",a[i]);
}
return 0;
}

Input:
Enter the size of array: 10
Enter 10 values of array: 1 2 3 4 5 6 7 8 9 10
Output: Array elements are : 1 2 3 4 5 6 7 8 9 10
Array elements in reverse order: 10 9 8 7 6 5 4 3 2 1

iv) Find 2’s complement of the given binary number.


2's complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1, thus 2's complement of
11100 is 00100.
● Traverse binary bits from right to left
● Find the first 1 bit
● Reverse every bit after first 1’s

#include<stdio.h>
int main()
{
int a[10],i,n,flag=0;
printf("Enter no of bits \n");
scanf("%d",&n);

printf("Enter binary numbers consists of either 1 or 0 \n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf(" Given binary number is \n");
for(i=0;i<n;i++)
printf("%d",a[i]);
for(i=n-1;i>=0;i--)
{
if(a[i]==1 && flag==0)
{ flag=1;
}
else if(flag==1)
{
if(a[i]==1)
a[i]=0;
else
a[i]=1;
}
}

printf("\nThe 2’s complement form is ");


for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
Input:
Enter number of bits: 5
Enter binary numbers consists of either 1 or 0 :
10100

Output: Given binary number is : 10100


The complement form is 01100

V. Write a C program to eliminate duplicate elements in an array.

#include <stdio.h>
int main ()
{
int a[100], i, j, k, n;
printf (" Enter array size: ");
scanf ("%d", &n);
printf(" Enter elements of an array: ");
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]) //to check duplicate element
{ // delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
n--; // decrease the size of array after removing duplicate element
j--;//if the position of the elements is changes, don't increase the index j
}
}
}
printf (" Array elements after deletion of the duplicate elements: \n");
for ( i = 0; i < n; i++)
{
printf (" %d \t", arr[i]);
}
return 0;
}
Input:
Enter array size: 10
Enter elements of an array: 1 2 2 3 4 2 3 4 5 7
Output: Array elements after deletion of the duplicate elements: 1 2 3 4 5 7

You might also like