Tuesday, May 1, 2012

programming language_01(CS)

4. Write a program that will print the following for a 4 by 4 matrix A
input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output:
1
5 6
9 10 11
13 14 15 16

solution:
#include< stdio.h >
#define row 4
#define column 4
int main(){
int A[row][column]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int i,j,k;
for( i = 0 ; i < row ; i++ ){
        for(  j = 0 ; j <=i; j++ )
            {
                printf("%d  ",A[i][j]);
            }

        printf("\n\n");

    }
    return 0;
}

4. Write a program that will print the following for a 4 by 4 matrix A
input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output:
 1  2  3  4
    6  7   8
       11  12
             16
solution
#include< stdio.h >
#define ROW 6
#define COLUMN 6
int main( void ){
    int A[ROW][COLUMN] = {1,2,3,4,5,6,
                          7,8,9,10,11,12,
                          13,14,15,16,17,18,
                          19,20,21,22,23,24,
                          25,26,27,28,29,30,
                          31,32,33,34,35,36};
    //here we are printing the lower left
int i,j,k;
    for(  i = 0 ; i < ROW ; i++ ){
        for(  j = 0 ; j < COLUMN ; j++ )
            if( i ==j ) {
                for(  k = j ; k < COLUMN ; k++ )
                    printf("\t%d",A[i][k]);
            }else{
                printf("\t" );
            }
        printf("\n\n");
    }
    return 0;
}

 3. Implement the following using function.
a. Input: Two numbers x and n, where x (must be decimal) is the value of to be used in
the series and n (must be integer) is the no of times the series will run.
b. Desired Output: calculate the series for Sin(x) up to n.

 solution:

#include< stdio.h >
int myfunc(int x){
    printf("\n\nthe value of sin is:\n\n");
printf("sin%d=%d-%d^3/3!+%d^5/5!-%d^7/7!+....\n\n",x,x,x,x,x);
  printf("the value of cos is:\n\n");
printf("cos%d=%d-%d^2/2!+%d^4/4!-%d^6/6!+....\n\n",x,x,x,x,x);
return 0;
}
int main(){
int n;
printf("put the value of x");
scanf("%d",&n);
myfunc(n);
return 0;
}
 

No comments:

Post a Comment