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

1-D Array Functions and Exercises

The document outlines exercises related to 1-D arrays and functions, including array declaration, initialization, data entry, and element access. It provides sample programs for finding the largest and smallest elements in an array, counting frequency, performing linear search, sorting, and merging arrays. Additionally, it includes viva questions and assignment marks tracking.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

1-D Array Functions and Exercises

The document outlines exercises related to 1-D arrays and functions, including array declaration, initialization, data entry, and element access. It provides sample programs for finding the largest and smallest elements in an array, counting frequency, performing linear search, sorting, and merging arrays. Additionally, it includes viva questions and assignment marks tracking.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exercise 9 Start Date

To demonstrate use of 1-D arrays and functions.


You should read the following topics before starting this exercise
1. What are arrays and how to declare an array?
2. How to enter data in to array and access the elements of an array.
3. How to initialize an array and how to check the bounds of an array?
4. How to pass an array to a function
An array is a collection of data items of the same data type referred to by a common
name. Each element of the array is accessed by an index or subscript. Hence, it is also
called a subscripted variable.

Actions involving syntax Example


arrays
Declaration of array data-type array_name[size]; int
temperature[10];
float pressure[20];
Initialization of data-type int
array array_name[]={element1, marks[]={45,57,87,20,90};
element2, ……, element n};
marks[3] refers to the fourth
data-type element which equals 20
array_name[size]={elem
ent-1, element-2, ……, int count[3]={4,2,9};
element-size}; count[2] is the last element
9 while 4 is count[0]

You cannot give more number


of initial values than the array
size. If you specify less values,
the
remaining will be initialized to
0.
Accessing elements The array index begins from 0 Value = marks[3];
of an array (zero) To access an array
element, we need to refer to it This refers to the 4th element
as array_name[index]. in the array

Entering data into for (i=0; i<=9; i++)


an array. scanf(“%d”,
&marks[i]);
Printing the data for(i=0; i<=9; i++)
from an array
printf(“%d”,
marks[i]);
Arrays and function We can pass an array to a /* Passing the whole array*/
function using two methods. void modify(int a[5])
Pass the array element by {
element
Pass the entire array to int i;
the function for(i=0; i<5 ; i++)
a[i] = i;
}

Sample program to find the largest element of an array


1. Write a program to accept n numbers in an array and display the largest and smallest number.
Using these values, calculate the range of elements in the array. Refer to the sample code given
above and make appropriate modifications.

2. Write a program to accept n numbers in an array and calculate the average. Refer to the
sample code given above and make appropriate modifications.

Set A. Write programs to solve the following problems


1. Write a program to accept n numbers in the range of 1 to 25 and count the frequency of
occurrence of each number.

2. Write a function for Linear Search, which accepts an array of n elements and a key as
parameters and returns the position of key in the array and -1 if the key is not found.
Accept n numbers from the user, store them in an array. Accept the key to be searched
and search it using this function. Display appropriate messages.

3. Write a function, which accepts an integer array and an integer as parameters and
counts the occurrences of the number in the array.

4. Write a program to accept n numbers and store all prime numbers in an array called
prime. Display this array.

Signature of the instructor Date

Set B. Write programs to solve the following problems

[Link] a program to accept n numbers from the user and store them in an array such that the
elements are in the sorted order. Display the array. Write separate functions to accept and display
the array. (Hint: Insert every number in its correct position in the array)

2. Write a function to sort an array of n integers using Bubble sort method. Accept n numbers
from the user, store them in an array and sort them using this function. Display the sorted array.

3. Write a program to accept a decimal number and convert it to binary, octal and hexadecimal.
Write separate functions.

4. Write a program to find the union and intersection of the two sets of integers (store it in two
arrays).

5. Write a program to remove all duplicate elements from an array.


Signature of the instructor Date

Set C. Write programs to solve


the following problems

[Link] a program to merge two sorted arrays into a third array such that the third array
is also in the sorted order.

a1 10 25 90
a2 9 16 22 26 10
0
a3 9 10 16 22 25 26 90 100

[Link] a program to accept characters from the user till the user enters EOF and
calculate the frequency count of every alphabet. Display the alphabets and their count.
Input: THIS IS A SAMPLE INPUT
Output: Character Count
T 2
H 1
I 3
…….
3. Write a recursive function for Binary Search, which accepts an array of n elements and a key
as parameters and returns the position of key in the array and -1 if the key is not found. Accept n
numbers from the user, store them in an array and sort the array. Accept the key to be searched
and search it using this function. Display appropriate messages

Signature of the instructor Date

Viva Questions:
Sr. No. Question Asked Marks(1/0)
1

Assignment 2: Total Marks (Out of 10)

Signature of the instructor Date

You might also like