Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Ex 3, point 3

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

void selectionsort(int n, int grade[n], char name[n][20], char surname[n]


[20], int top) {
for(int i = 0; i < top; i++) {
int maxindex = i;
for(int j = i + 1; j < n; j++) {
if(grade[j] > grade[maxindex]) {
maxindex = j;
}
}
int temp = grade[i];
grade[i] = grade[maxindex];
grade[maxindex] = temp;
// Swap corresponding names and surnames
char tempName[20];
strcpy(tempName, name[i]);
strcpy(name[i], name[maxindex]);
strcpy(name[maxindex], tempName);

char tempSurname[20];
strcpy(tempSurname, surname[i]);
strcpy(surname[i], surname[maxindex]);
strcpy(surname[maxindex], tempSurname);
}

for(int i = 0; i < top; i++) {


printf("%s %s - %d\n", name[i], surname[i], grade[i]);
}
}

void searchdata(FILE *fp, int n, char dep[], int top) {


int id;
int grade;
char name[20];
char surname[20];
char department[20];
int studentGradesInDep[n];
char studentNamesInDep[n][20];
char studentSurnamesInDep[n][20];
for (int i = 0; i < n; i++) {
studentGradesInDep[i] = 0;
}

int count = 0;
for(int i = 0; i < n; i++) {
fscanf(fp, "%d %s %s %s %d", &id, name, surname, department,
&grade);
if(strcmp(department, dep) == 0) {
studentGradesInDep[count] = grade;
strcpy(studentNamesInDep[count], name);
strcpy(studentSurnamesInDep[count], surname);
count++;
}
}
selectionsort(count, studentGradesInDep, studentNamesInDep,
studentSurnamesInDep, top);
}
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if(fp == NULL)
printf("File cannot be opened");
else {
char dep[] = "CEN";
int top = 5;
int n;
fscanf(fp, "%d", &n);
searchdata(fp, n, dep, top);
fclose(fp);
}
return 0;
}

Ex5, ex4 based on the lab exercises I have posted


#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 100
void copyFileContents(FILE *source, FILE *destination) {
char ch;
// Read one character at a time from source and write to destination
// until reaching the End Of File
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
}
int main() {
FILE *sourceFile, *destinationFile;
char topic[MAX_LENGTH];
printf("Enter a topic for revision (max 100 characters):\n");
//99: Specifies the maximum number of characters to read.
//In this case, it specifies that scanf should read at most 99 characters
//(leaving space for the null terminator \0) from the input stream.
//The scanner will read until a new line is met, meaning a single string in
a single line.
scanf("%99[^\n]", topic);
// Open source file
sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL) {
printf("Error opening file for reading.\n");
return EXIT_FAILURE;
}
// Open destination file
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL) {
fclose(sourceFile);
printf("Error opening file for writing.\n");
return EXIT_FAILURE;
}
// Copy contents a)
copyFileContents(sourceFile, destinationFile);
//Write topic to file b)
fprintf(destinationFile, "%s", topic);
// Close files
fclose(sourceFile);
fclose(destinationFile);
printf("Content copied from source to destination successfully\n");
return EXIT_SUCCESS;
}

You might also like