Monash University > CSSE > CSE1303 > Part A > Lectures > Lecture A06 notes
#include <stdio.h>
#include <stdlib.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
/* This function reads in a student's record. */
Student readStudent(void)
{
Student next;
scanf("%s %d", next.name, &next.mark);
return next;
}
/* This function prints out a student's record. */
void printStudent(const Student student)
{
printf("%s %d\n", student.name, student.mark);
}
/* This program reads in the student's records for a
* class and prints out the first student's record it
* finds with the highest mark. */
int main()
{
Student* class = NULL;
int n;
int best = 0;
int i;
printf("Enter number of students in the class: ");
scanf("%d", &n);
class = (Student*)malloc(n * sizeof(Student));
if (class != NULL)
{
for (i = 0; i < n; i++)
{
class[i] = readStudent();
if (class[best].mark < class[i].mark)
{
best = i;
}
}
printf("Best student is: ");
printStudent(class[best]);
}
}
#include <stdio.h>
#include <stdlib.h>
/* This function creates a n x m matrix of float; and
* returns a pointer to this matrix. */
float** makeMatrix(int n, int m)
{
float* memoryPtr;
float** matrixPtr;
int i;
memoryPtr = (float*)malloc(n*m*sizeof(float));
matrixPtr = (float**)malloc(n*sizeof(float*));
if (memoryPtr == NULL || matrixPtr == NULL)
{
fprintf(stderr, "Not enough memory\n");
exit(1);
}
for (i = 0; i < n; i++, memoryPtr += m)
{
matrixPtr[i] = memoryPtr;
}
return matrixPtr;
}