#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nSetting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i>=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}
Chat with our AI personalities
This sounds very much like a homework problem. If you work on it and get started, you found a great place to ask a specific question. However, this is not a place to have your homework done for you.
#include<stdio.h> int main() { double matrix[10][10],a,b, temp[10]; int i, j, k, n; printf("Enter the no of variables: "); scanf("%d", &n); printf("Enter the agumented matrix:\n"); for(i = 0; i < n ; i++){ for(j = 0; j < (n+1); j++){ scanf("%lf", &matrix[i][j]); } } for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ if(j>i){ a = matrix[j][i]; b = matrix[i][i]; for(k = 0; k < n+1; k++){ matrix[j][k] = matrix[j][k] - (a/b) * matrix[i][k]; } } } } printf("The Upper triangular matrix is: \n"); for( i = 0; i < n; i++){ for(j = 0; j < n+1; j++){ printf("%.2f", matrix[i][j]); printf("\t"); } printf("\n"); } printf("\nThe required result is: "); for(i = n-1; i>=0; i--){ b = matrix[i][n]; for(j = n-1 ; j > i; j--){ b -= temp[n-j]*matrix[i][j]; } temp[n-i] = b/matrix[i][i]; printf("\n%c => %.2f",97+i, temp[n-i]); } }
#include<iostream.h> #include<conio.h> # define n 10 void main( ) { int mat[n][n]; int d; // Input elements cout<< "\n Enter dimension of square matrix:"; cin >> d; for(int i = 0; i < d ; i++) for( int j = 0; j < d ; j++) {cout<<"\n Enter elements for "<< i+1 << "," << +1<"location :"; cin >> mat[i][j]; } clrscr(); //Print the array cout<<"\n The Original matrix : \n\n"; for( i = 0; i < d ; i++) {for( j = 0; j < d ; j++) cout<< mat[i][j]<<"\t"; cout<< "\n"; } //upper half of left diagonal..... cout<<"\n The Upper half of the matrix : \n\n"; for( i = 0; i < d ; i++) { for( j = 0; j < d ; j++) { if(i < j) cout << mat [i][j] << " " ; else cout << " " << " "; } cout << "\n "; } //lower half of left diagonal..... cout<<"\n The Lower half of the matrix : \n\n"; for( i = 0; i < d ; i++) { for( j = 0; j < d ; j++) { if(i > j) cout << mat [i][j] << " " ; else cout << " " << " "; } cout << "\n "; } getch ( ); }
By convention, all upper-case is used to identify macros and single capitals to identify template parameters. However, programmers are free to use upper-case as they see fit.
It depends on the language, but in most cases yes, you can return arrays to callers via the return statement. In C, arrays can be returned from a function by reference (that is, by pointer) but never by value (arrays cannot be copied automatically). The array must be allocated on the heap, never on the stack (you cannot return references to local variables). However, beware that returning arrays by reference is unsafe because there's no way to determine the upper bound of the array. This is why many C library functions return multiple values through output parameters and use the return value to indicate error conditions. One way to return both the array and its size via a return statement is by returning a structure: struct array_info { void* array_ptr; /* pointer to first element in array */ int size; /* number of elements in the array */ }; Obviously you must cast the array_ptr member to the appropriate type before dereferencing any of the array elements. In C++, C-style arrays work just as they do in C. However, the preferred method is to use a vector rather than a C-style array. A vector is a class template that encapsulates a C-style array with size and reserve, along with a rich set of useful functions (as with all templates, you don't pay for what you don't use). Vectors can be returned from functions both by value and by reference. Vectors also support move semantics making it possible to return vectors allocated on the stack as well as on the heap. C++ also supports an array class template. This is specifically for fixed-size arrays but is otherwise similar to a vector.