Google

Saturday, April 17, 2010

CSC202 Coursework Marks (April 17)




Congrats! Everyone has done very well scoring above 50% of
the Total Coursework marks.

Good luck in your Finals!



Wednesday, April 14, 2010

CSC202 Revision April 15


QUESTION:

(a) Use the typedef construct to define a type EMPLOYEE for a structure employee_record of the
following properties:
Name
Salary
Age
Address
Employee number

(b) Declare an array of 100 structures for EMPLOYEE

(c) Write a function that will display the employee name together with the employee
number whose age is between 40 to 50 years old.



QUESTION:

(a) create a link list structure with the following members:
name, age, next

(b)
Complete the following function to insert a node at the top of a linked list.
void insertnode(struct node **head)
{
}






Monday, April 12, 2010

CSC202 Revision April 14 - Part 3

QUESTION:

Note that (a), (b),(c) and (d) below are all related to the same program. The program converts
any given currency into USD.
(a) Write the a function prototype called convert() that takes the value of any currency and
returns the USD currency.

(b) Write a main function, then, declare variable currency of type float and declare another
variable usd_currency of type float.

(c) Create a perpetual loop using the statement for(;;)
Within this loop, do the following:
(i) prompt the user: “Enter currency value, 0 to quit: “
(ii) get the input using function scanf() and storing it in variable currency
(iii) using an if statement, quit the program if user enters 0.
(iv) prompt the user: “Enter Currency/USD exchange rate: “
(v) get the input using function scanf() and storing it in variable rate
(vi) call function convert(), passing it the value currency and rate and
storing the return value in usd_currency.
(vii) print the result, eg, “100.00 = USD 139.00”

(d) Write the function definition for the function convert().

SOLUTION:
(a)
float usd convert(float foreign);

(b)
float rate=0;
void main(){
float currency, usd_currency;
}

(c)
for(;;){
printf("Enter currency value, 0 to quit: ");
scanf("%f",&currency);
if(currency==0) return;
printf("Enter Currency/USD exchange rate: ");
scanf("%f",&rate);
usd_currency = convert(currency,rate);
printf("%.2f = USD %.2f\n",currency,usd_currency);
}
(c)
float convert(float currency){
return currency * rate;
}

CSC202 Revision April 14 - Part 2

QUESTION:

Write the main program that will call the triplingByReference function. Create a
variable called number and assign it the value 5. Then call triplingByReference passing
the address of number as the parameter. The function will triple the value stored in
number then store the result back into number. Then print out the result by printing the
new value stored in number.

SOLUTION:

//(a)
void doublingByRef(int *nPtr);

//(b)
void triplingByReference(int *nPtr){
*nPtr = *nPtr * 3;
//*nPtr = 3 * *nPtr;
}

//(c)
void main(){
int number=5;
triplingByReference(&number);
printf("%d\n",number);
}

CSC202 Revision April 14

Note: above is solution to Question 2

QUESTION 1:

Develop a program that will determine the gross pay for each of several employees. The
company pays "straight-time" for the first 40 hours worked by each employee and pays "timeand-
a-half" for all hours worked in excess of 40 hours. You are given a list of employees of the
company, the number of hours each employee worked last week and the hourly rate of each
employee. Your program should input this information for each employee, and should
determine and display the employee's gross pay. Here is a sample input/output dialog:
Enter # of hours worked: 41
Enter hourly rate of the worker: 10.00
Salary is $415.00


SOLUTION:

#include "stdio.h"

void main(){
float hours_worked,hourly_rate,salary;

printf("Enter # of hours worked: ");
scanf("%f",&hours_worked);

printf("Enter hourly rate of the worker: ");
scanf("%f",&hourly_rate);

if(hours_worked<=40){
salary = hours_worked*hourly_rate;
}else{
salary = 40*hourly_rate + (hours_worked-40)*hourly_rate*1.5;
}

printf("Salary is $%.2f\n",salary);
}

QUESTION 2:
(a) Define an array to input 10 elements of type string (hint: use char array);
string “hello”.

(c) Write a loop to print out all the array elements in (b).

SOLUTION 2:





Wednesday, April 7, 2010

CSC202: Link List

Write a program that can display the following menu:

1. Insert Record
2. Search Record
3. Delete Record
4. Print Record
5. Exit

The user will enter 1, 2,3, 4 or 5. Then the program will perform
the appropriate task. Use link list to implement. Use separate functions
for each of the above.


SOLUTION:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct node
{
char firstname[32];
struct node* next;
};


typedef struct node NODE;

void menu(void);
void insert();
void searchlist();
void deletenode();
void printlist();

NODE *root = NULL;

void main()
{
int choice;

for(;;)
{
menu();
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;

case 2:
searchlist();
break;

case 3:
deletenode();
break;

case 4:
printlist();
break;

default:
return;
}
}
}

void menu()
{
printf("\n\n1. Insert Record\n");
printf("2. Search Record\n");
printf("3. Delete Record\n");
printf("4. Print Record\n");
printf("5. Exit\n\n");
}

void insert()
{
NODE *pointer;

//--- if first node ---
if(root==NULL)
{ //memory allocation
pointer = (NODE*)malloc( sizeof(NODE) );
root = pointer;
}
else
{
pointer=root;
while(pointer->next!=NULL)
{
pointer=pointer->next;
}
pointer->next = (NODE*)malloc( sizeof(NODE) );
pointer=pointer->next;
}

char firstname[32]={0};
puts("Enter firstname: ");
scanf("%s",firstname);
strcpy(pointer->firstname,firstname);
pointer->next=NULL;
printlist();
}

void searchlist()
{
NODE *pointer;
pointer = root;

char firstname[32]={0};
puts("Enter firstname: ");
scanf("%s",firstname);
while(pointer!=NULL)
{
if(strcmp(pointer->firstname,firstname)==0)
{
puts("Found");
printf("Firstname: %s\n",pointer->firstname);
return;
}
//printf("%s -> ",pointer->firstname);
pointer=pointer->next;
}
puts("NO Records Found");
}

void deletenode()
{
NODE *pointer, *previous;
pointer = root;

char firstname[32]={0};
puts("Enter firstname: ");
scanf("%s",firstname);
while(pointer!=NULL)
{
if(strcmp(root->firstname,firstname)==0) {
root = root->next;
printlist();
return;
}
if(strcmp(pointer->firstname,firstname)==0)
{
previous->next=pointer->next;
printlist();
return;
}
//printf("%s -> ",pointer->firstname);
previous=pointer;
pointer=pointer->next;
}
puts("NO Records Found");

}

void printlist()
{
NODE *pointer;
pointer = root;
while(pointer!=NULL)
{
printf("%s -> ",pointer->firstname);
pointer=pointer->next;
}
}

Saturday, April 3, 2010

Webcam Biofeedback

Webcam Biofeedback - Tracks breathing and displays graph of heart rate variability:



http://www.youtube.com/watch?v=0L6Q3id4x2k

You can download it here (it's free - full version):

http://forumfiles.thegamecreators.com/download/1985167

After trying, please feedback to me so that I can improve on it. I...t uses an ordinary webcam to detect breathing and simulate heart rate variability. Download includes a user manual. Your webcam needs to be 640x480 resolution (which is what most webcams are). You can use this program to train for heart coherence similar to Wild Divine's Grapher program.

Below is a video showing comparison study between Webcam Biofeedback and Wild Divine's grapher:






http://www.youtube.com/watch?v=lnijwOA0ki0

Thursday, April 1, 2010

Lab Exercise 2: April 1, 2010

Rewrite the previous exercise to store the record inside a file called
data.txt. One line one record.

Also write a function to read from the file and print out the contents.

SOLUTION:
#include "stdio.h"
#include "string.h"

typedef struct name {
char firstname[16];
char lastname[16];
}NAME;

typedef struct student{
char id;
NAME studentname;
}STUDENT;

void main(){
FILE *f;
f = fopen("C:\\data.txt","w");
STUDENT csc202[2];

//--input---
for(int i=0; i<2; i++){
puts("enter id: ");
scanf("%c",&csc202[i].id);
fflush(stdin);
puts("enter firstname: ");
scanf("%s",csc202[i].studentname.firstname);
fflush(stdin);
puts("enter lastname: ");
scanf("%s",csc202[i].studentname.lastname);
fflush(stdin);
fprintf(f,"%c %s %s\n",
csc202[i].id, csc202[i].studentname.firstname,
csc202[i].studentname.lastname);
}
fclose(f);

//---output---
char myid; char firstname[16]={0},lastname[16]={0};
f = fopen("C:\\data.txt","r");
for(int i=0; i<2; i++){
fscanf(f,"%c%s%s",&myid,firstname,lastname);
printf("id: %c\nfirstname: %s\nlastname: %s\n",
myid,firstname,lastname);
}
fclose(f);

}

CSC202: Lab exercise April 1, 2010

Write a program that can store 2 student record using the following
structures:

#include
#include "string.h"

typedef struct name {
char firstname[16];
char lastname[16];
}NAME;

typedef struct student{
char id;
NAME studentname;
}STUDENT;

void main(){
STUDENT csc202[2]; //use an array

...
}

The program will use a loop to input 2 records into the array, then use
a separate loop to print out the contents of the array.

SOLUTION:
#include "stdio.h"
#include "string.h"

typedef struct name {
char firstname[16];
char lastname[16];
}NAME;

typedef struct student{
char id;
NAME studentname;
}STUDENT;

void main(){
STUDENT csc202[2];

//--input---
for(int i=0; i<2; i++){
puts("enter id: ");
scanf("%d",&csc202[i].id);
fflush(stdin);
puts("enter firstname: ");
scanf("%s",csc202[i].studentname.firstname);
fflush(stdin);
puts("enter lastname: ");
scanf("%s",csc202[i].studentname.lastname);
fflush(stdin);
}

//---output---
for(int i=0; i<2; i++){
printf("id: %d\nfirstname: %s\nlastname: %s\n",
csc202[i].id,csc202[i].studentname.firstname,
csc202[i].studentname.lastname);
}

}