0% found this document useful (0 votes)
14 views21 pages

Array Exercises

The document contains a series of programming exercises focused on array manipulation in C, including tasks such as finding the largest element, deleting elements, calculating averages, and handling duplicates. Each exercise is accompanied by sample code implementations that demonstrate the required functionality. The exercises progressively cover both one-dimensional and two-dimensional arrays, culminating in more complex operations and user interactions.

Uploaded by

uyennhi292006
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)
14 views21 pages

Array Exercises

The document contains a series of programming exercises focused on array manipulation in C, including tasks such as finding the largest element, deleting elements, calculating averages, and handling duplicates. Each exercise is accompanied by sample code implementations that demonstrate the required functionality. The exercises progressively cover both one-dimensional and two-dimensional arrays, culminating in more complex operations and user interactions.

Uploaded by

uyennhi292006
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
You are on page 1/ 21

Practice Exercises – Chapter: 07

* Exercise 7.1
Write a program that asks for the number of hours worked by six employees. It stores the
values in an array.

* Exercise 7.2: Find the largest element in the array


Write a program that allows declaring an integer array, the number of elements (size of
the array) and values for its elements. Then find and display the largest element of the
array.
#include<stdio.h>
#include<math.h>

int maxElement(int a[], int n){


int max = a[0];
for(int i = 1; i < n; ++i){
if(a[i] > max)
max = a[i];
}
return max;
}

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
int max = maxElement(a,n);
printf("%d", max);
return 0;
}
* Exercise 7.3: Delete an element in an array
Write the program to ask to the user to enter the array size and array elements, and then
ask the user to enter the position (not index) to delete the element. Display the elements
of the array after deletion.
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void deleteElement(int* a, int* n, int position){


for(int i = position - 1; i < *n - 1; i++){
a[i] = a[i+1];
}
(*n)--;
}
int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
int position;
scanf("%d", &position);
deleteElement(a,&n,position);
for(int i = 0; i < n; ++i){
printf("%d ", a[i]);
}
return 0;
}

Cách 2:
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void deleteElement(int* a, int* n, int position){


for(int i = position - 1; i < *n - 1; i++){
a[i] = a[i+1];
}
(*n)--;
}

void printArray(int* a, int* n){


for(int i = 0; i < *n; ++i){
printf("%d ", a[i]);
}
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
int position;
scanf("%d", &position);
deleteElement(a,&n,position);
printArray(a,&n);
return 0;
}
* Exercise 7.4: Average of element’s values in an array
Write the program to ask to the user to enter the array size and array elements. Then
display the elements of the array, calculate the average of element’s values of the array
and display the average value.
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void printArray(int* a, int n){


for(int i = 0; i < n; ++i){
printf("%d ", a[i]);
}
}

float avrArray(int* a, int n){


int sum = 0;
for(int i = 0; i < n; ++i){
sum += a[i];
}
return (float)sum/n;
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
printArray(a,n);
printf("\n");
float kq = avrArray(a,n);
printf("%.2f", kq);
return 0;
}

* Exercise 7.5: Find the average of the largest and smallest element in the array
Write a program that allows declaring an array, the number of elements (size of the array)
and values for its elements. Then find calculate and display the average of the largest and
the smallest element.
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

int maxElement(int a[], int n){


int max = a[0];
for(int i = 1; i < n; ++i){
if(a[i] > max)
max = a[i];
}
return max;
}

int minElement(int a[], int n){


int min = a[0];
for(int i = 1; i < n; ++i){
if(a[i] < min)
min = a[i];
}
return min;
}

float avrMinMax(int a[], int n){


int max = maxElement(a,n), min = minElement(a,n);
float avr = (float)(max+min)/2;
return avr;
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
float kq = avrMinMax(a,n);
printf("%.2f", kq);
return 0;
}
* Exercise 7.6: Find the average of even and odd numbers in the array
Write a program that allows declaring an array, the number of elements (size of the array)
and values for its elements. Then calculate and display:
- The average of even number (values) of the array.
- The average of odd number (values) of the array.
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

float avrEven(int a[], int n){


int sum = 0, cnt = 0;
for(int i = 0; i < n; ++i){
if(a[i] % 2 == 0){
sum += a[i];
cnt++;
}
}
return (float)sum/cnt;
}

float avrOdd(int a[], int n){


int sum = 0, cnt = 0;
for(int i = 0; i < n; ++i){
if(a[i] % 2 != 0){
sum += a[i];
cnt++;
}
}
return (float)sum/cnt;
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
float even = avrEven(a,n);
float odd = avrOdd(a,n);
printf("%.2f %.2f", even, odd);
return 0;
}
* Exercise 7.7: Find duplicate element in an array
Write a program that allows declaring an array, the number of elements (size of the array)
and values for its elements. Then find and display all values that appear 2 or more times
in the array. For example, with the array [ 1 2 3 4 3 4 2 3 5 6], the result will be: 2 3 4
#include<stdio.h>
#include<math.h>
void inputArray(int* a, int n){
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void duplicateElement(int* a, int* b, int n){


for(int i = 0; i < n; ++i){
b[a[i]]++;
}
}

int main(){
int n;
scanf("%d", &n);
int a[n], b[1000]={0};
inputArray(a,n);
duplicateElement(a,b,n);
for(int i = 0; i < n; ++i){
if(b[a[i]] > 1){
printf("%d ", a[i]);
b[a[i]] = 0;
}
}
return 0;
}
* Exercise 7.8: Insert new element into an array
Write a program that allows declaring an array, the number of elements (size of the array)
and values for its elements. and then ask the user to enter the index to insert a new
element. Display the elements of the array after insertion.
\ #include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void insertElement(int* a, int* n, int index, int element){


(*n)++;
for(int i = *n - 1; i >= index; --i){
a[i] = a[i - 1];
}
a[index] = element;
}

void printArray(int* a, int* n){


for(int i = 0; i < *n; ++i){
printf("%d ", a[i]);
}
}
int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
int index, element;
scanf("%d%d", &index, &element);
insertElement(a,&n,index,element);
printArray(a,&n);
return 0;
}

* Exercise 7.9: Delete the duplicate values in an array


Write a program that allows declaring an array, the number of elements (size of the array)
and values for its elements. Then find and delete all values that appear from 2nd onwards
in the array. Display the elements of the array after deletion. For example, with the array
[ 1 2 3 4 3 4 2 3 5 6], the result will be: [1 2 3 4 5 6];
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void printArray(int* a, int n){


for(int i = 0; i < n; ++i){
printf("%d ", a[i]);
}
}

int delDuplicate(int* a, int* b, int* c, int n){


for(int i = 0; i < n; ++i){
b[a[i]]++;
}
int k = 0;
for(int i = 0; i < n; ++i){
if(b[a[i]] >= 1){
c[k] = a[i];
k++;
b[a[i]] = 0;
}
}
return k;
}

int main(){
int n;
scanf("%d", &n);
int a[n], b[1000] = {0}, c[n];
inputArray(a,n);
int lenC = delDuplicate(a, b, c, n);
printArray(c,lenC);
return 0;
}

* Exercise 7.10: Multiply Two arrays


Write a program that allows declaring TWO arrays with the same number of elements
(size of the array), then input the values for the two elements. Calculates and replaces the
values of elements in the first array by multiplying the value of the element in the first
array by the value of the element in the same position in the second array. Display the
values of the elements of the first array after replacing. For example, with the first array
[ 1 2 3 4 ] and the second array [2 3 4 5] the result will be: [2 6 12 20];
#include<stdio.h>
#include<math.h>

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void printArray(int* a, int n){


for(int i = 0; i < n; ++i){
printf("%d ", a[i]);
}
}

void multiply(int* a, int* b, int n){


for(int i = 0; i < n; ++i){
a[i] = a[i] * b[i];
}
}

int main(){
int n;
scanf("%d", &n);
int a[n], b[n];
inputArray(a,n);
inputArray(b,n);
multiply(a,b,n);
printArray(a,n);
return 0;
}v
* Exercise 7.11 Two-dimension array:
Write a program that allows the user to:
- Declare an integer 2-dimensional array by declaring the size of the array (row and
column);
- Enter the elements of the array from the keyboard;
- Display the elements of the array including the index (row and column) and the
corresponding value of each element;
- Calculate and display the sum of value of elements in each row;
- Calculate the display sum of value of elements in each column.
For example: Assume that, you declare a 2-dimensional array A which consists of 2
rows and 3 columns. And you enter 6 values for the elements of the array as follows:
2
4
6
8
10
12
So, result of displaying the elements of the array including the index (row and column)
and the corresponding value of each element is as following:
A[0][0] = 2 A[0][1] = 4 A[0][2] = 6
A[1][0] = 8 A[1][1] = 10 A[1][2] = 12
And, result of calculating of sum of element’s values in each row and column as follows:
Calculate and display the sum of value of elements in each row:
Sum of value of elements in row 0: 12
Sum of value of elements in row 1: 30
Calculate and display the sum of value of elements in each column:
Sum of value of elements in column 0: 10
Sum of value of elements in column 1: 14
Sum of value of elements in column 2: 18
#include <stdio.h>

void inputArray(int n, int m, int a[n][m]){


for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf("%d", &a[i][j]);
}
}
}

int sumRow(int n, int m, int a[n][m], int i){


int sum = 0;
for (int j = 0; j < m; ++j) {
sum += a[i][j];
}
return sum;
}

int sumColumn(int n, int m, int a[n][m], int j){


int sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i][j];
}
return sum;
}

int main(){
int n, m;
scanf("%d%d", &n,&m);
int a[n][m];
inputArray(n,m,a);
for (int i = 0; i < n; ++i) {
printf("Sum of row %d: %d\n", i, sumRow(n,m,a,i));
}
for (int j = 0; j < m; ++j) {
printf("Sum of column %d: %d\n", j, sumColumn(n,m,a,j));
}
return 0;
}
* Exercise 7.12 Two-dimension array:
Write a program that allows the user to:
- Declare an integer 2-dimensional array by declaring the size of the array (row and col);
- Enter the elements of the array from the keyboard;
- Calculate and display the sum of value of elements in row number n; n is entered from
keyboard and 0<=n<row;
- Calculate the display sum of value of elements in column number m; m is entered from
keyboard and 0<=m<col.
- Find and display the largest and smallest element in row number n; n is entered from
keyboard and 0<=n<row;
- Find and display the largest and smallest element in column number m; m is entered
from keyboard and 0<=m<col;

* Exercise 7.13: Exercise to do in the online system


Write a program to:
- Enter the positive integer n from the keyboard (n>0).
- Declare a 1-dimensional array of integer type (up to 50 elements). Enter values for
the n elements of the array.
- Display the elements of the array.
- Find and display the largest number and its occurrences in the array.
Data (Input)
- The first line contains a positive integer n;
- The second line contains n integers, each number separated by space;
Result (Output)
- The first line contains n integers, each number separated by space;
- The second line contains the largest number;
- The third line contains the number of occurrences of the largest number;

Example
Input
6
5 8 3 11 9 11
Output
5 8 3 11 9 11
11
2

* Exercise 7.14:
Write a program to:
- Enter the positive integer n from the keyboard (n>0).
- Declare a 1-dimensional array of integer type (up to 50 elements). Enter values for
the n elements of the array.
- Display the elements of the array.
- Displays the total number of prime numbers of the array.
#include<stdio.h>
#include<math.h>

int isPrime(int n){


for(int i = 2; i <= sqrt(n); ++i){
if(n % i == 0){
return 0;
}
}
return 1;
}

void inputArray(int* a, int n){


for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
}

void printArray(int* a, int n){


for(int i = 0; i < n; ++i){
printf("%d ", a[i]);
}
}

int countPrime(int* a, int n){


int cnt = 0;
for(int i = 0; i < n; ++i){
if(isPrime(a[i]))
cnt++;
}
return cnt;
}

int main(){
int n;
scanf("%d", &n);
int a[n];
inputArray(a,n);
printArray(a,n);
printf("\n");
int kq = countPrime(a,n);
printf("%d", kq);
return 0;
}

Data
- The first line contains a positive integer n;
- The next n lines, each line contains 1 integer (values of array’s elements).
Result
- The first n lines, each line contain an integer (values of array’s elements);
- The (n+1) line contains the total number of prime numbers of the array.
Example
Input
6
5
8
3
11
9
11
Output
5
8
3
11
9
11
4

You might also like