feat: update structure
This commit is contained in:
17
cs2106/labs/lab1/part1/lab1p1.c
Normal file
17
cs2106/labs/lab1/part1/lab1p1.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include "queue.h"
|
||||
|
||||
int main() {
|
||||
double v;
|
||||
|
||||
for(int i = 0; i<= MAX_Q_SIZE; i++) {
|
||||
v = ((double) i / 10.0);
|
||||
printf("Adding %3.2f\n", v);
|
||||
enq(v);
|
||||
}
|
||||
|
||||
for(int i = 0; i<= MAX_Q_SIZE; i++) {
|
||||
v = deq();
|
||||
printf("Element %d is %3.2f\n", i, v);
|
||||
}
|
||||
}
|
||||
31
cs2106/labs/lab1/part1/lab1p1a.c
Normal file
31
cs2106/labs/lab1/part1/lab1p1a.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int (*fptr)(int);
|
||||
|
||||
int func(int x) {
|
||||
return 2 * x;
|
||||
}
|
||||
|
||||
|
||||
int y = 10;
|
||||
|
||||
int *(*pfptr)();
|
||||
|
||||
int *func2() {
|
||||
return &y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Calling func with value 6: %d\n", func(6));
|
||||
printf("Now setting fptr to point to func.\n");
|
||||
fptr = func;
|
||||
printf("Caling fptr with value 6: %d\n", fptr(6));
|
||||
|
||||
printf("\nNow caling func2 which returns the address of global variable y: %p\n", func2());
|
||||
printf("Pointing pfptr to func2.\n");
|
||||
pfptr = func2;
|
||||
printf("Now calling fpfptr: %p\n", pfptr());
|
||||
}
|
||||
|
||||
|
||||
|
||||
83
cs2106/labs/lab1/part1/queue.c
Normal file
83
cs2106/labs/lab1/part1/queue.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "queue.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static double _queue[MAX_Q_SIZE];
|
||||
static int _front = 0, _rear = 0;
|
||||
|
||||
void enq(double data) {
|
||||
if ((_front + 1) % MAX_Q_SIZE == _rear){
|
||||
printf("Error: Queue is full. Item value %3.2f is not added.\n", data);
|
||||
}
|
||||
else {
|
||||
_queue[_front] = data;
|
||||
_front = (_front + 1) % MAX_Q_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
double deq() {
|
||||
double val = -1;
|
||||
if (_rear == _front){
|
||||
printf("Error: Queue is empty. Nothing to return\n");
|
||||
}
|
||||
else {
|
||||
val = _queue[_rear];
|
||||
_rear = (_rear + 1) % MAX_Q_SIZE;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
/* This section is for the function pointers exercise */
|
||||
|
||||
static double _res;
|
||||
|
||||
void sum(double x) {
|
||||
_res += x;
|
||||
}
|
||||
|
||||
void prod(double x) {
|
||||
_res *= x;
|
||||
}
|
||||
|
||||
void clear_sum() {
|
||||
_res = 0;
|
||||
}
|
||||
|
||||
void clear_prod() {
|
||||
_res = 1.0;
|
||||
}
|
||||
|
||||
double reduce() {
|
||||
int ndx = _rear;
|
||||
|
||||
clear_sum();
|
||||
while(ndx != _front) {
|
||||
sum(_queue[ndx]);
|
||||
ndx = (ndx + 1) % MAX_Q_SIZE;
|
||||
}
|
||||
return _res;
|
||||
}
|
||||
|
||||
/* Implement flex_reduce here:
|
||||
|
||||
double flex_reduce(clear, op){
|
||||
// clear(); // Clear _res to either 0 or 1
|
||||
// for every element in queue:
|
||||
//Call op with element.
|
||||
|
||||
return _res;
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
double flex_reduce(void (*clear)(), void (*op)(double)) {
|
||||
int ndx = _rear;
|
||||
|
||||
clear();
|
||||
while(ndx != _front) {
|
||||
op(_queue[ndx]);
|
||||
ndx = (ndx + 1) % MAX_Q_SIZE;
|
||||
}
|
||||
return _res;
|
||||
}
|
||||
17
cs2106/labs/lab1/part1/queue.h
Normal file
17
cs2106/labs/lab1/part1/queue.h
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
// Maximum number of elements in a queue
|
||||
//
|
||||
#define MAX_Q_SIZE 10
|
||||
|
||||
// Place function prototypes here.
|
||||
|
||||
void enq(double);
|
||||
double deq();
|
||||
|
||||
void sum(double);
|
||||
void prod(double);
|
||||
void clear_sum();
|
||||
void clear_prod();
|
||||
double reduce();
|
||||
|
||||
double flex_reduce(void (*)(), void (*)(double));
|
||||
20
cs2106/labs/lab1/part1/testr.c
Normal file
20
cs2106/labs/lab1/part1/testr.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include "queue.h"
|
||||
|
||||
int main() {
|
||||
|
||||
int i;
|
||||
for(i=1; i<=9; i++)
|
||||
{
|
||||
printf("Enqueing %d\n", i);
|
||||
enq((double) i);
|
||||
}
|
||||
|
||||
printf("\nCalling reduce result is %3.2f\n", reduce());
|
||||
|
||||
/* Uncomment the following two statements to test flex_reduce */
|
||||
|
||||
printf("Calling flex reduce with sum. Result is %3.2f\n", flex_reduce(clear_sum, sum));
|
||||
printf("Calling flex reduce with prod. Result is %3.2f\n", flex_reduce(clear_prod, prod));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user