C PROGRAMMING LABORATORY
PROBLEM SHEET 9
1. Write a function to display the maximum, minimum and sum of the array elements of an integer
array using pointers.
PROGRAM
#include <stdio.h>
void findMaxMinSum(int *arr, int size, int *max, int *min, int *sum)
{
*max = *min = *arr;
*sum = 0;
for (int i = 0; i < size; i++) {
if (*(arr + i) > *max)
{
*max = *(arr + i);
}
if (*(arr + i) < *min)
{
*min = *(arr + i);
}
*sum += *(arr + i);
}
}
int main()
{
int arr[] = {23, 9, -21, 89, 1};
int size = sizeof(arr) / sizeof(arr[0]);
int max, min, sum;
findMaxMinSum(arr, size, &max, &min, &sum);
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
printf("Sum: %d\n", sum);
return 0;
}
OUTPUT
Maximum: 89
Minimum: -21
Sum: 101
2. Read numbers between 10 and 100 (validate it). Print the number only if it is not a duplicate of
the already read number. Use single subscripted array for this. (Use: Pointer to an array)
PROGRAM
#include <stdio.h>
int main ()
{
int a[5];
for (int i=0;i<5;i++)
{
printf("Enter value for element %d from 10 to 100: ",i+1);
scanf("%d",&a[i]);
if (a[i] > 100 || a[i] < 10)
{
printf("Enter a value in the correct range");
break;
}
}
int (*p)[5] = &a;
for (int i=0;i<5;i++)
{
int s=1;
for (int j=0;j<5;j++)
{
if (*(*p+i) == *(*p+j) && i!=j)
{
s=0;
break;
}
}
if (s==1)
{
printf("%d ",*(*p+i));
}
}
}
INPUT
Enter value for element 1 from 10 to 100: 20
Enter value for element 2 from 10 to 100: 30
Enter value for element 3 from 10 to 100: 40
Enter value for element 4 from 10 to 100: 50
Enter value for element 5 from 10 to 100: 60
OUTPUT
20 30 40 50 60
3. Declare 5 integer pointer variables in the program. Store them in an array. Write a C
Program to sort the array of pointers.
PROGRAM
#include <stdio.h>
int main ()
{
int a[5],*p[5],x;
for (int i=0;i<5;i++)
{
printf("Enter value for element %d: ",i+1);
scanf("%d",&a[i]);
}
for (int i=0;i<5;i++)
{
p[i] = &a[i];
}
for (int i=0;i<5;i++)
{
int min = *p[i];
for (int j=i;j<5;j++)
{
if (*p[j] < min)
{
x = *p[i];
*p[i] = *p[j];
*p[j] = x;
}
}
}
for (int i=0;i<5;i++)
{
printf("%d ",*p[i]);
}
}
INPUT
Enter value for element 1: 23
Enter value for element 2: 45
Enter value for element 3: 67
Enter value for element 4: 11
Enter value for element 5: 20
OUTPUT
20 11 23 45 67
4. Write a program that allows the user to enter students’ names followed by their test scores
and outputs the following information (assume that maximum number of students is 50):
a. The average score.
b. Names of all students whose test scores are below the average, with an appropriate
message.
c. Highest test score and the name of all students having the highest score.
(Allocate memory dynamically for the array).
PROGRAM
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i,q=3,max=-9,maxi;
float total;
int *marks=(int *)malloc(q), *maxa;
char names[q][50];
for(i=0; i<q; i++)
{
printf("Enter name of student %d: ", i+1);
scanf("%s", &names[i]);
printf("Enter marks of student %d: ", i+1);
scanf("%d", &marks);
scores++;
}
marks-=q;
for(i=0; i<q; i++)
{
total=total+*marks;
marks++;
}
marks-=q;
total/=q;
printf("%f", total);
for(i=0; i<q; i++)
{
if(*marks<total)
{
printf("\n%s is below average with a mark of %d",names[i], *scores);
}
marks +=1;
}
marks-=q;
for(i=0; i<q; i++)
{
if(*marks>max)
{
max = *marks;
maxa = marks;
maxi = i;
}
marks +=1;
}
printf("\n%s is the highest with a mark of %d",names[maxi], *maxa);
}
INPUT
Enter name of student 1: Charlie
Enter marks of student 1: 89
Enter name of student 2: Max
Enter marks of student 2: 34
Enter name of student 3: Bob
Enter marks of student 3: 87
Enter name of student 4: Alice
Enter marks of student 4: 50
Enter name of student 5: Stu
Enter marks of student 5: 14
OUTPUT
54.799999
Max is below average with a mark of 34
Alice is below average with a mark of 50
Stu is below average with a mark of 14
Charlie is the highest with a mark of 89
5. Create an array of 10 elements and fill 9 elements of the array. Write a function to accept a
number and position and insert the number at the given position in the array.( Pass
pointers to the function).
PROGRAM
#include<stdio.h>
int insert(int *p,int *value,int *a)
{
for(int i=9;i>*p;i--)
{
*a=*(a-1);
a=a-1;
}
*a=*value;
}
void main()
{
int a[10]={10,20,30,40,50,60,70,80,90};
int p,value;
printf("Enter the position: ");
scanf("%d",&p);
printf("Enter the value to be inserted: ");
scanf("%d",&value);
insert(&p,&value,&a[9]);
for(int i=0;i<10;i++)
{
printf("%d ",a[i]);
}
}
INPUT
Enter the position: 5
Enter the value to be inserted: 6666
OUTPUT
10 20 30 40 50 6666 60 70 80 90
6. Create an array to store random numbers for a 4x4 matrix. Write a function to interchange
the values of the first two rows of the matrix with that of the second two rows.
PROGRAM
#include <stdio.h>
void main() {
int i, j, mat[4][4], tmp;
int *p;
p = &mat[0][0];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("Enter value for element %d,%d: ", i, j);
scanf("%d", p);
p++;
}
}
p = &mat[0][0];
for (i = 0; i < 2; i++) {
for (j = 0; j < 4; j++) {
tmp = *(p + i * 4 + j);
*(p + i * 4 + j) = *(p + (i + 2) * 4 + j);
*(p + (i + 2) * 4 + j) = tmp;
}
}
p = &mat[0][0];
printf("\nMatrix after interchanging rows:\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%3d ", *p);
p++;
}
printf("\n");
}
}
INPUT
Enter value for element 0,0: 3
Enter value for element 0,1: 4
Enter value for element 0,2: 5
Enter value for element 0,3: 6
Enter value for element 1,0: 7
Enter value for element 1,1: 8
Enter value for element 1,2: 9
Enter value for element 1,3: 1
Enter value for element 2,0: 2
Enter value for element 2,1: 3
Enter value for element 2,2: 4
Enter value for element 2,3: 5
Enter value for element 3,0: 6
Enter value for element 3,1: 7
Enter value for element 3,2: 8
Enter value for element 3,3: 9
OUTPUT
Matrix after interchanging rows:
2 3 4 5
6 7 8 9
3 4 5 6
7 8 9 1
7. Write a C program to accept a string and delete all vowels from the string and push all the
characters up until the hole is removed.
PROGRAM
#include <stdio.h>
void main() {
int i, j = 0, flag;
char string[100], vowel[] = {'a', 'e', 'i', 'o', 'u'};
char *p = string;
printf("Enter a string: ");
scanf("%s", string);
for (p = string; *p != '\0'; p++) {
flag = 0;
for (i = 0; i < 5; i++) {
if (*p == vowel[i]) {
flag = 1;
break;
}
}
if (flag != 1) {
string[j++] = *p;
}
}
string[j] = '\0';
printf("String after removing vowels: %s\n", string);
}
INPUT
Enter a string: Programming
OUTPUT
String after removing vowels: Prgrmmng
8. Write a program which takes as input a PIN number of the user and verifies his number.If
the pin number is matched then the program shall display the message “ Pin
verified, Welcome”, otherwise the program shall give the user an other choice.After four
wrong attempts the program shall display “Limit expired” and then exit.
PROGRAM
#include <stdio.h>
void main() {
int pin[] = {1, 2, 3, 4}, i, in[4], flag, repeat = 0;
while(repeat < 4) {
flag = 0;
for (i = 0; i < 4; i++) {
printf("Enter pin digit %d: ", i + 1);
scanf("%d", &in[i]);
}
for (i = 0; i < 4; i++) {
if (in[i] == pin[i]) {
flag++;
}
}
if (flag == 4) {
printf("Pin verified, Welcome\n");
break;
} else {
printf("Incorrect PIN, try again.\n");
repeat++;
}
}
if (repeat == 4) {
printf("Limit expired\n");
}
}
INPUT 1
Enter pin digit 1: 1
Enter pin digit 2: 2
Enter pin digit 3: 3
Enter pin digit 4: 4
OUTPUT 1
Pin verified, Welcome
INPUT 2
Enter pin digit 1: 1
Enter pin digit 2: 2
Enter pin digit 3: 3
Enter pin digit 4: 5
Incorrect PIN, try again.
Enter pin digit 1: 1
Enter pin digit 2: 2
Enter pin digit 3: 3
Enter pin digit 4: 6
Incorrect PIN, try again.
Enter pin digit 1: 1
Enter pin digit 2: 2
Enter pin digit 3: 3
Enter pin digit 4: 7
Incorrect PIN, try again.
Enter pin digit 1: 1
Enter pin digit 2: 2
Enter pin digit 3: 3
Enter pin digit 4: 8
Incorrect PIN, try again.
Limit expired
9. Write a program to calculate the sum of two numbers using pointers to function.
PROGRAM
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int num1, num2, result;
int (*funcPtr)(int, int) = &add;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = funcPtr(num1, num2);
printf("The sum is: %d\n", result);
return 0;
}
INPUT
Enter two numbers: 1
2
OUTPUT
The sum is: 3
10. Write a program to merge two sorted arrays so that the resultant array is also in the sorted
order using pointers.
PROGRAM
#include<stdio.h>
int sum(int a[40][40],int b[40][40], int c[40][40],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
c[i][j]= ((a+i)+j)+((b+i)+j);
}
printf("\n");}
printf("Sum of two matrix:");
printf("\n");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
}
int main()
{
int a[40][40],b[40][40],size,i,j,c[40][40];
int(*fp)(int a[40][40],int b[40][40], int c[40][40],int size);
fp=sum;
printf("Enter the size:");
scanf("%d",&size);
printf("Enter matrix A:");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter matrix B:");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
sum(a,b,c,size);
}
INPUT
Enter the size:2
Enter matrix A:1
2
3
4
12
34
Enter matrix B:1
2
3
4
12
34
OUTPUT
Sum of two matrix:
24
68