0% found this document useful (0 votes)
43 views4 pages

Skema Jawapan Test 2

The document discusses functions for calculating properties of a circle like diameter, circumference and area. It also contains code to add two matrices and read/write data from files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Skema Jawapan Test 2

The document discusses functions for calculating properties of a circle like diameter, circumference and area. It also contains code to add two matrices and read/write data from files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

q1(a)_g1

i. stdio.h
ii. math.h
iii. stdlib.h

q1(b)_g1

#include <stdio.h>

double getDiameter(double radius);


double getCircumference(double radius);
double getArea(double radius);

int main()
{
float radius, dia, circ, area;
printf("Enter radius of circle (in cm): ");
scanf("%f", &radius);
dia = getDiameter(radius);
circ = getCircumference(radius);
area = getArea(radius);
printf("Diameter of the circle = %.2f cm\n", dia);
printf("Circumference of the circle = %.2f cm\n", circ);
printf("Area of the circle = %.2f cm^2", area);
return 0;
}

double getDiameter(double radius)


{
return (2 * radius);
}

double getCircumference(double radius)


{
const float pi=3.14159;
return (2 * pi * radius);
}

double getArea(double radius)


{
const float pi=3.14159;
return (pi* radius * radius);
}
q2_g1

#include <stdio.h>

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

int row, col, sz1, sz2;


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

printf("\nEnter elements in matrix B of size %dx%d: \n",sz1,sz2);


for(row=0; row<sz1; row++)
{
for(col=0; col<sz2; col++)
{
scanf("%d", &B[row][col]);
}
}
for(row=0; row<sz1; row++)
{
for(col=0; col<sz2; col++)
{
C[row][col] = A[row][col] + B[row][col];
}
}
printf("\nSum of matrices A+B = \n");
for(row=0; row<sz1; row++)
{
for(col=0; col<sz2; col++)
{
printf("%d ", C[row][col]);
}
printf("\n");
}

return 0;
}
q3 (a)_g1

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

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

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


out_file= fopen("marks.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);
}

fclose(in_file);
fclose(out_file);
return 0;
}

q3 (b)_g1

#include <stdio.h>
int main (void) {
int no;
char name[30];
float mark;
FILE *cfPtr;

if ((cfPtr = fopen("students.txt", "r")) == NULL)


printf("File cant be opened");
else
{
printf("%s\t%s\t%s\n", "No","Name", "Mark");
fscanf(cfPtr, "%d%s%f",&no, name, &mark);
while(!feof(cfPtr)) {
printf("%d\t%s\t%.2f\n",no, name, mark);
fscanf(cfPtr, "%d\t%s\t%f",&no, name, &mark);
}
fclose(cfPtr);
}
return 0;
}

You might also like