add code for solutions for exam papers

This commit is contained in:
Akbar Rahman 2024-01-23 16:27:24 +00:00
parent 996700bec9
commit a24eb028a9
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF
16 changed files with 426 additions and 0 deletions

View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 10
#define INPUT_BUFFER_LEN 64
/// Used to clear the buffer scanf reads from.
/// Useful when user enters malformed input, as scanf does not clear
/// when it fails to read an input.
#define READ_STDIN_UNTIL_NEWLINE() while (getchar() != '\n'){};
int main() {
int rc, height, i, j;
char *input[INPUT_BUFFER_LEN];
do {
printf("Enter height: ");
rc = scanf("%d", &height);
READ_STDIN_UNTIL_NEWLINE();
if (rc == 1) {
break;
}
} while (1);
for(i = 0; i < height; i++) {
for (j = 0; j < i; j++) {
printf(" ");
}
for (j = 0; j < WIDTH; j++) {
printf("#");
}
printf("\n");
}
}

View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#define BUF_LEN 1024
#define GRADE_FIRST 70
#define GRADE_UPPER_SECOND 60
#define GRADE_LOWER_SECOND 50
#define GRADE_THIRD 40
int main() {
int rc, mark;
FILE *fp;
char buf[BUF_LEN];
char first_name[BUF_LEN];
char last_name[BUF_LEN];
fp = fopen("Results.txt", "r");
if (fp == NULL) {
printf("Unable to open file Results.txt\n");
return 1;
}
while (fgets(buf, BUF_LEN, fp) != NULL) {
rc = sscanf(buf, "%s %s\t%d", first_name, last_name, &mark);
if (rc != 3) {
printf("Failed to read line: %s\n", buf);
return 1;
}
printf("%s %s\t", first_name, last_name);
if (mark >= GRADE_FIRST) {
printf("First\n");
} else if (mark >= GRADE_UPPER_SECOND ) {
printf("Upper second\n");
} else if (mark >= GRADE_LOWER_SECOND) {
printf("Lower second\n");
} else if (mark >= GRADE_THIRD) {
printf("Third\n");
} else {
printf("Fail\n");
}
}
}

View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUF_LEN 1024
/// Used to clear the buffer scanf reads from.
/// Useful when user enters malformed input, as scanf does not clear
/// when it fails to read an input.
#define READ_STDIN_UNTIL_NEWLINE() while (getchar() != '\n'){};
int cone_volume(float *volume, float radius, float height) {
if (radius < 0.0 || height < 0.0) {
return -1;
}
*volume = M_PI*radius*radius*height/3.0;
return 0;
}
int cone_area(float *area, float radius, float height) {
if (radius < 0.0 || height < 0.0) {
return -1;
}
*area = M_PI*radius*(radius + sqrt(radius*radius + height*height));
return 0;
}
int main() {
int rc;
float result, height, radius;
char buf[BUF_LEN];
do {
printf("Enter radius of cone: ");
rc = scanf("%f", &radius);
READ_STDIN_UNTIL_NEWLINE();
} while(rc != 1 && radius < 0.0);
do {
printf("Enter height of cone: ");
rc = scanf("%f", &height);
READ_STDIN_UNTIL_NEWLINE();
} while(rc != 1 && height < 0.0);
rc = cone_area(&result, radius, height);
if(rc != 0) {
printf("Unable to calculate surface area\n");
} else {
printf("Surface area: %.3f\n", result);
}
rc = cone_volume(&result, radius, height);
if(rc != 0) {
printf("Unable to calculate volume\n");
} else {
printf("Volume: %.3f\n", result);
}
}

View File

@ -0,0 +1,4 @@
CFLAGS=-lm
%.c:
$(CC) $(CFLAGS) -c $< -o $*

View File

@ -0,0 +1,15 @@
Fred Bloggs 73
Michael Smith 43
Claire White 64
Simon Black 23
Joshua Mason -20
Jenny Jones 57
George Clark 40
Ann Bell 101
Fay Allen 52
John Morris 60
Steven Hayes 47
Hannah Jackson 94
Alex Benson 50
Susan Baker 70
Wendy Brooks 57

View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE (size_t) 4096
typedef struct {
int time_seconds;
float temperature;
} Datum;
int main() {
int rc, i, data_size;
char buf[BUF_SIZE+1];
char *rc_cp;
FILE *fp;
Datum *data;
printf("Enter filename of temperature data: ");
rc = scanf("%s", buf);
// scanf return number of successfully scanned items
if (rc != 1) {
printf("Unable to read input\n");
return -1;
}
fp = fopen(buf, "r");
if (fp == NULL) {
printf("Unable to open file '%s' for reading\n", buf);
return -1;
}
rc_cp = fgets(buf, BUF_SIZE, fp);
if (rc_cp == NULL) {
printf("Unable to read first line of file\n");
return -1;
}
rc = sscanf(buf, "%d", &data_size);
if (rc != 1) {
printf("Unable to parse first line of file as integer\n");
return -1;
}
data = (Datum*) malloc(data_size * sizeof(Datum));
for(i = 0; i < data_size; i++) {
rc_cp = fgets(buf, BUF_SIZE, fp);
if (rc_cp == NULL) {
printf("Unexpected EOF or file read error\n");
return -1;
}
rc = sscanf(buf, "%d", &data[i].time_seconds);
if (rc != 1) {
printf("Unable to parse line as time (integer): %s\n", buf);
return -1;
}
rc_cp = fgets(buf, BUF_SIZE, fp);
if (rc_cp == NULL) {
printf("Unexpected EOF or file read error\n");
return -1;
}
rc = sscanf(buf, "%f", &data[i].temperature);
if (rc != 1) {
printf("Unable to parse line as temperature (float): %s\n", buf);
return -1;
}
}
printf("Time/s\tTemperature\n");
for(i = 0; i < data_size; i++) {
printf("%d\t%.2f\n", data[i].time_seconds, data[i].temperature);
}
fclose(fp);
free(data);
return 0;
}

View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#define CLR_STDIN() while(getchar() != '\n'){}
/// Calculate kinetic energy of object
///
/// *e - pointer to store calculated energy in
/// m - mass of object, must be greater than 0
/// v - velocity of object
///
/// Returns 1 on success, 0 on failure
int calculate_kinetic_energy(float *e, float m, float v) {
// cannot have negative mass
if (m <= 0.0) return 0;
*e = 0.5 * m * v * v;
return 1;
}
int main() {
int rc;
float e, m = 0.0, v = 0.0;
do {
printf("Enter mass of object: ");
rc = scanf("%f", &m);
CLR_STDIN();
if (rc != 1) {
continue;
}
} while (m <= 0.0);
do {
printf("Enter velocity of object: ");
rc = scanf("%f", &v);
CLR_STDIN();
if (rc != 1) {
continue;
}
break;
} while (1);
rc = calculate_kinetic_energy(&e, m, v);
if (rc != 1) {
printf("calculate_kinetic_energy failed\n");
return -1;
}
printf("Kinetic energy: %.3f kJ\n", e/1000);
return 0;
}

View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CLR_STDIN() while(getchar() != '\n'){}
int main() {
int rc, i = 0;
float input, guess, prev;
do {
printf("Number to square root: ");
rc = scanf("%f", &input);
CLR_STDIN();
if (rc != 1) {
continue;
}
} while (input <= 0.0);
guess = input/3;
do {
i++;
prev = guess;
guess = 0.5 * (prev + (input/prev));
printf("Input: %.5f Result: %.5f Iterations: %d Error: %f\n", input, guess, i, labs(prev-guess)/input);
} while ( fabs((prev-guess)/input) > 0.000001 );
printf("Input: %.5f Result: %.5f Iterations: %d\n", input, guess, i);
return 0;
}

View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CLR_STDIN() while(getchar() != '\n'){}
int polygon_area(float *p_area, unsigned int sides, float length) {
if (sides < 3) return 0;
*p_area = length*length*(float)sides / (4*tan(M_PI/sides));
return 1;
}
int main() {
int rc;
unsigned int sides = 0;
float length, area;
polygon_area(&area, sides, length);
do {
printf("Enter number of sides: ");
rc = scanf("%u", &sides);
CLR_STDIN();
if (rc == 1) {
break;
}
} while (1);
do {
printf("Enter side length: ");
rc = scanf("%f", &length);
CLR_STDIN();
if (rc == 1) {
break;
}
} while (1);
rc = polygon_area(&area, sides, length);
if (rc == 0) {
printf("Error\n");
return 1;
}
printf("Area: %.2f\n", area);
return 0;
}

View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUF_LEN 4096
#define CLR_STDIN() while(getchar() != '\n'){}
int main() {
char buf[BUF_LEN+1];
char letter;
unsigned int mode;
FILE *fp;
int rc;
do {
printf("Enter file name: ");
rc = scanf("%s", buf);
if (rc == 1) break;
} while(1);
fp = fopen(buf, "r");
if (fp == NULL) {
printf("Unable to open file '%s'\n", buf);
return -1;
}
printf("---Formats---\n");
printf("1) Character only\n");
printf("2) ASCII only\n");
printf("3) Character and ASCII\n");
do {
printf("Select format: ");
rc = scanf("%d", &mode);
CLR_STDIN();
if (rc == 1 && mode < 4 && mode > 0) break;
} while(1);
while ((letter = fgetc(fp)) != EOF) {
if (mode == 1 || mode == 3) printf("Character: %c", letter);
if (mode == 3) printf(" ");
if (mode == 2 || mode == 3) printf("ASCII: %u", letter);
printf("\n");
}
fclose(fp);
return 0;
}

View File

@ -0,0 +1,4 @@
CFLAGS=-lm
%.c:
$(CC) $(CFLAGS) -c $< -o $*