CSC103 - LabManual (For Students)
CSC103 - LabManual (For Students)
CSC103 - LabManual (For Students)
Learning Objectives:
1. To make you familiar with the Windows programming environment and the "C"
compiler. For this purpose you will use DevCPP/Code Blocks. You will write, compile and
run a (it may be your first) program in "C"
2. To get you to write several simple algorithms using flowcharts
Your lab report is expected to contain the following for each exercise:
• Flowcharts
• C Source Code
Exercise 3: Write a C-Program to swap two integer numbers without and with using third
variable.
Start
End
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of simple programs in C which make use of basic decision constructs and for loops.
OBS: For each exercise you need to write the flowchart before translating it to your C
program. Your programme should print “Programme written by <your name>” at the end of
the programme on the screen.
Exercise 1: Write a program that reads 10 positive numbers from the keyboard and determines
and displays the sum and average of the numbers.
Exercise 3: Write a program that reads in five integers and then determines and prints the
largest and smallest integers in the group. Use only the techniques you have learnt so far, and
make sure that the program only uses three variables. (Hint: use two of the variables to hold
the current largest and smallest integers.)
Exercise 4: Write a program that reads a number and determines and prints whether it is odd
or even. (Hint: use the modulus operator. Any even number is multiple of two, and any multiple
of two gives a remainder of zero when divided by two.)
Exercise 5: Write a program which asks the user to enter 10 numbers and prints out the
message “even” if the number is even and “divisible by three” if the number is divisible by
three.
Exercise 6: A person invests $1000.0 in a savings account yielding 5% interest. Assuming that all
interest is left on deposit in the account, calculate and print the amount of money in he account
at the end of each year for 10 years. Use the following formula for determining these amounts:
a = p(1+r)n
where
p is the original investment(i.e. the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.
Sample output
Learning Objectives: The objective of this exercise is to get you to write, compile and run more
complex programs in C which make use of while loops and sentinel-controlled repetition.
OBS: For each exercise you need to draw the flowchart before translating to your C prog ram.
Exercise 1: Because of the high price of petrol, you are concerned with the fuel consumption of
your car. As a result you have a record of the kilometres driven and litres used for each tank of
petrol you purchase. Write a program that will display the kilometres driven, litres used, and
consumption (in litres/100km) for each tankful. After processing all the input information, the
program should calculate the overall average consumption:
Exercise 2: Write a C program that uses a while loop to calculate and print the sum of the even
integers from 2 to 30.
Exercise 3: Write a C program that uses a while loop to calculate and print the product of the
odd integers from 3 to 19.
Exercise 5: Write a C program that will determine if a department store customer has exceeded
the credit limit on a charge account. For each customer, the following facts are available:
1. Account number
2. Balance at beginning of month
3. Total of all items charged by the customer this month
4. Total of all credits applied to the account this month
5. Allowed credit limit
The program should input each of these facts, calculate the new balance (initial balance +
charges - credits) and determine if the new balance exceeds the credit limit. If it does, a suitable
message should be displayed.
Exercise 6: A large chemical company pays its sales staff on a commission basis. They receive
£200 per week plus 9% of their gross sales for that week. For example, someone who sells
£5000 of chemicals in one week will earn £200 plus 9% of £5000, a total of £650. Develop a C
program that will input each salesperson’s sales for the previous week, and print out their
salary. Process one person’s figures at a time.
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of programs in C which make use of simple functions.
OBS: For each exercise you need to write the C program and after running your program you
need to show the screen results to the instructor.
Exercise 1: Recall the programme you wrote to implement a simple calculator now rewrite the
same programme using functions.
Exercise 2: Write a program that uses six calls to the function rand() to generate six random
integer numbers, num1, num2, num3, num4, num5, and num6, and then print them out.
num1 should be in the range 1 to 2 (inclusive), num2 should be in the range 1 to 100 (inclusive),
num3 should be in the range 0 to 9, num4 should be in the range 1000 to 1112 (inclusive),
num5 should be in the range -1 to 1, and num 6 should be in the range -3 to 11.
Exercise 3:A car park charges a £2.00 minimum fee to park for up to 3 hours, and an additional
£0.50 for each hour or part hour in excess of three hours. The maximum charge for any given
24-hour period is £10.00. Assume that no car parks for more than 24 hours at a time.
Write a C program that will calculate and print the parking charges for each of 3 customers who
parked their car in the car park yesterday. The program should accept as input the number of
hours that each customer was parked, and output the results in a neat tabular form, along with
the total receipts from the three customers:
The program should use the function calculate_charges to determine the charge for each
customer.
Exercise 4:Implement the following functions. The functions return a real number:
(a) Function Celsius returns the Celsius equivalent of a Fahrenheit temperature (Hint: 0 Celsius
is equal to 32 Fahrenheit and 100 Celsius is equal to 212 Fahrenheit).
Exercise 5:The greatest common divisor of integers x and y is the largest integer that divides
both x and y. Write a recursive function GCD that returns the greatest common divisor of x and
y. The GDC of x and y is defined as follows: If y is equal to zero, then GDC(x, y) is x; otherwise
GDC(x, y) is GDC(y, x % y) where % is the remainder operator.
Learning Objectives: The objective of this exercise is to get you familiar with the collection of
data of same type and manipulate it using arrays.
OBS: For each exercise you need to write the C program and after running your program you
need to show the screen results to the instructor.
A. Write a function getArray() to get array input from the user that will be used to initialize the first
thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would
be set to zero (0).
B. Write a function FindEven() to find the total numbers of even numbers in the given array.
C. Write a function ModifyArray() to make the each array element to a multiple of four(04).
D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return
the smallest element.
Exercise 2: Develop a program to calculate the basic salary of 5 employees where the hourly rate and
the no. of hours worked by each employee are given by the user.
Note: Formula for calculating the basic salary = no. of hours worked * hourly rate.
Exercise 3: Develop a program to input the marks of 10 (ten) different subjects of a student through the
keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the
maximum marks that can be obtained by a student in each subject is 100.
Learning Objectives: The objective of this exercise is to get you familiar with pointers, different
pointer referencing techniques and array traversing using pointers
OBS: For each exercise you need to write the C program and after running your program you
need to show the screen results to the instructor.
Exercise 1. Given the following piece of code
int i = 10;
char c= 'A';
double f = 25.5;
int *iptr = &i;
char *cptr = &c;
You are expected to display the result of following statements in the given format.
Note: The format specifier for displaying address of any data item is %x
Exercise 2
Implement the swap function to exchange the contents of the two variables using pointer
notation for parameter passing.
Implement the following pointer notations to traverse and display the given array ‘arr’.
1. Printing array using array[i] notation.
2. Printing array using ptr[i] notation.
3. Printing array using *(array+i) notation.
4. Printing array using *(ptr+i) notation.
5. Printing array using *ptr notation.
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of simple programs in C which make use ofpointers.
OBS: For each exercise you need to write the C program and after running your program you
need to print the screen results.
Exercise 1: When variables are declared, are they located in memory contiguously? Write a
program with the declaration: char a, b, c, *p, *q, *r;
And print out the locations (addresses) that are assigned to all these variables by your
computer. Are the locations in order? If the locations are in order, are they increasing or
decreasing? Is the address of each pointer variable divisible by 4? If so, this probably means
that each pointer value gets stored in machine word. Double check the results using sizeof() for
char and for char*. (Hint: if you want to see addresses printed as decimal numbers rather than
hexadecimals, it is usually safe to cast an address as unsigned long and use the %lu format).
Exercise 2:Write, compile and execute the program below. Explain why the function swap does
not work properly. Using pointers, change the program to make the function swap to work
properly.
Exercise 3: Write a program that reads 8 floating point values into an array and then prints out
the second, fourth, sixth and eighth members of the array, and the sum of the first, third, fifth
and seventh, using pointers to access the members of the array.
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of simple programs in C which make use ofstrings.
OBS: For each exercise you need to write the C program and after running your program you
need to print the screen results.
Exercise 1: Write a programme which should take an organization name from the user and then
print the abbreviation on the screen.
Use above function in your program to get and print length of a string, copy str1 to str2 and
append str2 at the end of str1. Do not use string.h header file in your programme.
OBS: For each exercise you need to write the C program and after running your program you
need to print the screen results.
Exercise 1: Write a C Program to create the data for some students (roll, name, mark1, mark2,
mark3) and then find the total marks for each student and average mark of each student:
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of simple programs in C which use file management.
OBS: For each exercise you need to write the C program and after running your program you
need to print the screen results.
Exercise 1: Write a C Program to open a file named “DATA” and write a line of text in it by
reading the text from the keyboard.
Exercise 2: Write a C Program to read the contents of file ‘File1’ and paste the contents at the
beginning of another file ‘File2’.
Exercise 3: Write a C Program to count the number of characters and spaces in the input
supplied from a file.