Vous êtes sur la page 1sur 18

Lab No.

10

Name: YOGESH PARTH

Roll No. SC11B128

Dept: Avionics

Sub: C Programming Lab

Question 1.
Matrix Manipulation:
Write a C program to find sum, product, trace of given two 22 matrices. Each function should be
written in separate file. use appropriate inclusion directive to combine all the files to create one
main file. use separate file to read the data from user and to display the results to user.
Program code:
Sum:
#include<stdio.h>
void sum(int a[2][2], int b[2][2], int c[2][2]){
int i=0, j=0;
printf("Sum of Matrices:\n");
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
c[i][j]=a[i][j]+b[i][j];
printf("sum[%d][%d]=
%d\n",i,j,c[i][j]);
}
}
}
Product:
#include<conio.h>
#include<stdio.h>
void prod(int a[2][2], int b[2][2], int c[2][2]){
int i=0, j=0,m=0;
printf("Product of the Matrices:\n");
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
c[i][j]=0;
for(m=0;m<=1;m++){
c[i][j]+=a[i][m]*b[m][j];
}
printf("prd[%d][%d]=
%d\n",i,j,c[i][j]);
}
}
}
Sum column:
#include<stdio.h>
void sum_col(int a[2][2],int c[1][2]){

int i=0,j=0;
c[0][0]=0;
c[0][1]=0;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
c[0][i]+=a[j][i];
}
printf("sumofcol[0][%d]= %d\n",i,c[0][i]);
}
}
Sum row:
#include<stdio.h>
void sum_row(int a[2][2],int c[2][1]){
int i=0,j=0;
c[0][0]=0;
c[1][0]=0;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
c[i][0]+=a[i][j];
}
printf("sumofrow[%d][0]= %d\n",i,c[i][0]);
}
}
Trace:
#include<stdio.h>
void trace(int a[2][2]){
int tr=0;
int i=0,j=0;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
if(i==j)
tr+=a[i][j];
}
}
printf("trace of matrix= %d\n",tr);
}
Main:
#include"sum.cpp"
#include"prod.cpp"
#include"sum_col.cpp"
#include"sum_row.cpp"
#include"trace.cpp"
#include<conio.h>
#include<stdio.h>
main(){
int a[2][2],b[2][2],p[2][2],s[2][2],sc[1][2],sr[2][1];

int i=0,j=0;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
printf("Enter
a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
printf("Enter
b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
prod(a,b,p);
sum(a,b,s);
sum(a,s,s);
sum_col(a,sc);
sum_row(a,sr);
trace(a);
getch();
}

Result:

Discussion:
Programs are actually retrieved from different c files, which are included in the pre-processor directives.

Question 2:
Program code for Macro.c::
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define __AREA__(a,b,c,s) sqrt(s*(s-a)*(s-b)*(s-c))
#define __SEMI__(a,b,c) (a+b+c)/2
int main(){
int a,b,c,ar,s;
printf("Enter a, b, c:\n");
scanf("%d %d %d",&a,&b,&c);
s=__SEMI__(a,b,c);
ar=__AREA__(a,b,c,s);
printf("The area of triangle is %d sq units\n",ar);
getch();
}
Result:

Discussion:

Parameters are allowed in macro.

Question 3: Area of circle


Program code:
#include<conio.h>
#include<stdio.h>
#include<math.h>
#ifndef pi
#define pi 3.1416
#endif
int main(){
float r;
printf("Enter the radius: \n");
scanf("%f",&r);
float ar=pi*r*r;
printf("The area is :%f\n",ar);
getch();
return(0);
}
Result:
Enter the radius
1
The area is: 3.141600
Discussion:
We have to redefine the value of pi as 3.1416 .

Question 4:
For printing error.

Program code:
#include<stdio.h>
#include<conio.h>
#ifndef __cplusplus
#error your IDE doesn't support c++
#endif

int main(){
}

Result:
#error your IDE doesn't support c++
Discussion:
As the file i.e used is error.c, the error occurs. If, otherwise, the filename was error.cpp, the error
would not have appeared.

Question 5: Sine function generation.

Program code:
#include<stdio.h>
#include<math.h>
#define pi 3.14159
int main(){
int i;
float x[512];
x[0]=0;
FILE *fp;
fp=fopen("sin_fun.txt","w");
for(i=1;i<=511;i++){
x[i]= x[i-1]+(8*pi/512);
}
float val[512];
for(i=0;i<512;i++){
val[i]=sin(x[i]);
fprintf(fp,"%f %f\n",x[i],val[i]);
}
fclose(fp);
return 0;
}
Result:
0.000000 0.000000
0.049087 0.049068
0.098175 0.098017
0.147262 0.146730
0.196349 0.195090
0.245437 0.242980
0.294524 0.290284

0.343611 0.336890
0.392699 0.382683
0.441786 0.427555
0.490873 0.471396
0.539961 0.514102
0.589048 0.555570
0.638135 0.595699
0.687223 0.634393
0.736310 0.671558
0.785398 0.707106
0.834485 0.740951
0.883572 0.773010
0.932660 0.803207
0.981747 0.831469
1.030834 0.857728
1.079921 0.881921
1.129009 0.903989
1.178096 0.923879
1.227183 0.941544
1.276271 0.956940
1.325358 0.970031
1.374445 0.980785
1.423532 0.989176
1.472620 0.995185
1.521707 0.998795
1.570794 1.000000
1.619882 0.998796
1.668969 0.995185
1.718056 0.989177
1.767143 0.980786
1.816231 0.970032
1.865318 0.956941
1.914405 0.941545
1.963493 0.923881
2.012580 0.903991
2.061667 0.881923
2.110754 0.857730
2.159842 0.831471
2.208929 0.803209
2.258016 0.773013
2.307104 0.740953
2.356191 0.707109
2.405278 0.671562
2.454365 0.634396
2.503453 0.595702
2.552540 0.555574
2.601627 0.514106
2.650715 0.471400

2.699802 0.427559
2.748889 0.382687
2.797976 0.336894
2.847064 0.290289
2.896151 0.242985
2.945238 0.195095
2.994326 0.146735
3.043413 0.098022
3.092500 0.049073
3.141587 0.000005
3.190675 -0.049062
3.239762 -0.098012
3.288849 -0.146725
3.337937 -0.195085
3.387024 -0.242975
3.436111 -0.290279
3.485198 -0.336884
3.534286 -0.382678
3.583373 -0.427550
3.632460 -0.471391
3.681548 -0.514097
3.730635 -0.555565
3.779722 -0.595694
3.828809 -0.634388
3.877897 -0.671554
3.926984 -0.707102
3.976071 -0.740947
4.025159 -0.773006
4.074246 -0.803204
4.123334 -0.831466
4.172421 -0.857725
4.221509 -0.881918
4.270597 -0.903987
4.319684 -0.923877
4.368772 -0.941542
4.417859 -0.956939
4.466947 -0.970030
4.516034 -0.980784
4.565122 -0.989176
4.614209 -0.995184
4.663297 -0.998795
4.712384 -1.000000
4.761472 -0.998796
4.810559 -0.995185
4.859647 -0.989177
4.908734 -0.980786
4.957822 -0.970032
5.006909 -0.956941

5.055997 -0.941545
5.105084 -0.923881
5.154172 -0.903991
5.203259 -0.881923
5.252347 -0.857730
5.301435 -0.831471
5.350522 -0.803209
5.399610 -0.773012
5.448697 -0.740953
5.497785 -0.707109
5.546872 -0.671561
5.595960 -0.634395
5.645047 -0.595701
5.694135 -0.555572
5.743222 -0.514104
5.792310 -0.471398
5.841397 -0.427556
5.890485 -0.382685
5.939572 -0.336891
5.988660 -0.290286
6.037747 -0.242981
6.086835 -0.195091
6.135922 -0.146731
6.185010 -0.098018
6.234097 -0.049068
6.283185 0.000000
6.332273 0.049068
6.381360 0.098017
6.430448 0.146731
6.479535 0.195091
6.528623 0.242981
6.577710 0.290285
6.626798 0.336890
6.675885 0.382684
6.724973 0.427556
6.774060 0.471398
6.823148 0.514104
6.872235 0.555571
6.921323 0.595701
6.970410 0.634395
7.019498 0.671560
7.068585 0.707108
7.117673 0.740952
7.166760 0.773012
7.215848 0.803209
7.264935 0.831471
7.314023 0.857730
7.363111 0.881923

7.412198 0.903991
7.461286 0.923881
7.510373 0.941545
7.559461 0.956941
7.608548 0.970032
7.657636 0.980786
7.706723 0.989177
7.755811 0.995185
7.804898 0.998796
7.853986 1.000000
7.903073 0.998795
7.952161 0.995184
8.001248 0.989176
8.050336 0.980784
8.099423 0.970030
8.148511 0.956939
8.197598 0.941542
8.246686 0.923878
8.295774 0.903987
8.344861 0.881919
8.393949 0.857726
8.443036 0.831466
8.492124 0.803204
8.541211 0.773007
8.590299 0.740947
8.639386 0.707102
8.688474 0.671554
8.737561 0.634388
8.786649 0.595694
8.835736 0.555564
8.884824 0.514097
8.933911 0.471390
8.982999 0.427548
9.032086 0.382677
9.081174 0.336883
9.130261 0.290277
9.179349 0.242973
9.228436 0.195082
9.277524 0.146722
9.326612 0.098009
9.375699 0.049059
9.424787 -0.000009
9.473874 -0.049076
9.522962 -0.098026
9.572049 -0.146739
9.621137 -0.195099
9.670224 -0.242989
9.719312 -0.290294

9.768399 -0.336899
9.817487 -0.382692
9.866574 -0.427564
9.915662 -0.471406
9.964749 -0.514111
10.013837 -0.555579
10.062924 -0.595708
10.112012 -0.634401
10.161099 -0.671567
10.210187 -0.707114
10.259274 -0.740959
10.308362 -0.773018
10.357450 -0.803214
10.406537 -0.831476
10.455625 -0.857735
10.504712 -0.881927
10.553800 -0.903994
10.602887 -0.923884
10.651975 -0.941548
10.701062 -0.956944
10.750150 -0.970034
10.799237 -0.980788
10.848325 -0.989178
10.897412 -0.995186
10.946500 -0.998796
10.995587 -1.000000
11.044675 -0.998795
11.093762 -0.995183
11.142850 -0.989175
11.191937 -0.980783
11.241025 -0.970028
11.290112 -0.956936
11.339200 -0.941539
11.388288 -0.923874
11.437375 -0.903983
11.486463 -0.881914
11.535550 -0.857721
11.584638 -0.831461
11.633725 -0.803199
11.682813 -0.773001
11.731900 -0.740941
11.780988 -0.707096
11.830075 -0.671548
11.879163 -0.634381
11.928250 -0.595687
11.977338 -0.555557
12.026425 -0.514089
12.075513 -0.471383

12.124600 -0.427540
12.173688 -0.382668
12.222775 -0.336874
12.271863 -0.290269
12.320951 -0.242964
12.370038 -0.195074
12.419126 -0.146714
12.468213 -0.098000
12.517301 -0.049050
12.566388 0.000018
12.615476 0.049085
12.664563 0.098035
12.713651 0.146748
12.762738 0.195108
12.811826 0.242998
12.860913 0.290302
12.910001 0.336907
12.959088 0.382701
13.008176 0.427572
13.057263 0.471413
13.106351 0.514119
13.155438 0.555586
13.204526 0.595715
13.253613 0.634408
13.302701 0.671573
13.351789 0.707121
13.400876 0.740964
13.449964 0.773023
13.499051 0.803220
13.548139 0.831481
13.597226 0.857739
13.646314 0.881931
13.695401 0.903998
13.744489 0.923887
13.793576 0.941551
13.842664 0.956946
13.891751 0.970036
13.940839 0.980789
13.989926 0.989180
14.039014 0.995187
14.088101 0.998797
14.137189 1.000000
14.186276 0.998794
14.235364 0.995183
14.284451 0.989173
14.333539 0.980781
14.382627 0.970026
14.431714 0.956934

14.480802 0.941536
14.529889 0.923871
14.578977 0.903979
14.628064 0.881910
14.677152 0.857717
14.726239 0.831456
14.775327 0.803193
14.824414 0.772995
14.873502 0.740935
14.922589 0.707090
14.971677 0.671541
15.020764 0.634374
15.069852 0.595680
15.118939 0.555550
15.168027 0.514081
15.217114 0.471375
15.266202 0.427532
15.315289 0.382660
15.364377 0.336866
15.413465 0.290260
15.462552 0.242955
15.511640 0.195065
15.560727 0.146705
15.609815 0.097991
15.658902 0.049041
15.707990 -0.000026
15.757077 -0.049094
15.806165 -0.098044
15.855252 -0.146757
15.904340 -0.195117
15.953427 -0.243006
16.002514 -0.290310
16.051601 -0.336915
16.100689 -0.382708
16.149776 -0.427579
16.198864 -0.471420
16.247952 -0.514126
16.297039 -0.555593
16.346127 -0.595721
16.395214 -0.634414
16.444302 -0.671579
16.493389 -0.707126
16.542477 -0.740970
16.591564 -0.773028
16.640652 -0.803224
16.689739 -0.831485
16.738827 -0.857743
16.787914 -0.881935

16.837002 -0.904002
16.886089 -0.923891
16.935177 -0.941554
16.984264 -0.956949
17.033352 -0.970038
17.082439 -0.980791
17.131527 -0.989181
17.180614 -0.995188
17.229702 -0.998797
17.278790 -1.000000
17.327877 -0.998794
17.376965 -0.995182
17.426052 -0.989172
17.475140 -0.980779
17.524227 -0.970024
17.573315 -0.956931
17.622402 -0.941534
17.671490 -0.923868
17.720577 -0.903976
17.769665 -0.881907
17.818752 -0.857712
17.867840 -0.831452
17.916927 -0.803189
17.966015 -0.772990
18.015102 -0.740930
18.064190 -0.707084
18.113277 -0.671535
18.162365 -0.634368
18.211452 -0.595673
18.260540 -0.555543
18.309628 -0.514075
18.358715 -0.471368
18.407803 -0.427525
18.456890 -0.382653
18.505978 -0.336858
18.555065 -0.290253
18.604153 -0.242948
18.653240 -0.195057
18.702328 -0.146697
18.751415 -0.097983
18.800503 -0.049033
18.849590 0.000034
18.898678 0.049102
18.947765 0.098052
18.996853 0.146765
19.045940 0.195125
19.095028 0.243014
19.144115 0.290318

19.193203 0.336923
19.242290 0.382716
19.291378 0.427587
19.340466 0.471428
19.389553 0.514134
19.438641 0.555600
19.487728 0.595728
19.536816 0.634421
19.585903 0.671586
19.634991 0.707133
19.684078 0.740976
19.733166 0.773034
19.782253 0.803230
19.831341 0.831490
19.880428 0.857748
19.929516 0.881939
19.978603 0.904005
20.027691 0.923894
20.076778 0.941557
20.125866 0.956951
20.174953 0.970041
20.224041 0.980793
20.273129 0.989182
20.322216 0.995189
20.371304 0.998797
20.420391 1.000000
20.469479 0.998794
20.518566 0.995181
20.567654 0.989171
20.616741 0.980778
20.665829 0.970022
20.714916 0.956929
20.764004 0.941531
20.813091 0.923864
20.862179 0.903972
20.911266 0.881902
20.960354 0.857708
21.009441 0.831447
21.058529 0.803183
21.107616 0.772985
21.156704 0.740924
21.205791 0.707078
21.254879 0.671528
21.303967 0.634361
21.353054 0.595666
21.402142 0.555536
21.451229 0.514067
21.500317 0.471360

21.549404 0.427517
21.598492 0.382644
21.647579 0.336850
21.696667 0.290244
21.745754 0.242939
21.794842 0.195048
21.843929 0.146688
21.893017 0.097974
21.942104 0.049025
21.991192 -0.000043
22.040279 -0.049111
22.089367 -0.098060
22.138454 -0.146774
22.187542 -0.195133
22.236629 -0.243023
22.285717 -0.290327
22.334805 -0.336932
22.383892 -0.382724
22.432980 -0.427595
22.482067 -0.471436
22.531155 -0.514141
22.580242 -0.555608
22.629330 -0.595736
22.678417 -0.634428
22.727505 -0.671593
22.776592 -0.707139
22.825680 -0.740982
22.874767 -0.773040
22.923855 -0.803235
22.972942 -0.831495
23.022030 -0.857752
23.071117 -0.881943
23.120205 -0.904009
23.169292 -0.923897
23.218380 -0.941560
23.267467 -0.956954
23.316555 -0.970043
23.365643 -0.980794
23.414730 -0.989183
23.463818 -0.995189
23.512905 -0.998798
23.561993 -1.000000
23.611080 -0.998793
23.660168 -0.995180
23.709255 -0.989169
23.758343 -0.980776
23.807430 -0.970019
23.856518 -0.956926

23.905605 -0.941528
23.954693 -0.923861
24.003780 -0.903968
24.052868 -0.881898
24.101955 -0.857703
24.151043 -0.831442
24.200130 -0.803178
24.249218 -0.772979
24.298306 -0.740918
24.347393 -0.707071
24.396481 -0.671522
24.445568 -0.634354
24.494656 -0.595659
24.543743 -0.555528
24.592831 -0.514059
24.641918 -0.471352
24.691006 -0.427509
24.740093 -0.382636
24.789181 -0.336842
24.838268 -0.290236
24.887356 -0.242930
24.936443 -0.195040
24.985531 -0.146679
25.034618 -0.097965
25.083706 -0.049016

Discussion:
Using mat lab we can plot graphs by raw data generated.
Question 6(Bonus): For self display.
Program code:
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
fp=fopen("self_disp.cpp","r");
while(!(feof(fp))){
printf("%c",fgetc(fp));
}
getch();
return 0;
}

Result:
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
fp=fopen("self_disp.cpp","r");
while(!(feof(fp))){
printf("%c",fgetc(fp));
}
getch();
return 0;
}
Discussion:
feof function is always useful as it reduces the code-length very much.
.

Vous aimerez peut-être aussi