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

Arrays, Pointers, Functions Experiments

Uploaded by

Shaik Mohammed
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)
13 views

Arrays, Pointers, Functions Experiments

Uploaded by

Shaik Mohammed
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/ 14

Arrays, Pointers and Functions

Experiment 1: Develop a C program to sort n elements in single dimension array.


Aim : To sort n elements in single dimension array.
Software used : TurboC++ / DevC++
Program :
#include<stdio.h>
void main()
{
int a[100],i,j,t;
printf("enter total number of elements:");
scanf("%d",&n);
printf("enter values:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
} }
}
printf("AFTER SORTING \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

Sample Output:
enter total number of elements 6
enter values : 34, 23, 56, 12 , 24, 78
AFTER SORTING 12 , 23 , 24, 34 , 56, 78

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 2 : Write a C program to perform Addition of Two Matrices
Aim : To develop a C program to perfom addition of two matrices
Software used : TurboC++/ Devc++
Program:

#include<stdio.h>
int main()
{
int i,j,a[3][3],b[3][3],c[3][3];
printf("Enter first matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

scanf("%d",&b[i][j]);

}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]+b[i][j]);
}
}
return 0;
}

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 3 :Write a C program to perform Multiplication of Two Matrices
Aim : To develop a C program to perform Multiplication of two matrices
Software used : TurboC++/DevC++
Program:

#include<stdio.h>
int main()
{
int i,j,a[3][3],b[3][3],c[3][3];
printf("Enter first matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

scanf("%d",&b[i][j]);

}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]*b[i][j]);
}
}
return 0;
}

Actual Output:

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 4 : Write a C program to determine transpose of a matrix with memory
dynamically allocated for the new matrix as row and column counts may not be same.
Aim : To determine transpose of a matrix with memory dynamically allocated for the new matrix
as row and column counts may not be same
Software used : TurboC++/DevC++
Program:
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

// Storing elements of the matrix


printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
{
printf("\n\n");
}
}
}

// Finding the transpose of matrix a


for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
}

// Displaying the transpose of matrix


printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
{
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
{
printf("\n\n");
}
}
}
return 0;
}
SAMPLE OUTPUT:
Enter rows and columns of matrix: 2
3

Enter element of matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix:
2 3 4
5 6 4

Transpose of Matrix:
2 5
3 6
4 4

ACTUAL OUTPUT :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 5 : Write C programs to find factorial of an integer using non recursive
function

Aim : To develop C program to find factorial of an integer using non recursive function
Software used : TurboC++/DevC++
Program:
#include<stdio.h>
void main()
{
int n,fact=1,i;
printf("enter number to find factorial");
scanf("%d",&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial of %d is %d",n,fact);
}
Sample output:
enter number to find factorial 5
factorial of 5 is 120

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 6 : Write C programs to find factorial of an integer using recursive function

Aim : To develop C program to find factorial of an integer using recursive function


Software used : TurboC++/DevC++
Program:

#include<stdio.h>
long factorial(int);//
int main()
{
int number;
long fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);
printf("%d! = %ld\n", number, factorial(number));
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
{
result = result * c;
}
return result;
}
Sample Output:
Enter a number to calculate its factorial 6
6! = 720

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 7 : Write C program to find GCD(Greatest Common Divisor) using non
recursive function

Aim : To develop C program to find GCD(Greatest Common Divisor) using non recursive
function
Software used : TurboC++/DevC++
Program:

#include<stdio.h>
void main()
{
int a,b,x,y;
printf("enter numbers to GCD");
scanf("%d%d",&a,&b);
x=a;
y=b;
while(a!=b)
{
if(a<b)
{
b=b-a;
}
else
{
a=a-b;
}
}
printf("gcd of %d , %d is %d",x,y,a);
}

Sample Output:
Enter numbers to GCD 81, 153
Gcd of 81, 153 is 9
Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 8: Write C program to find GCD(Greatest Common Divisor) using recursive
function

Aim : To develop C program to find GCD(Greatest Common Divisor) using recursive


function
Software used : TurboC++/DevC++
Program:

#include<stdio.h>
void main()
{
int a,b,res;
printf("enter numbers to GCD");
scanf("%d%d",&a,&b);
res=gcd(a,b);
printf("gcd of %d , %d is %d",a,b,res);
}
int gcd(int a,int b)
{
if(a==b)
{
return a;
}
else
{
if(a<b)
{
return gcd(a,b-a);
}
else
{
return gcd(a-b,b);
}
}
}
Sample Output:
enter numbers to GCD 81 153
Gcd of 81, 153 is 9
Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 9: Write a program for reading elements using pointer into array and display
the values using array.

Aim : To develop C program for reading elements using pointer into array and display the
values using array.
Software used : TurboC++/DevC++
Program:

#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
{
scanf("%d", data + i);
}
printf("You entered: \n");
for(i = 0; i < 5; ++i)
{
printf("%d\n", *(data + i));
}
return 0;
}
Sample Output
Enter elements: 1 2 3 5 4
You entered: 1 2 3 5 4

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 10 : Write a program for display values reverse order from array using
pointer.
Aim : To develop a C program to display values in reverse order from array using pointer
Software used : TurboC++/DevC++
Program :
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++)
{
scanf("%d", ptr);
ptr++;
}
ptr = &arr[size - 1];
printf("\nElements of array in reverse order are :");
for (i = size - 1; i >= 0; i--)
{
printf("\nElement%d is %d : ", i, *ptr);
ptr--;
}
getch();
}

SAMPLE OUTPUT:
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11
ACTUAL OUTPUT:
******LEAVE AN EMPTY SPACE FOR OUTPUT*****************

Experiment 11 : Write a program for compute sum of n elements from array using pointer.
Aim : To develop a C a program for compute sum of n elements from array using pointer
Software used : TurboC++/DevC++
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
{
scanf("%d", &numArray[i]);
}
ptr = numArray;
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
getch();
}

SAMPLE OUTPUT:
ENTER 10 elements 1 2 3 4 5 6 7 8 9
The sum of array elements 55

ACTUAL OUTPUT:
******LEAVE AN EMPTY SPACE FOR OUTPUT*****************
Experiment 12 : Write C programs to compute x^n using non recursive function
Aim : To develop a C program to compute x^n using non recursive function
Software used : TurboC++/ DevC++
Program :
#include<stdio.h>
void main()
{
int x,n,res=1;
printf("enter numbers x,n");
scanf("%d%d",&x,&n);
while(n>0)
{
res=res*x;
n--;
}
printf("result is %d",res);
}

Sample Output:
Enter numbers x , n 2 , 3
Result is 8

Actual Output :

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************


Experiment 13 : Write C programs to compute x^n using recursive function
Aim : To develop a C program to compute x^n using recursive function
Software used : TurboC++/ DevC++
Program :#include<stdio.h>
void main()
{
int x,n,i,res=1;
printf("enter x,n");
scanf("%d%d",&x,&n);
res=power(x,n);
printf("result is %d",res);
}
int power(int x,int n)
{
if(n==0)
{
return 1;
}
else
{
return x*power(x,n-1);
}
Sample Output:
Enter numbers x , n 3 3
Result is 27
Actual Output:

******LEAVE AN EMPTY SPACE FOR OUTPUT*****************

You might also like