Tanti Technology

My photo
Bangalore, karnataka, India
Multi-platform UNIX systems consultant and administrator in mutualized and virtualized environments I have 4.5+ years experience in AIX system Administration field. This site will be helpful for system administrator in their day to day activities.Your comments on posts are welcome.This blog is all about IBM AIX Unix flavour. This blog will be used by System admins who will be using AIX in their work life. It can also be used for those newbies who want to get certifications in AIX Administration. This blog will be updated frequently to help the system admins and other new learners. DISCLAIMER: Please note that blog owner takes no responsibility of any kind for any type of data loss or damage by trying any of the command/method mentioned in this blog. You may use the commands/method/scripts on your own responsibility. If you find something useful, a comment would be appreciated to let other viewers also know that the solution/method work(ed) for you.

Monday 4 July 2011

C program-5

1. /* Write a C program to input real numbers and find the *
* mean, variance and standard deviation */

#include
#include
#define MAXSIZE 10
main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;

clrscr();

printf("Enter the value of N\n");
scanf("%d", &n);

printf("Enter %d real numbers\n",n);
for(i=0; i
#define MAX 4

main()
{
int a[MAX], i, l1,l2, s, temp;
clrscr();

printf("Enter %d integer numbers\n", MAX);
for (i=0; i < MAX; i++) { scanf("%d", &a[i]); } printf("Input interger are\n"); for (i=0; i < MAX; i++) { printf("%5d", a[i]); } printf("\n"); l1 = a[0]; /*assume first element of array is the first largest*/ l2 = a[1]; /*assume first element of array is the second largest*/ if (l1 < l2) { temp = l1; l1 = l2; l2 = temp; } for (i=2;i<4;i++) { if (a[i] >= l1)
{
l2 = l1;
l1 = a[i];
}
else if(a[i] > l2)
{
l2= a[i];
}
}
printf("\n%d is the first largest\n", l1);
printf("%d is the second largest\n", l2);
printf("\nAverage of %d and %d = %d\n", l1,l2, (l1+l2)/2);
}
/*-----------------------------------
Output
RUN 1
Enter 4 integer numbers
45
33
21
10
Input interger are
45 33 21 10

45 is the first largest
33 is the second largest

Average of 45 and 33 = 39

RUN 2
Enter 4 integer numbers
12
90
54
67
Input interger are
12 90 54 67

90 is the first largest
67 is the second largest

Average of 90 and 67 = 78

RUN 3
Enter 4 integer numbers
100
200
300
400
Input interger are
100 200 300 400

400 is the first largest
300 is the second largest

Average of 400 and 300 = 350

------------------------------------*/

**************************************************************************


3. /* Write a C program to evaluate the given polynomial *
* P(x)=AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0, by *
* reading its coefficents into an array. [Hint:Rewrite *
* the polynomial as *
* P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) *
* and evalate the function starting from the inner loop]*/
#include
#define MAXSIZE 10
main()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;

clrscr();
printf("Enter the order of the polynomial\n");
scanf("%d", &N);

printf("Enter the value of x\n");
scanf("%f", &x);
/*Read the coefficients into an array*/
printf("Enter %d coefficients\n",N+1);
for (i=0;i <= N;i++) { scanf("%d",&a[i]); } polySum = a[0]; for (i=1;i<= N;i++) { polySum = polySum * x + a[i]; } power = N; /*power--;*/ printf("Given polynomial is:\n"); for (i=0;i<= N;i++) { if (power < 0) { break; } /* printing proper polynomial function*/ if (a[i] > 0)
printf(" + ");
else if (a[i] < 0) printf(" - "); else printf (" "); printf("%dx^%d ",abs(a[i]),power--); } printf("\nSum of the polynomial = %6.2f\n",polySum); } /*------------------ Output RUN 1 Enter the order of the polynomial 2 Enter the value of x 2 Enter 3 coefficients 3 2 6 Given polynomial is: + 3x^2 + 2x^1 + 6x^0 Sum of the polynomial = 22.00 RUN 2 Enter the order of the polynomial 4 Enter the value of x 1 Enter 5 coefficients 3 -5 6 8 -9 Given polynomial is: + 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0 Sum of the polynomial = 3.00 --------------------*/ ********************************************************************* 4. /* Write a C program to read two matrices A (MxN) and B(MxN)* * and perform addition OR subtraction of A and B. Find the * * trace of the resultant matrix. Output the given matrix, * * their sum or Differences and the trace. */ #include

main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;
void trace (int arr[][10], int M, int N);
clrscr();
printf("Enter the order of the matrice A and B\n");
scanf("%d %d", &M, &N);

printf("Enter the elements of matrix A\n");
for(i=0; i

main()
{
int i,j,M,N;
int A[10][10], B[10][10];
int transpose(int A[][10], int r, int c); /*Function prototype*/
clrscr();

printf("Enter the order of matrix A\n");
scanf("%d %d", &M, &N);
printf("Enter the elements of matrix\n");
for(i=0;i {
for(j=0;j {
scanf("%d",&A[i][j]);
}
}
printf("Matrix A is\n");
for(i=0;i {
for(j=0;j {
printf("%3d",A[i][j]);
}
printf("\n");
}
/* Finding Transpose of matrix*/
for(i=0;i {
for(j=0;j {
B[i][j] = A[j][i];
}
}

printf("Its Transpose is\n");
for(i=0;i {
for(j=0;j {
printf("%3d",B[i][j]);
}
printf("\n");
}

} /*End of main()*/
/*---------------------------------------
Output
Enter the order of matrix A
3 3
Enter the elements of matrix
1
2
3
4
5
6
7
8
9
MatrixxA is
1 2 3
4 5 6
7 8 9
Its Transpose is
1 4 7
2 5 8
3 6 9
-----------------------------*/




No comments:

Post a Comment