Arrays
Arrays
CHAPTER 3 - ARRAYS
***********************************************************************************
*********
***********************************************************************************
*********
3. Write a program to print the position of the smallest number of n numbers using
arrays.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], small, pos;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
small = arr[0]
pos =0;
for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];
pos = i;
}
}
printf("\n The smallest element is : %d", small);
printf("\n The position of the smallest element in the array is : %d", pos);
return 0;
}
***********************************************************************************
*********
***********************************************************************************
******
5. Write a program to enter n number of digits. Form a number using these digits.
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int number=0, digit[10], numofdigits,i;
clrscr();
printf("\n Enter the number of digits : ");
scanf("%d", &numofdigits);
for(i=0;i<numofdigits;i++)
{
printf("\n Enter the digit at position %d", i+1);
scanf("%d", &digit[i]);
}
i=0;
while(i<numofdigits)
{
number = number + digit[i] * pow(10,i);
i++;
}
printf("\n The number is : %d", number);
return 0;
}
***********************************************************************************
*******
***********************************************************************************
********
***********************************************************************************
************
***********************************************************************************
*******
10. Write a program to delete a number from an array that is already sorted in
ascending order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be deleted : "
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
for(j=i; j<n�1;j++)
arr[j] = arr[j+1];
}
}
n = n�1;
printf("\n The array after deletion is : ");
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
***********************************************************************************
**********
***********************************************************************************
*********
***********************************************************************************
********
13. Write a program to read an array of n numbers and then find the smallest
number.
#include <stdio.h>
#include <conio.h>
void read_array(int arr[], int n);
int find_small(int arr[], int n);
int main()
{
int num[10], n, smallest;
clrscr();
printf("\n Enter the size of the array : ");
scanf("%d", &n);
read_array(num, n);
smallest = find_small(num, n);
printf("\n The smallest number in the array is = %d", smallest);
getch();
return 0;
}
void read_array(int arr[10], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
}
int find_small(int arr[10], int n)
{
int i = 0, small = arr[0];
for(i=1;i<n;i++)
{
if(arr[i] < small)
small = arr[i];
}
return small;
}
***********************************************************************************
********
14. Write a program to interchange the largest and the smallest number in an array.
#include <stdio.h>
#include <conio.h>
void read_array(int my_array[], int);
void display_array(int my_array[], int);
void interchange(int arr[], int);
int find_biggest_pos(int my_array[10], int n);
int find_smallest_pos(int my_array[10], int n);
int arr[10], n;
clrscr();
printf("\n Enter the size of the array : ");
scanf("%d", &n);
read_array(arr, n);
interchange(arr, n);
printf("\n The new array is: ");
display_array(arr,n);
getch();
return 0;
}
void read_array(int my_array[10], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &my_array[i]);
}
}
void display_array(int my_array[10], int n)
{
int i;
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, my_array[i]);
}
void interchange(int my_array[10], int n)
{
int temp, big_pos, small_pos;
big_pos = find_biggest_pos(my_array, n);
small_pos = find_smallest_pos(my_array,n);
temp = my_array[big_pos];
my_array[big_pos] = my_array[small_pos];
my_array[small_pos] = temp;
}
int find_biggest_pos(int my_array[10], int n)
{
int i, large = my_array[0], pos=0;
for(i=1;i<n;i++)
{
if (my_array[i] > large)
{
large = my_array[i];
pos=i;
}
}
return pos;
}
int find_smallest_pos (int my_array[10], int n)
{
int i, small = my_array[0], pos=0;
for(i=1;i<n;i++)
{
if (my_array[i] < small)
{
small = my_array[i];
pos=i;
}
}
return pos;
}
***********************************************************************************
***********
***********************************************************************************
**********
***********************************************************************************
***********
***********************************************************************************
********
18. In a small company there are five salesmen. Each salesman is supposed to sell
three
products. Write a program using a 2D array to print (i) the total sales by each
salesman
and (ii) total sales of each item.
#include <stdio.h>
#include <conio.h>
int main()
{
int sales[5][3], i, j, total_sales=0;
//INPUT DATA
printf("\n ENTER THE DATA");
printf("\n *****************");
for(i=0; i<5; i++)
{
printf("\n Enter the sales of 3 items sold by salesman %d: ", i+1) ;
for(j=0; j<3; j++)
scanf("%d", &sales[i][j]);
}
// PRINT TOTAL SALES BY EACH SALESMAN
for(i=0; i<5; i++)
{
total_sales = 0;
for(j=0; j<3; j++)
total_sales += sales[i][j];
printf("\n Total Sales By Salesman %d = %d", i+1, total_sales);
}
// TOTAL SALES OF EACH ITEM
for(i=0; i<3; i++) // for each item
{
total_sales=0;
for(j=0; j<5; j++) // for each salesman
total_sales += sales[j][i];
printf("\n Total sales of item %d = %d", i+1, total_sales);
}
getch();
return 0;
}
***********************************************************************************
**********
19. Write a program to read a 2D array marks which stores the marks of five
students in three
subjects. Write a program to display the highest marks in each subject.
#include <stdio.h>
#include <conio.h>
int main()
{
int marks[5][3], i, j, max_marks;
for(i=0; i<5; i++)
{
printf("\n Enter the marks obtained by student %d",i+1);
for(j=0; j<3; j++)
{
printf("\n marks[%d][%d] = ", i, j);
scanf("%d", &marks[i][j]);
}
}
for(j=0; j<3; j++)
{
max_marks = �999;
for(i=0; i<5; i++)
{
if(marks[i][j]>max_marks)
max_marks = marks[i][j];
}
printf("\n The highest marks obtained in the subject %d = %d", j+1, max_marks);
}
getch();
return 0;
}
***********************************************************************************
**********
***********************************************************************************
************
***********************************************************************************
*********
22. Write a program to input two m � n matrices and then calculate the sum of their
corresponding elements and store it in a third m � n matrix.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
int rows1, cols1, rows2, cols2, rows_sum, cols_sum;
int mat1[5][5], mat2[5][5], sum[5][5];
clrscr();
printf("\n Enter the number of rows in the first matrix : ");
scanf("%d",&rows1);
printf("\n Enter the number of columns in the first matrix : ");
scanf("%d",&cols1);
printf("\n Enter the number of rows in the second matrix : ");
scanf("%d",&rows2);
printf("\n Enter the number of columns in the second matrix : ");
scanf("%d",&cols2);
if(rows1 != rows2 || cols1 != cols2)
{
printf("\n Number of rows and columns of both matrices must be equal");
getch();
exit();
}
rows_sum = rows1;
cols_sum = cols1;
printf("\n Enter the elements of the first matrix ");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\n Enter the elements of the second matrix ");
for(i=0;i<rows2;i++)
{
for(j=0;j<cols2;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
sum[i][j] = mat1[i][j] + mat2[i][j];
}
printf("\n The elements of the resultant matrix are ");
for(i=0;i<rows_sum;i++)
{
printf("\n");
for(j=0;j<cols_sum;j++)
printf("\t %d", sum[i][j]);
}
return 0;
}
***********************************************************************************
**********
***********************************************************************************
************
24. Write a program to fill a square matrix with value zero on the diagonals, 1 on
the upper
right triangle, and �1 on the lower left triangle.
#include <stdio.h>
#include <conio.h>
void read_matrix(int mat[5][5], int);
void display_matrix(int mat[5][5], int);
int main()
{
int row;
int mat1[5][5];
clrscr();
printf("\n Enter the number of rows and columns of the matrix:");
scanf("%d", &row);
read_matrix(mat1, row);
display_matrix(mat1, row);
getch();
return 0;
}
void read_matrix(int mat[5][5], int r)
{
int i, j;
for(i=0; i<r; i++)
{
for(j=0; j<r; j++)
{
if(i==j)
mat[i][j] = 0;
else if(i>j)
mat[i][j] = �1;
else
mat[i][j] = 1;
}
}
}
void display_matrix(int mat[5][5], int r)
{
int i, j;
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<r; j++)
printf("\t %d", mat[i][j]);
}
}
***********************************************************************************
************
***********************************************************************************
*************
***********************************************************************************
************
***********************************************************************************
***********