Sie sind auf Seite 1von 9

Addition of Two Matrices

#include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); } return 0; }

Output of program:

Multiplication of Two Matrices

#include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = { for ( d { for ( { sum } 0 ; c < m ; c++ ) = 0 ; d < q ; d++ ) k = 0 ; k < p ; k++ ) = sum + first[c][k]*second[k][d];

multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0; }

A 3 X 3 matrix multiply in c is shown as example below.

Transpose a Matrix

#include <stdio.h> int main() { int m, n, c, d, matrix[10][10], transpose[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d",&m,&n); printf("Enter the elements of matrix \n"); for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { scanf("%d",&matrix[c][d]); } } for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { transpose[d][c] = matrix[c][d]; } } printf("Transpose of entered matrix :-\n"); for( c = 0 ; c < n ; c++ ) {

for( d = 0 ; d < m ; d++ ) { printf("%d\t",transpose[c][d]); } printf("\n"); } return 0; }

Download Transpose Matrix program. Output of program:

Simple program of c find the largest number #include<stdio.h> int main(){ int n,num,i; int big; printf("Enter the values of n: "); scanf("%d",&n); printf("Number %d",1); scanf("%d",&big); for(i=2;i<=n;i++){ printf("Number %d: ",i); scanf("%d",&num); if(big<num) big=num; } printf("Largest number is: %d",big);

return 0; } Sample Output: Enter the values of n: Number 1: 12 Number 2: 32 Number 3: 35 Largest number is: 35

Sort N Numbers use Pointers

#include <stdio.h> #include <conio.h> void main(){ int *arr,i,j,tmp,n; clrscr(); printf("Enter how many data you want to sort : "); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",arr+i); for(i=0;i<n;i++) { for(j=i+1;j<n;j++){ if( *(arr+i) > *(arr+j)){ tmp = *(arr+i); *(arr+i) = *(arr+j); *(arr+j) = tmp; } } } printf("\n\nAfter Sort\n"); for(i=0;i<n;i++) printf("%d\n",*(arr+i)); getch(); }

Find No of Vowels
#include<stdio.h> #include<conio.h> void main() { char str[50]; int vowels = 0, i = 0;

clrscr(); printf(\n\n\t ENTER A STRING: ); gets(str); while(str[i] != \0) { if(str[i]==A || str[i]==a || str[i]==E || str[i]==e || str[i]==I || str[i]==i || str[i]==O || str[i]==o || str[i]==U || str[i]==u') vowels++; i++; } printf(\n\n\t THE TOTAL NUMBER OF VOWELS IS: %d, vowels); getch(); }

Sort N Names use pointers #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 5 #define MAX_STRLEN 256 #define MAX_NAMELEN 128 typedef struct { char *first; char *last; } Name; void sort(Name *a, int n); Name *nameArray; int main(int argc, char *argv[]) { int i; char line[MAX_STRLEN],*p; nameArray = (Name *)calloc(N,sizeof(Name)); printf("\nEnter %d names (First Last), one per line:\n",N); for (i = 0; i < N; i++) { printf("> "); fgets(line,MAX_STRLEN,stdin); *(strchr(line,'\n')) = '\0'; if (strlen(line) > 0) { /* * Could use some more logic here to verify input. * This will work for : First Last */

p = strchr(line,' '); *p++ = '\0'; nameArray[i].first = (char *)malloc(strlen(line)); nameArray[i].last = (char *)malloc(strlen(p)); strcpy(nameArray[i].first,line); strcpy(nameArray[i].last,p); } } sort(nameArray,N); printf("\nSorted:\n"); for (i = 0; i < N; i++) { printf("%s, %s\n", nameArray[i].last, nameArray[i].first); free(nameArray[i].first); free(nameArray[i].last); } free(nameArray); return 0; } /* selection sort */ void sort(Name *a, int n) { int min,i,j; Name t; for (i = 0; i < n; i++) { min = i; for (j = i+1; j < n; j++) { if (strcmp(a[j].last,a[min].last) < 0) min = j; } t = a[min]; a[min] = a[i]; a[i] = t; } } Sample run: Enter 5 names (First Last), one per line: Alex Hinshaw Matt Cain Waldis Joaquin Jeremy Affeldt Bob Howry Sorted: Affeldt, Jeremy Cain, Matt Hinshaw, Alex

Howry, Bob Joaquin, Waldis

Roots of quadratic equation


#include<stdio.h> #include<conio.h> #include<math.h> int main() { int a,b,c; float r1,r2,up; printf("Enter value of a : "); scanf("%d", &a); printf("Enter value of b : "); scanf("%d", &b); printf("Enter value of c : "); scanf("%d", &c); up=(b*b)-(4*a*c); if(up>0) { printf("\n ROOTS ARE REAL ROOTS\n"); r1 = ((-b) + sqrt(up)) /(2*a); r2 = ((-b) - sqrt(up)) /(2*a); printf("\n ROOTS : %f, %f\n", r1, r2); } else if(up==0) { printf("\n ROOTS ARE EQUAL\n"); r1 = (-b/(2*a)); printf("\n ROOT IS...: %f\n", r1); } else printf("\n ROOTS ARE IMAGINARY ROOTS\n"); getch(); return 0; } Output:Enter value of a : 6 Enter value of b : -13 Enter value of c : 6 ROOTS ARE REAL ROOTS ROOTS : 1.500000, 0.666667

Enter value of a : 1 Enter value of b : 1 Enter value of c : -2 ROOTS ARE REAL ROOTS ROOTS : 1.000000, -2.000000

Das könnte Ihnen auch gefallen