Archive for the ‘Lab 3’ Category

Practical 7.6

Posted: January 23, 2012 in Lab 3

Problem statement- Write a function to calculate the (floating point) average of an array of integers.

#include<stdio.h>
float avg(int *, int);
int main()
{
int a[20],i,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);
printf("Entre elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
avg(&a[0],n);
return 0;
}
float avg(int *a, int n)
{
int i;
float avg,s=0;
for(i=0;i<n;i++)
s=s+*(a+i);
avg=s/n;
printf("Avg is %f",avg);
}
Advertisements

Practical 7.5

Posted: January 23, 2012 in Lab 3

Problem Statement- Using the two previous functions, and using a starting array [1, 1], calculate the first 20 numbers in the Fibonacci sequence.

#include<stdio.h>
int last2(int *, int);
int last2(int *arr,int n)
{
int i,sum=0;
for(i=n-1;i>(n-3);i--)
sum=sum+*(arr+i);
return sum;
}
int append(int *, int, int);
int append(int *arr,int n, int a)
{
*(arr+n)=a;
return 0;
}
int main()
{
int arr[20],i;
arr[0]=1;
arr[1]=1;
for(i=2;i<20;i++)
append(&arr[0],i,last2(&arr[0],i));
for(i=0;i<20;i++)
printf("%d\n",*(arr+i));
return 0;
}

Practical 7.4

Posted: January 23, 2012 in Lab 3

Problem Statement- Append: write a function to take in an array and an integer and append the integer onto the end of the array.

#include<stdio.h>
int append(int *, int, int);
int main()
{
int arr[20],n,i,a;
printf("NO. of elements?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("What no. do u want to append\n");
scanf("%d",&a);
append(&arr[0],n,a);
return 0;
}
int append(int *arr,int n, int a)
{
int i;
*(arr+n)=a;
printf("Array after appending %d\n",a);
for(i=0;i<=n;i++)
printf("%d ",arr[i]);
return 0;
}

Practical 7.3

Posted: January 23, 2012 in Lab 3

Problem Statement- Add last two: write a function to take in an array and return the sum of the last two elements in it.

#include<stdio.h>
int last2(int *, int);
int main()
{
int arr[20],n,i;
printf("NO. of elements?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
last2(&arr[0],n);
return 0;
}
int last2(int *arr,int n)
{
int i,sum=0;
for(i=n-1;i>(n-3);i--)
sum=sum+*(arr+i);
printf("Sum of last 2 elements is %d",sum);
return 0;
}

Practical 7.2

Posted: January 23, 2012 in Lab 3

Problem Statement- Find max: write a function to take in an array and return the value of the largest element in the array.

#include<stdio.h>
int max(int *, int);
int main()
{
int arr[20],n,i;
printf("NO. of elements?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
max(&arr[0],n);
return 0;
}
int max(int *arr,int n)
{
int i,max=0;
for(i=0;i<n;i++)
if(*(arr+i)>max)
max=*(arr+i);
printf("largest is %d",max);
return 0;
}

Practical 7.1

Posted: January 23, 2012 in Lab 3

Problem statement- Implement a program that reads in a list of integers (up to 20 of them), and then performs a sequence of operations on them. The minimum operations that your program must perform are as follows:
• Sum: print out the sum of the integers in your array.
• Print: print out the array
• Print backward: print out the array in reverse order
• Reverse: reverse the array
• Drop first: remove the first element of the array

#include<stdio.h>
int main()
{
int arr[20],rev[20],i,j=0,s=0,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
//sum
for(i=0;i<n;i++)
s=s+arr[i];
printf("sum=%d\n",s);
//print
printf("Array is= ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
//print backward
printf("\nPrint backward\n");
for(i=(n-1);i>=0;i--)
printf("%d ",arr[i]);
//reverse array
for(i=(n-1);i>=0;i--)
rev[j++]=arr[i];
printf("\nReverse\n");
for(i=0;i<n;i++)
printf("%d ",rev[i]);
//drop first
for(i=0;i<(n-1);i++)
arr[i]=arr[i+1];
printf("\nAfter dropping first element\n");
for(i=0;i<(n-1);i++)
printf("%d ",arr[i]);
return 0;
}

Practical 6- Array and recursion

Posted: January 20, 2012 in Lab 3

Problem Statement-
WAP to calculate the determinant of square matrix of order 3 by 3.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define dim 3
 
int matrix[dim][dim];
 
int determinant(int n, int mat[n][n])
{
	int i,j,i_count,j_count, count=0;
	int array[n-1][n-1], det=0;
 
	if(n<1)
	{
		puts("Error");
		exit(1);
	}
	if(n==1) return mat[0][0];
	if(n==2) return (mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]);
 
	for(count=0; count<n; count++)
	{
		//Creating array of Minors
		i_count=0;
		for(i=1; i<n; i++)
		{
			j_count=0;
			for(j=0; j<n; j++)
			{
				if(j == count) continue;
				array[i_count][j_count] = mat[i][j];
				j_count++;
			}
			i_count++;
		}
		det += pow(-1, count) * mat[0][count] * determinant(n-1,array);	//Recursive call
	}
	return det;
}
 
int main()
{
	int i,j;
	printf("Enter the elements\n");
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
			scanf("%d",&matrix[i][j]);
	int x = determinant(dim, matrix);
	printf("Determinant: %d\n", x);
}

Practical 5B- Wap for bubble sort

Posted: January 20, 2012 in Lab 3
#include<stdio.h>  
#include<conio.h>  
  
  void bubble(int a[],int n)  
  {  
        int i,j,t;  
         for(i=n-2;i>=0;i--)  
         {  
            for(j=0;j<=i;j++)  
  
                  {  
                    if(a[j]>a[j+1])  
                                    {  
                                      t=a[j];  
                                     a[j]=a[j+1];  
                                     a[j+1]=t;  
                                    }  
                   }  
         
  
           }  
  
  }
  
  
  void main()  
  {  
  
      int a[100],n,i;  
  
  
      printf("\n\n Enter integer value for total no.s of elements to be sorted: ");  
      scanf("%d",&n);  
	printf("\n\n Enter integer value for element no: ");
  
      for( i=0;i<=n-1;i++)  
            {   
              scanf("%d",&a[i]);  
            }  
  
       bubble(a,n);  
  
       printf("\n\n Finally sorted array is: ");  
       for( i=0;i<=n-1;i++)  
       printf("%3d",a[i]);  
  
  }