Friday, January 29, 2010
Thursday, January 28, 2010
CSC202 : Exercise 4
1. A salesman gets basic pay $200 per month + commission.
The commission is based on the following formula.
For the first $1000 sales made, the commission is 10% sales.
For the remaining sales above $1000, the commission is 20% of the sales.
For example, if a salesman sale for the month is $3000, then his salary is:
200 + (10% x 1000) + (20% x 2000)
= 200 + 100 + 400
= 700
SOLUTION:

2. Write a program that can find all prime numbers from 2 to 1000.
Example of the first eight prime numbers:
2,3,5,7,11,13,17,23...
SOLUTION:
The commission is based on the following formula.
For the first $1000 sales made, the commission is 10% sales.
For the remaining sales above $1000, the commission is 20% of the sales.
For example, if a salesman sale for the month is $3000, then his salary is:
200 + (10% x 1000) + (20% x 2000)
= 200 + 100 + 400
= 700
SOLUTION:
2. Write a program that can find all prime numbers from 2 to 1000.
Example of the first eight prime numbers:
2,3,5,7,11,13,17,23...
SOLUTION:
Wednesday, January 27, 2010
Friday, January 22, 2010
CSC202 & ENG4033M : Beginning WIN32 API
CSC202 & ENG4033M : File Processing
//File Processing
#include "stdio.h"
void main()
{
char s[] = "avatar";
FILE *f;
f = fopen("D:\\data.txt","w");//w for overwrite
if(!f) return;
fprintf(f,"%s",s);
fclose(f);
f = fopen("D:\\data.txt","r");
if(!f) return;
char t[64];
fscanf(f,"%s",t);
fclose(f);
printf("Read: %s",t);
}
#include "stdio.h"
void main()
{
char s[] = "avatar";
FILE *f;
f = fopen("D:\\data.txt","w");//w for overwrite
if(!f) return;
fprintf(f,"%s",s);
fclose(f);
f = fopen("D:\\data.txt","r");
if(!f) return;
char t[64];
fscanf(f,"%s",t);
fclose(f);
printf("Read: %s",t);
}
CSC202 & ENG4033M : Comparing Strings
//Comparing Strings
#include "stdio.h"
#include "string.h"
void main()
{
char name[32];
scanf("%s",name);
if(strcmp(name,"bill")==0){
printf("Hello Bill Gates\n");
}else{
printf("Hello %s\n",name);
}
}
#include "stdio.h"
#include "string.h"
void main()
{
char name[32];
scanf("%s",name);
if(strcmp(name,"bill")==0){
printf("Hello Bill Gates\n");
}else{
printf("Hello %s\n",name);
}
}
CSC202 & ENG4033M : Function call by reference
//Function Call-By-Reference
#include "stdio.h"
//Function Prototype (Declaration)
void doubleIt(int* n);
int main()
{
//Function Call
int b = 2;
doubleIt(&b);
printf("result = %d\n",b);
return 0;
}
void doubleIt(int* n) //pointer
{
*n = (*n)*2;
}
#include "stdio.h"
//Function Prototype (Declaration)
void doubleIt(int* n);
int main()
{
//Function Call
int b = 2;
doubleIt(&b);
printf("result = %d\n",b);
return 0;
}
void doubleIt(int* n) //pointer
{
*n = (*n)*2;
}
Thursday, January 21, 2010
CSC 202: Exercise 3
Wednesday, January 20, 2010
Friday, January 15, 2010
CSC202 & ENG4033M : Functions
/*Modular Program*/
#include "stdio.h"
void sayHello(); //Function Prototype (Declaration)
int doubleIt(int n);
int main()
{
sayHello(); //Function Call
int n = doubleIt(2);
printf("result = %d\n",n);
return 0;
}
int doubleIt(int n)
{
return n*2;
}
void sayHello() //Function Definition
{
printf("Hello\n");
}
#include "stdio.h"
void sayHello(); //Function Prototype (Declaration)
int doubleIt(int n);
int main()
{
sayHello(); //Function Call
int n = doubleIt(2);
printf("result = %d\n",n);
return 0;
}
int doubleIt(int n)
{
return n*2;
}
void sayHello() //Function Definition
{
printf("Hello\n");
}
CSC202 & ENG4033M : Switch
/*Switch */
#include "stdio.h"
int main()
{
int i;
printf("enter a integer: ");
scanf("%d",&i);
switch(i){
case 0:
printf("zero\n");
break;
case 1:
printf("one\n");
break;
case 2:
printf("two\n");
break;
case 3:
printf("three\n");
break;
default:
printf("other number\n");
break;
}
return 0;
}
/*else if */
#include "stdio.h"
int main()
{
int i;
printf("enter a integer: ");
scanf("%d",&i);
if(i==0) printf("zero\n");
else if(i==1) printf("one\n");
else if(i==2) printf("two\n");
else if(i==3) printf("three\n");
else printf("other number\n");
return 0;
}
#include "stdio.h"
int main()
{
int i;
printf("enter a integer: ");
scanf("%d",&i);
switch(i){
case 0:
printf("zero\n");
break;
case 1:
printf("one\n");
break;
case 2:
printf("two\n");
break;
case 3:
printf("three\n");
break;
default:
printf("other number\n");
break;
}
return 0;
}
/*else if */
#include "stdio.h"
int main()
{
int i;
printf("enter a integer: ");
scanf("%d",&i);
if(i==0) printf("zero\n");
else if(i==1) printf("one\n");
else if(i==2) printf("two\n");
else if(i==3) printf("three\n");
else printf("other number\n");
return 0;
}
CSC202 & ENG4033M : Loops
Thursday, January 14, 2010
CSC202 & ENG4033M: Exercise 2
1. Write a program to input three integers. Then,
print out the total and average.
2. Write a program to input an integer.
If the number is odd, print "Odd Number",
else, print "Even Number".
3. Write a program to input age.
If age is equal to or larger than 16,
print the message "You can party",
else print "Sorry, you cannot party".
If age is larger than 18,
print the message "You can drive a car",
else print "Sorry you cannot drive a car".
print out the total and average.
2. Write a program to input an integer.
If the number is odd, print "Odd Number",
else, print "Even Number".
3. Write a program to input age.
If age is equal to or larger than 16,
print the message "You can party",
else print "Sorry, you cannot party".
If age is larger than 18,
print the message "You can drive a car",
else print "Sorry you cannot drive a car".
Wednesday, January 13, 2010
CSC202 & ENG4033M : Exercise 1
CSC202 & ENG4033M : If - Else Statements & Relational-Equality Operators
Text Version:
/* if statements */
#include "stdio.h"
int main()
{
int a=10, b=3, c=3;
if(b==3){
printf("equals\n");
}
if(a>b){
printf("bigger than\n");
}
if(c=c){
printf("bigger than or equal to\n");
}
if(b<=c){ printf("smaller than or equal to\n"); } return 0; } Another way to write it:
Note also the operator !=
which means NOT EQUAL TO
IF-ELSE example:
CSC202 & ENG4033M : Arithmetic Operators
Monday, January 11, 2010
CSC 202: Programming Assignments
Propose your own Title.
1. Introduction (Written)
Explain what you want to build
(One Paragraph)
(10 marks)
2. Design (Written)
Explain the design of your program.
(1 page)
(20 marks)
3. Write the program (Written)
The actual complete source code.
(1 page or more)
(25 marks)
4. Demonstration of the Program (Oral)
Give a presentation
(Live 5 mins or more)
(20 marks)
5. Explanation (Written)
(1 page or more)
(20 marks)
6. Summary(Written)
(1 paragraph or more)
(5 marks)
After completing the Assignment, please print a Report
consisting of the above parts and submit for marking.
The actual deadline will be announced later. The deadline
will about 2 weeks before end of semester.
1. Introduction (Written)
Explain what you want to build
(One Paragraph)
(10 marks)
2. Design (Written)
Explain the design of your program.
(1 page)
(20 marks)
3. Write the program (Written)
The actual complete source code.
(1 page or more)
(25 marks)
4. Demonstration of the Program (Oral)
Give a presentation
(Live 5 mins or more)
(20 marks)
5. Explanation (Written)
(1 page or more)
(20 marks)
6. Summary(Written)
(1 paragraph or more)
(5 marks)
After completing the Assignment, please print a Report
consisting of the above parts and submit for marking.
The actual deadline will be announced later. The deadline
will about 2 weeks before end of semester.
CSC 202: Tutorial and Test 1 Dates
Tutorial (Open Book): Jan 18 at 10.15 am - 11.45 am (Monday)
Test 1 (Closed Book): Jan 20 at 12.15 noon - 1.45 pm (Wednesday)
Test 1 (Closed Book): Jan 20 at 12.15 noon - 1.45 pm (Wednesday)
Friday, January 8, 2010
CSC202 & ENG4033M : Entering Characters
/* entering characters */
#include "stdio.h"
int main()
{
char a, b, c;
scanf("%c%c%c",&a,&b,&c);
printf("%c %c %c\n",a,b,c);
return 0;
}
#include "stdio.h"
int main()
{
char a, b, c;
scanf("%c%c%c",&a,&b,&c);
printf("%c %c %c\n",a,b,c);
return 0;
}
CSC202 & ENG4033M : A program to input and add 2 numbers
Text Version:
/* A program to add 2 numbers */
#include "stdio.h"
int main()
{
int a,b,c;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
c = a + b;
printf("%d + %d = %d\n",a,b,c);
return 0;
}
A variation (using only one scanf):
/* A program to add 2 numbers */
#include "stdio.h"
int main()
{
int a,b,c;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
c = a + b;
printf("%d + %d = %d\n",a,b,c);
return 0;
}
Thursday, January 7, 2010
ENG4033M: How to open CHM files in Vista/Windows 7
One of my students, Khaw Jun Cong found a solution to the problem of
opening chm files in Vista/Windows 7:
I have just found out how to open it correctly in the internet. It seems like due to the security "feature" in our OS. It can be view after right click on the file and choose "properties" and at the bottom of the properties there is an unblock word, choose unblock and it can be view after pressing the "apply" button, here is a link which help me solve this problem.http://www.west-wind.com/weblog/posts/581253.aspx
Thanks to Jun Cong for sharing!
opening chm files in Vista/Windows 7:
I have just found out how to open it correctly in the internet. It seems like due to the security "feature" in our OS. It can be view after right click on the file and choose "properties" and at the bottom of the properties there is an unblock word, choose unblock and it can be view after pressing the "apply" button, here is a link which help me solve this problem.http://www.west-wind.com/weblog/posts/581253.aspx
Thanks to Jun Cong for sharing!
Wednesday, January 6, 2010
CSC202 & ENG4033M: Hello World Program
How To Write a Hello World program
1. Create a New Project called HelloWorld.
2. Right-click the Source File folder.
3. You will see a Context Menu (Pop-Up Menu).
Click on Add\New Item...
4. You will see a Add New Item dialog box.
Select Code\C++File(.cpp)
Give a name to the file, eg. main, then click Add button
At this point, you should see the following:

Type in the source code as shown outlined in red below:

Text format
(just copy and paste into your Visual C++ 2008):
#include "stdio.h"
int main()
{
printf("Hello World\n");
return 0;
}
5. Then, to build your program.
From the Menu Bar, select Build\Build Solution.
In the Output window at the bottom of the screen,
if you see any error, click on the error message
to go to the line which contains the error.

Correct the error, corrected line should be:
printf("Hello World\n");
then Build again.
6. Once you have successfully built the program, you can Test it.
From the Menu Bar, select either Debug\Start Debugging OR,
select Debug\Start Without Debugging.
Program output:
1. Create a New Project called HelloWorld.
2. Right-click the Source File folder.
3. You will see a Context Menu (Pop-Up Menu).
Click on Add\New Item...
4. You will see a Add New Item dialog box.
Select Code\C++File(.cpp)
Give a name to the file, eg. main, then click Add button
At this point, you should see the following:
Type in the source code as shown outlined in red below:
Text format
(just copy and paste into your Visual C++ 2008):
#include "stdio.h"
int main()
{
printf("Hello World\n");
return 0;
}
5. Then, to build your program.
From the Menu Bar, select Build\Build Solution.
In the Output window at the bottom of the screen,
if you see any error, click on the error message
to go to the line which contains the error.
Correct the error, corrected line should be:
printf("Hello World\n");
then Build again.
6. Once you have successfully built the program, you can Test it.
From the Menu Bar, select either Debug\Start Debugging OR,
select Debug\Start Without Debugging.
Program output:
CSC202 & ENG4033M: How to Use Visual C++ 2008
How to Start a New Project in Visual C++ 2008
1. Start a New Project by clicking
File\New\Project...
2. You will see the New Project dialog box.
Select Win32\Win32ConsoleApplication
The default Folder is
...MyDocuments\Visual Studio 2008\
Create a new Folder called CSC202 in the
above parent Folder, eg:
...MyDocuments\Visual Studio 2008\CSC202
Give a name to the Project, eg
HelloWorld, then click OK.
3. You will then see Welcome to the
Win32 Application Wizard dialog box.
Just click Next.
4. You will then see Application Settings
dialog box. Under Additional Options,
click on Empty Project radio checkbox, then
click Finish button.
You should see the below at this point:
1. Start a New Project by clicking
File\New\Project...
2. You will see the New Project dialog box.
Select Win32\Win32ConsoleApplication
The default Folder is
...MyDocuments\Visual Studio 2008\
Create a new Folder called CSC202 in the
above parent Folder, eg:
...MyDocuments\Visual Studio 2008\CSC202
Give a name to the Project, eg
HelloWorld, then click OK.
3. You will then see Welcome to the
Win32 Application Wizard dialog box.
Just click Next.
4. You will then see Application Settings
dialog box. Under Additional Options,
click on Empty Project radio checkbox, then
click Finish button.
You should see the below at this point:
Subscribe to:
Posts (Atom)