Follow Me

One dimensional array exercises in C Programming in C

  • Reading and displaying an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array....
  • Sum of elements in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Calculate and then display the sum of the elements of the array....
  • Delete all zeros from an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Calculate and then display the sum of the elements of the array. Then del...
  • Reversing an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Then store the elements of T in the reverse order without using an auxili...
  • Subdividing an array into 2 positive and negative arrays: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Then copy all strictly positive components into a second TPOS array and a...
  • Distinct elements in an array: Let X be an array containing n positive integers. Write a program that creates: An array Dist containing the distinct elements of X An array Effec containing the number of occurrence of each distinct element of X An integer k containing the number o...
  • Delete an element from an array: Write a program that deletes the first occurrence of an element in an array Example A={0, 1, 0, 1, 2, 3, 1} Delete the first occurrence of 0 A={1, 0, 1, 2, 3, 1} ...
  • Storing the reciprocal of N double values: Write a program that will read N values of type double from the keyboard andstore the reciprocal of each value (the reciprocal of a value x is 1.0/x) in an array. Display the array....
  • Summing 100 data values (approximation of pi): Define an array, data, with 100 elements of type double. Write a loop that will store the following sequence of values in corresponding elements of the array:1/(2*3*4) 1/(4*5*6) 1/(6*7*8) … up to 1/(200*201*202) Write another loop that will calcula...
  • Count number of common elements in 2 sorted arrays: Write a program that counts the number of common elements in 2 sorted arrays...
  • Mean and variance of an array: Write a program that reads an array X of size N and calculates that mean and the variance. \(\mu=\frac{\sum_{i=0}^{N-1}X[i]}{N}\) \(\sigma^2=\frac{\sum_{i=0}^{N-1}(X[i]-\mu)}{N}\)  ...
  • Duplicate elements in an array: Write a program that reads an array and finds and counts the total number of duplicate elements....
  • Second maximum in an array: Write a program that reads an array and finds the second maximum element in the array....
  • Sum of 2 elements close to zero: Write a program that finds two elements in an array of ineteger whose sum is closest to zero....
  • Sorting using insertion algorithm: Sort the elements of an array A in ascending order. Method: Every repetition of insertion sort, removes an element from the input data, inserts it into the correct position in the already sorted list until no input elements remain. Sorting is typical...
  • Finding a value in an array using jump search: Search an array A of integers of size N for a VAL value entered on the keyboard. Display the position of VAL if it is in the array, otherwise display a corresponding message. The INDEX value that is used to memorize the position of the value in the a...
  • Finding a value in an array using Interpolation search: Search an array of integers for a VAL value entered on the keyboard. Display the position of VAL if it is in the array, otherwise display a corresponding message. The INDEX value that is used to memorize the position of the value in the array, will h...
  • Lagrange form of the polynomial interpolation: Suppose we are given a set of tabulated values for y versus x, i.e.,\(\begin{array}{c c c c c} y_0 & y_1 & y_2 & \cdots & y_n\\x_0 & x_1 & x_2 & \cdots & x_n\end{array}\)and we wish to obtain a value of y at some speci...
  • Potential energy of N electric charges: We consider N electric charges of values \(q_i\) located in a plane at the points \(Q_i\) of coordinates \(x_i, y_i\).The \(q_i, x_i\) and \(y_i\)  values are stored in a one dimensional array. The potential energy of these N charges is: \(\frac{1}{4\pi\epsilon_0}\sum_{i=0}^{i=N-1}\sum_{j>i}^{j=N-1}\frac{q_iq_j}{|\overrightarrow{Q_iQ_j}|}\)...
  • Decimal value of a binary number stored in an array: A binary number is a sequence of 0 or 1 (called bit). It is coded by an array b of fixed size LENGTH where each element is equal to 0 or 1. The first bit of the left of the binary number is the element b[0] of b. The corresponding decimal value is ob...
  • Merge two arrays into a third sorted array: We have two arrays A and B (of respective dimensions N and M). Merge the elements of A and B into a third FUS array sorted in ascending order. Method: we will loop over each array and insert the element in a sorted way in the FUS array....
  • Print elements of an array just once: Write a program that reads an array and print all elements in array just once (i.e. duplicates elements must be printed just once)...
  • Print unique elements of an array: Write a program that reads an array and print just unique elements in the array (i.e. duplicates elements must not be printed)...
  • Count and print duplicate elements in an array: Write a program that reads an array and counts and prints duplicate elements in the array....
  • Delete duplicate elements in an array: Write a program that reads an array and deletes duplicate elements in an array....
  • Left rotate an array: Write a program that reads an array and rotates it to left by n positions....
  • Right rotate an array: Write a program that reads an array and rotates it to right by n positions....
  • Prime factors: Write functions that : reads and return a positive integer checks whether an integer is prime or not generates the first N primes numbers generates the prime factors of an integer ...
  • Perfect numbers: Write the function perfect that checks if a number is perfect or not. The function returns 1 if the number is perfect and 0 otherwise. Note that an integer N is considered perfect if the sum of the divisors of N, excluding the number N itself, is equ...
  • Maximum of first half and the minimum of second half of an array: Write a C function DisplayArray that takes as parameter an array and two index m and n. This function should display all arrays’ elements that are between these indexes. Write a C function MaxMin that takes as parameter an array of integers and its...
  • Brother numbers: Two numbers N1 and N2 are said to be brothers, if each digit of N1 appears at least one time in N2, AND each digit of N2 appears at least one time in N1. Examples: N1 = 1162 and N2 = 612, are brothers. N1 = 905 and N2 = 9059, are bothers. N1 = 405 an...
  • Zeroing duplicated elements in an array: Write a main program that declares and reads an array of  integers, and then sets to 0 the duplicated appearance of all its elements. (elements are different than 0) Example: If the entered array is:1, 7, 1, 3, 5, 1, 7The array should become 1, 7, ...
  • Inverse summation of 2 arrays: Write a program that: reads the effective dimension \(\texttt{N}\) of an array (maximum dimension: 50 components); then fills two arrays \(\texttt{A}\) and \(\texttt{B}\) each with \(\texttt{N}\) integers entered on the keyboard; then fills a third a...
  • Kth largest element in an array: Write the function \(\texttt{int READDIM()}\) that reads and returns a strictly positive integer less than 50; Write the function \(\texttt{void READARRAY(int Arr[], int N)}\) that reads \(\texttt{N}\) positive integers of the array \(\texttt{Arr}\);...
  • Reverse the order of appearance of elements in an array: Write the function int READDIM() that reads and returns a strictly positive integer less than 50; Write the function void READARRAY(int Arr[], int N) that reads N integers of the array Arr; Write the function void DISPLAY(int Arr[], int N) that displ...
  • Scalar product of two vectors: Write a program that calculates the scalar product of two integer vectors U and V (of the same dimension). \((3\ \ 2\ -4) \times (2\ -3\ \ 5) = 3 \times 2 + 2\times(-3) + (-4)\times 5 = -20 \)...
  • Calculation of a Polynomial of degree N: Calculate for a given value X of the type float the numerical value of a polynomial of degree n: \(P(X) = A_nX^n + A_{n-1}X^{n-1}+ \cdots + A_1X + A_0\) The values of n, coefficients \(A_n, \cdots , A_0\) and X will be entered on the keyboard and sto...
  • Maximum and minimum values of an array: Write a program that determines the largest and the smallest value in an array of integers A. Next, display the value, the number of appearance and the position of the maximum and minimum values. If the array contains several maxima or minima, the pr...
  • Insert a value into a sorted array: An array A of dimension N + 1 contains N integer values sorted in ascending order; the (N + 1) th value is undefined. Insert a given VAL value read from the keyboard in the array A to obtain a sorted array of N + 1 values. Example: A={7, 9, 13, 88, 1...
  • Finding a value in an array using sequential search: Search an array of integers for a VAL value entered on the keyboard. Display the position of VAL if it is in the array, otherwise display a corresponding message. The INDEX value that is used to memorize the position of the value in the array, will h...
  • Finding a value in an array using binary search: Search an array of integers for a VAL value entered on the keyboard. Display the position of VAL if it is in the array, otherwise display a corresponding message. The INDEX value that is used to memorize the position of the value in the array, will h...
  • Merge two sorted arrays: We have two arrays A and B (of respective dimensions N and M), sorted in ascending order. Merge the elements of A and B into a third FUS array sorted in ascending order.   Method: Use three indexes i, j and k. Compare A[i] and B[j]; replace FUS[k] w...
  • Sorting an array by minimum selection: Sort the elements of an array A in ascending order. Method: Traverse the array from left to right using the index I. For each element A[I] of the array, determine the PMIN position of the (first) minimum to the right of A[I] and exchange A[I] and A[P...
  • Sorting by propagation (bubble sort): Sort the elements of an array A in ascending order. Method: Starting again each time at the beginning of the table, we carry out several times the following treatment: We propagate, by successive permutations, the largest element of the array towards...
  • Sum and product of elements in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Calculate and then display the sum and the product of the elements of the...
  • Number of occurrence of an element in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. The user than enters a value V and the program must indicate the number o...
  • Check if two arrays are identical: Write a program that reads the dimension N1 and N2 of two integer arrays T1 and T2 (maximum dimension: 50 components), fills the arrays with values entered on the keyboard and displays the array. The program then indicates if these two arrays are ide...
  • Replace elements in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Then replace all 1 and 100 values by 0 and display the array....
  • Display values having even indexes in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array elements having even indexes....
  • Display values having indexes multiple of 5 in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array elements having indexes multiple of 5....
  • Sorting seven numbers: Write a program which reads an array of 7 integers then represents them in an increasing order....
  • Rearranging values in an array: Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array. Then re-arrange the values as follows: Odd integers first, then even inte...
  • Display some elements in an array: Write a program which fills an array of integers of size 15. This program must show the elements of the array which are multiple of 7....
  • Reading elements of an array with a special condition: Write a program which declares an array of integers of size maximum size 20, asks the user to enter its effective size (should be less than 20) and allows the user to fill his values. This program should not accept values greater than 150. In other w...
  • Convert a binary number to decimal using arrays: Write a function that initializes each element $$tab[i]$$ of a tab array to the value $$2^i$$. Using  the first question, write a program that reads characters equal to 0 or 1 from the keyboard and calculates the number that these digits represent i...
  • Read the dimension and the elements on an array using an iterative function: Write the function READARRAY with 2 parameters TAB and NMAX reads and returns the dimension N and the components of a table TAB of the type int. The dimension N must be less than NMAX. Implement the function READARRAY by choosing the type of the para...
  • Display the elements on an array using an iterative function: Write the DISPLAYARRAY function with two parameters TAB and N which displays N components of the array TAB of the type int. Example: The table T read in the previous exercise will be displayed by the call:       DISPLAYARRAY(T, N); and will be p...
  • Sum of the elements of an array using an iterative function: Write the function SUM_TAB which calculates the sum of the N elements of an array TAB of the type int. N and TAB are provided as parameters; the sum is returned as the result of the long type....
  • Calling the sum function on an array: Using the functions of the previous exercises, write a program that reads an array A with a dimension less than or equal to 100 and displays the array and the sum of the elements of the array....
  • Average of an array: Write a function that returns the average of the grades given in an array....
  • Checking whether two arrays contain the same number of prime numbers: Write the function prime that checks if a number is prime or not. The function returns 1 if the number is prime and 0 otherwise. Write a function ReadArray that allows to read the value of an array of size M. All the elements of the array should be b...
  • Symmetric array: Write a function ArrayEqual that takes two arrays A1 and A2 of the same size and checks if the two arrays are equal. Two arrays are equal when their same position elements are equal. Example:A1 = {2,4,5,1} and A2 = {2,4,5,1} are equalA1 = {2,4,5,1} a...
  • Maximum element of an array using an iterative function: Determine the maximum of N elements of a TAB array of integers....
  • Index of the maximum element of an array using an iterative function: Determine the index of the maximum of N elements of a TAB array of integers....
  • Insert an element into a sorted array using an iterative function: Write the INSERT function which places an X element inside an array that contains N elements sorted in ascending order, so as to obtain an array with N + 1 elements sorted in ascending order. The dimension of the array is incremented in the INSERT fu...
  • Merge 2 sorted arrays using an iterative function: Write the FUSION function that constructs a FUS array sorted in ascending order with the elements of two arrays A and B sorted in ascending order. For two arrays of dimensions N and M, the table FUS will have the dimension N + M. Write a program that...
  • Maximum distance between points: A point is defined by its real coordinates (X, Y). The purpose of this program is to allow the user to enter N distinct points, calculates and prints the smallest distance between any two points of the sequence. Two arrays should be used to store the...
  • Program output: Give the output on the screen of the following program (indicate a space using this symbol _): #include<stdio.h>double mystery(int A[], int i){     i = A[i] * 2;     A[0] = 0;     return i+1;} int main(){     int i = 8, j = 5, k, A[...
  • Dot product of two unequal-length vectors: We are interested to perform the dot product for unequal-length vectors. Algebraically, the dot product is the sum of the products of the corresponding entries of the two sequences of numbers. The dot product of two unequal-length vectors $A = [a_0, ...
  • Program output 13: Give the output on the screen of the following program (indicate a space using this symbol _): #include<stdio.h>double mystery(int T[], int i){     int k;      for(k=0 ; k<i ; k++)          T[k] -= T[i] - k;     return T[k] + ...
  • Array manipulation using functions: Write the function $\texttt{int READDIM()}$ that reads and returns a strictly positive integer $N$ less than 50; Write the function $\texttt{void READARRAY(int T[], int N)}$ that fills $N$ positive integers in the array $\texttt{T}$; Write the functi...
  • The missing value: We have an array $T[N]$ containing all integers in the interval $0 \cdots N$, except one integer. We want to determine which integer is missing from $T$ using multiple approaches.  Example: for the following array $T$ of size $N=6$, the missing valu...
  • Minimum value of an array using recursive function: Write a recursive function that returns the minimum of an array of integers.Prototype: int min(int tab[], int tab_size)Write a main function to test your program....
  • Sum of the first N value of an array using recursive function: Write a recursive function that returns the sum of the first N integers of an array.Write a main function to test your program....
  • Minimum and maximum values of an array using recursive function: Write a recursive function that returns the minimumand the maximum values of an array of integers.Write a main function to test your program....
  • Checking whether all the elements of an array belong to another array: Write a recursive function that determines whether all the elements of an integer arrayT1 belong to another integer array T2....
  • Checking whether an array is included in block in another array: Write a recursive function that determines whether an integer array T1 is includedin block in another array of integers T2. It is advisable to write another recursive function that determines whether an integer array T1 begins (is in block at the beg...
  • Checking whether a specific element is in an array: Write a recursive function testing whether a specific element is in an array.Prototype : int in_array(int tab[], int N, int element)Write a main function to test your program....
  • Calculating the number of occurrences of an element in an array: Write a recursive function that returns the number of occurrences of an element in anarray.Prototype : int nb_occurrence(int tab[], int N, int value)Write a main function to test your program....
  • Mirroring the elements in an array: Write a recursive function that mirrors the elements in an array.Prototype : void mirror(int tab[], int start, int end)Write a main function to test your program....
  • Sorting using merge-sort algorithm: Sort the elements of an array A in ascending order. Condition Merge 2 arrays (each of 1 element) Divide the non sorted array until the condition is verified and sort the divided parts into pairs. Precisely: Recursively divide the non sorted array in...
  • Sorting using heap-sort algorithm: Sort the elements of an array A in ascending order. Heapsort algorithm inserts all elements into a heap, then removes them from the root of a heap until the heap is empty. Sort can be done in place with the array to be sorted.Instead of deleting an e...
  • Sorting using quick-sort algorithm: Sort the elements of an array A in ascending order. Quick-sort uses recursive calls for sorting the elements. Is an example for divide-and-conquer algorithmic technique.Divide: the array A[low … high] is partitioned into two non-empty sub arrays A[...
  • Sorting using the counting sort algorithm: Sort the elements of an array A in ascending order. According to wikipedia, the counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by ...
  • Sorting using the radix sort algorithm: Sort the elements of an array A in ascending order. According to wikipedia, the radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant po...
  • Static implementation of a set - version 1: You are asked to give a static implementation of the ADT set having the following functions: void toempty(SET *S) int empty(SET S) int find(int x, SET S) void add(int x, SET *S) void removeS(int x, SET *S) void intersect(SET S1, SET S2, SET *R) void ...
  • Static implementation of a set - version 2: You are asked to give a static implementation of the ADT set having the following functions: void toempty(SET *S) int empty(SET S) int find(int x, SET S) void add(int x, SET *S) void removeS(int x, SET *S) void intersect(SET S1, SET S2, SET *R) void ...
  • Implementation of a set using an array of Boolean: You are asked to give an implementation of the ADT set using an array of Boolean and having the following functions: void toempty(SET *S) int empty(SET S) int find(int x, SET S) void add(int x, SET *S) void removeS(int x, SET *S) void intersect(SET ...
  • Implementation of a set using a long integer as bits: You are asked to give an implementation of the ADT set using an array of bits and having the following functions: void toempty(SET *S) int empty(SET S) int find(int x, SET S) void add(int x, SET *S) void removeS(int x, SET *S) void intersect(SET S1,...
  • Implementation of a set using an array of long integers as bits: You are asked to give an implementation of the ADT set using an array of bits and having the following functions: void toempty(SET *S) int empty(SET S) int find(int x, SET S) void add(int x, SET *S) void removeS(int x, SET *S) void intersect(SET S1,...
  • Disjoint sets - Well known union find algorithm: A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint subsets. The union-find algorithm is an efficient and fast algorithm that performs many useful operations: findSet: Determin...
  • Nearest common ancestors in binary trees: Write a function that returns the value of the father of a given element in a binary tree of integers without repetition. Write a function that returns an array containing the values of all ancestors of a given element in a binary tree of integers wi...
  • Sorting an array using two stacks: We want to sort an array of $N$ integers in decreasing order using a stack. To do this, you should use two stacks: a current stack ($\texttt{$S_1$}$) and an auxiliary stack ($\texttt{$S_2$}$) that will be used temporarily for eventual pops.  Write a...

Back to the list of exercises