Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

q1(a)_g3

i. We can avoid rewriting same logic/code again and again in a program.


ii. We can call C functions any number of times in a program and from any place in a program.
iii.We can track a large C program easily when it is divided into multiple functions.

q1(b)_g3
#include <stdio.h>

double getBMI(double,double);
void chart(double);

int main()
{
float weight,height,BMI;
printf("Enter weight (in kg) and height (in m): ");
scanf("%f%f", &weight,&height);
BMI = getBMI(weight,height);
chart(BMI);
return 0;
}

double getBMI(double w,double h)


{
return (w/(h*h));
}
void chart(double bmi)
{
if (bmi>=18.5&&bmi<24.9)
printf("BMI = %.2f\nnormal\n", bmi);
else if (bmi>=25&&bmi<29.9)
printf("BMI = %.2f\noverweight\n", bmi);
else if (bmi>=30&&bmi<34.9)
printf("BMI = %.2f\nobese\n", bmi);
else if (bmi>=35&&bmi<39.9)
printf("BMI = %.2f\nseverely obese\n", bmi);
else
printf("BMI = %.2f\nmorbid obese\n", bmi);
}
q2_g3

#include <stdio.h>
#define SIZE 2

int main()
{
int A[SIZE][SIZE];
int B[SIZE][SIZE];
int C[SIZE][SIZE];

int row, col, i, sum;


printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &A[row][col]);
}
}
printf("\nEnter elements in matrix B of size %dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &B[row][col]);
}
}
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
sum = 0;
for(i=0; i<SIZE; i++)
{
sum += A[row][i] * B[i][col];
}

C[row][col] = sum;
}
}
printf("\nProduct of matrix A * B = \n");
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
printf("%d ", C[row][col]);
}
printf("\n");
}
return 0;
}

q3 (a)_g3

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int no;
char name[30];
float mark,high,low;
FILE *in_file;
FILE *out_file;

in_file = fopen("students.dat", "r");


out_file= fopen("highnlow.out", "w");
if(in_file == NULL){
printf("Error opening file\n");
exit(-1);
}

while(!feof(in_file))
{
fscanf(in_file, "%d%s%f",&no, name, &mark);
fprintf(out_file, "%d\t%s\t%.2f\n",no, name, mark);
if (no==3)
high=mark;
if (no==6)
low=mark;
}

fprintf(out_file,"\nHighest value is %.2f",high);


fprintf(out_file,"\nLowest value is %.2f",low);
fclose(in_file);
fclose(out_file);
return 0;
}

q3 (b)_g3

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int no;
char name[30];
float mark,high,low;
FILE *in_file;
in_file = fopen("students.dat", "r");
if(in_file == NULL){
printf("Error opening file\n");
exit(-1);
}
printf("%s\t%s\t%s\n", "No","Name", "Mark");
while(!feof(in_file))
{
fscanf(in_file, "%d%s%f",&no, name, &mark);
printf("%d\t%s\t%.2f\n",no, name, mark);
if (no==3)
high=mark;
if (no==6)
low=mark;
}

printf("\nHighest value is %.2f%",high);


printf("\nLowest value is %.2f%\n",low);
fclose(in_file);
return 0;
}

You might also like