Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Types-Computer Fundamentals-Lab Codes, Exercises of Computer Fundamentals

The course covers important and advance elements of C and C plus plus programming language. This course provides the student with the skills required to design, code, and test and execute programs of simple to intermediate complexity. It includes: Float, Input, Matrix, Printing, Subtitution, Equations, Invalid, Stores, Incrementing, Diagnol, Element

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(17)

100 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download Data Types-Computer Fundamentals-Lab Codes and more Exercises Computer Fundamentals in PDF only on Docsity! #include<stdio.h> float input(int a,float mat [][a+1]);//func1:it takes input and stores in a matrix(112-141) void printing(int a,float *mptr);//func2:it prints matrix(195-207) int input_check(int a,float mat[][a+1]);//func3:it checks whether input is right or wrong(145-193) float swap_rows(int a,int e,int c,int g,float mat[][a+1],float swp[g]);//func4:it swaps the rows when diagnol element become 0(209-224) float make_1(int c,int d,int a,float mat[][a+1]);//func5:it makes the diagnol element 1(243-254) float make_0(int i,int g,int d,int a,int c,float mat[][a+1]);//func6:it makes the elements below diagnol element 0(256-268) float back_subtitution(int a,float mat[][a+1]);//func7:it is responsible for back subtitution(226-241) int main() { int m,c,d,e,g,i,ammar=0,a,ali; float *ptr=NULL; printf("how many equations you want to solve\n"); ali=scanf("%d",&a); if(ali==0) { printf("INVALID CHOICE CHOSEN\n"); return 0; } float mat[a][a+1],swp[a+1]; ptr=&mat[0][0]; m=input(a,mat);//func1:it takes input and stores in a matrix(112- 141) if(m==0) { return 0; } printing(a,ptr);//func2:it prints matrix(195-207) ali=input_check(a,mat);//func3:it checks whether input is right or wrong(145-193) if(ali==0) { return 0; } for(ali=0,i=0,d=-1;ali<a;ali++) docsity.com { c=i;//storing initial value of i in c(in c we keep the back record of i.later in end of loop we increment in i using c) i++;//incrementing i d++;//incrementing d g=d;//storing d in g(in g we keep the back record of d.later in end of loop we increment in d using g) if(mat[c][g]==0)//mat[c][g] is a diagnol element.in any case c and g would be equal.with the help of c and g we are able to define the row and column of diagnol element { if(c==a-1)//if diagnol element is from last row { if(mat[c][g+1]==0)//if element of last row and column is zero { printf("THERE ARE INFINITE SOLUTION OF GIVEN EQUATIONS\n"); return 0; } else//if element of last row and column is not zero { printf("THERE IS NO SOLUTION OF GIVEN EQUATIONS\n"); return 0; } } for(e=c+1;e<a;e++)//e ia row counter. it starts from just below the row(c) where diagnol element become 0 { if(mat[e][g]==0)//ammar is acounter of zeroes below the diagnol element { ammar++; } if(mat[e][g]!=0)//if a non zero entry comes below the diagnol element then swaping between row c(where diagnol element was 0) and the row e(where non zero element is found) take place. { swap_rows(a,e,c,g,mat,swp);//func4:it swaps the rows when diagnol element become 0(209-224) break;//it breaks the loop controlled by e(the loop to find a row containing non-zero element in g column and swapping it) } if(ammar==(a-1)-c)//if there are all zeroes below the zero diagnol element then... { docsity.com if(c==a) //x is a counter for zero column.if all elements of a column are zeroes then x will be incemented. { x++; } c=0; } if(x>0)//if x is incremented atleast once then.... { printf("INVALID INPUT\n%d VARIABLE MISSING\nATLEAST ONE VALUE OF A VARIABLE MUST BE NON-ZERO\n",x); return 0; } } void printing(int a,float *mptr)//func2:it prints matrix { int i,j; for(i=0;i<a;i++) { for(j=0;j<=a;j++) { printf("%.1f\t",*(mptr+(j+(i*(a+1)))));//printing matrix using pointers.j is column,i is row ,a+1 is total no. of column.mptr is pointer to first element of a matrix. } printf("\n");//after every row new line will be printed } printf("\n"); } float swap_rows(int a,int e,int c,int g,float mat[][a+1],float swp[g])//func4:it swaps the rows when diagnol element become 0 { for(g=0;g<=a;g++)//g is counter for columns { swp[g]=mat[e][g];//swp is 1D array which stores the complete row containig non zero element in column containing zero diagnol element } for(g=0;g<=a;g++) { mat[e][g]=mat[c][g];//rows are interchanging } for(g=0;g<=a;g++) { mat[c][g]=swp[g];//rows are interchanging } } float back_subtitution(int a,float mat[][a+1])//func7:it is responsible for back subtitution { int p,x,k,i,j; docsity.com p=a;//a(total size of matrix)is storing in p so that a counter can be run on the basis of a x=a; for(k=1;k<p;k++)//k is a counter { for(i=x-2,j=x-1;i>=0;i--) { mat[i][j]=mat[i][j]*mat[x-1][a];//multiplying variables with co-efficients mat[i][a]=mat[i][a]-mat[i][j];//subtracting them from element of constant column in that row } x--; } } float make_1(int c,int d,int a,float mat[][a+1])//func5:it makes the diagnol element 1 { float z; int j,g; g=d; z=mat[c][g];//storing diagnol element in z.if we don't do so then first the diagnol element will be one and then all elements of that rrow will be divided by one.a logical error.while value of z remains same even if diagnaol element becomes 1 for(j=g;j<=a;j++) //j is acolumn counter { mat[c][j]=mat[c][j]/z; } } float make_0(int i,int g,int d,int a,int c,float mat[][a+1])//func6:it makes the elements below diagnol element 0 { int t; for( ;i<a;i++) { t=mat[i][g];//t is storing first non zero element for(d=g;d<=a;d++) { mat[i][d]=mat[i][d]-(mat[c][d]*t);//it subtracts a multiple of a mat[c][d]element with t(the first non zero element of that row) from the elementmat[i][d](element of row whose first element we are going to make zero) } } } docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved