0% found this document useful (0 votes)
4 views

Array Working Questions

The document outlines a series of programming tasks in C involving array manipulation, including initialization, summation, checking for elements, arithmetic sequences, reversing arrays, circular shifting, and calculating medians. Each task specifies the creation of arrays, random initialization, and various operations to be performed on the arrays, such as printing, summing, and sorting. Additionally, it includes function prototypes for modular programming and emphasizes the use of random number generation for certain operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array Working Questions

The document outlines a series of programming tasks in C involving array manipulation, including initialization, summation, checking for elements, arithmetic sequences, reversing arrays, circular shifting, and calculating medians. Each task specifies the creation of arrays, random initialization, and various operations to be performed on the arrays, such as printing, summing, and sorting. Additionally, it includes function prototypes for modular programming and emphasizes the use of random number generation for certain operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Assume that “A” is an integer array.

i) Define the “A” array that stores 10 integer numbers (i.e. A[10]), then initialize the array
by using initialization method such that each element is 1-digit (0-9) number randomly.

ii) Print the array.

iii) Select a number between 9 and 19 by using rand() function. Print it.

iv) Sum the items of the array. If the sum is less than selected number, print “Win”.
Otherwise, print “Lost”.

2) Write a C program to calculate the equation as follows:

-(0 × 𝑎[0]) − (1 × 𝑎[1]) − (2 × 𝑎[2]) − ⋯ − (9 × 𝑎[9])

i) Define the “a” array that stores 10 integer numbers (i.e. a[10]), then initialize the array
by using initialization method such that each element is 1-digit (0-9) number randomly.

ii) Print the array.

iii) Calculate the result of the equation.

iv) Print the result of the calculation.

3) Write a C program to check the array if it contains a number or not.

i) Define an array that stores 20 integer numbers (i.e., A[20]), then initialize the array
such that each element is 1-digit (0-9) number randomly.
ii) Print the array.
iii) Prompt a number from the user that is between 0 and 19. Print it.
iv) Check the array if it contains the number. If it is, print “the array contains the
number”, otherwise print “the array does not contain the number”.

4) In mathematics, an arithmetic sequence is a sequence of numbers such that the difference


between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 … is an
arithmetic progression with common difference of 2. If the initial term of an arithmetic sequence
is 𝑎1 and the common difference of successive members is 𝑑, then the 𝑛𝑡ℎ term of the sequence
𝑎𝑛 is calculated as follows:

𝑎𝑛 = 𝑎1 + 𝑑 ∗ (𝑛 − 1)
i) Define an array that stores 20 integer numbers (i.e., numbers[20]), then initialize the
array to zero.
ii) Print the array.
iii) Prompt the initial term 𝑎1 and common difference 𝑑. Print them.
iv) Calculate the the series and store it in array numbers.
v) Print the array.
vi) Test your program with 𝑎1 = 7 and 𝑑 = 4.

5) Reversing is an array operation that reverse all items of the array. An example is given below:

Assume that array items are 3 7 10 5 4 1 6 9 2 8

After reversing the array becomes 8 2 9 6 1 4 5 10 7 3

The difference between the original and reversed array is -5 5 1 -1 3 -3 1 -1 -5 5. Write a C


program to determine how many negative items are in difference array.

i) Define an array that stores 10 integer numbers (i.e., numbers[10]), then initialize and
print the array such that each element must be in the interval [7-24] number
randomly. Also, define integer arrays for reversed and difference arrays (i.e.,
reversed[10] and diff[10]) and initialize them to zero.
ii) The program must include the reverse function that receives the numbers and
reversed arrays. Then, reversing the original array and store it to reversed array.
(Prototype: void reverse(const int original[], int reversed[], int size)

iii) The program must include the difference function that receives the numbers, reversed,
and diff arrays. Then, take difference between original and reversed array and store
the difference in diff array. Return how many negative items in diff array (Prototype:
int difference(const int original[], const int reversed[], int diff[], int size).
iv) In main function, print the result, reversed and diff arrays.

6) Circular shifting is an array operation that shifts all items according to its operand. An example
is given below:

Assume that array items are 3 7 10 5 4 1 6 9 2 8 and shift operand is 3.

After circular shifting, the array becomes 5 4 1 6 9 2 8 3 7 10. The first 3 items according to its
operand are moved to the last 3 position.
Write a C program to operate circular shifting.

i) Define an array that stores 10 integer numbers (i.e., numbers[10]), then initialize and
print the array such that each element must be in the interval [47-94] number
randomly. Also, define an integer array for shifted array (i.e., shifted[10]) and
initialize it to zero.
ii) Select the operand between 1 and 4 by using rand function.
iii) The program must include the shift function that receives the operand, the numbers
and shifted arrays. Then, shifting the original array and store it to shifted array.
(Prototype: void shift(const int numbers[], int shifted[],int size, int operand)
iv) In main function, print numbers and shifted arrays.

7) Assume that array items are 37 24 27 35 33 32 28 16 34 36 30 21 29 22 23

Sort in ascending order 16 21 22 23 24 27 28 29 30 32 33 34 35 36 37

Separate sorted array as taking the first 3 elements, then second 3 element ... as follows:

16 21 22 | 23 24 27 | 28 29 30 | 32 33 34 | 35 36 37 and determine middle values.

21 24 29 33 36

Write a C program to calculate the mean of the 3-median of the array.

i) Define an array that stores 15 integer numbers (i.e., numbers[15]), then initialize the
array for the data given above by using the initializer list method. Also, define
integer array 3-median (i.e., med[5]) and initialize it to zero.
ii) The program must include the sort function that receives the numbers array. Then,
sorting the original array. (Prototype: void sort(int numbers[],int size)
iii) The program must include the median function that receives the numbers and med
arrays, then returns the mean of the 3-median of the array. In function, find 3-median
values of the array and store it to med array. Then calculate the mean of these five
values as follows: (Prototype: double median(const int numbers[], int med[],int
numbers_size, int med_size).

𝑚𝑒𝑑[0] + ⋯ + 𝑚𝑒𝑑[𝑛]
𝑚𝑒𝑎𝑛 =
𝑛
i) In main function, print the result, numbers, and med arrays.

You might also like